TheAlgorithms/JavaScript · error · RangeError

Unsupported base. Must be in range [2, 10]

Error message

Unsupported base. Must be in range [2, 10]

What it means

Thrown by decExp(a, b, base = 10, ...) (DecimalExpansion.js:24) as a RangeError when the base argument is less than 2 or greater than 10. The function computes the expansion of a/b in the given base using Euclidean division and digit conversion via Number.prototype.toString(base), which itself only supports radix 2-36, but this library further restricts to single-digit bases (2-10). The check runs on every recursive call but base never changes during recursion.

Source

Thrown at Maths/DecimalExpansion.js:25

 * Because this function is recursive, it may throw an error when reaching the
 * maximum call stack size.
 *
 * Returns an array containing : [
 *  0: integer part of the division
 *  1: array of decimals (if any, or an empty array)
 *  2: indexOf 1st cycle digit in decimals array if a/b is periodic, or undef.
 * ]
 *
 * @see https://mathworld.wolfram.com/DecimalExpansion.html
 *
 * @param {number} a
 * @param {number} b
 * @param {number} [base=10]
 * @returns {array}
 */
export function decExp(a, b, base = 10, exp = [], d = {}, dlen = 0) {
  if (base < 2 || base > 10) {
    throw new RangeError('Unsupported base. Must be in range [2, 10]')
  }

  if (a === 0) {
    return [0, [], undefined]
  }

  if (a === b && dlen === 0) {
    return [1, [], undefined]
  }

  // d contains the dividends used so far and the corresponding index of its
  // euclidean division by b in the expansion array.
  d[a] = dlen++

  if (a < b) {
    exp.push(0)
    return decExp(a * base, b, base, exp, d, dlen)
  }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Restrict base to the supported range before calling: base = Math.min(10, Math.max(2, base)).
  2. If you need base > 10, use a different library or implement the expansion yourself with a custom digit alphabet.
  3. Remember the default base is 10; only pass the third argument when you actually need binary/octal/etc.
  4. Do not pass values into the third slot unless you intend to set the base — the trailing params are internal.

Example fix

// before
const e = decExp(1, 3, 16) // throws RangeError

// after
const e = decExp(1, 3, 10) // use base 10, or clamp:
// const base = Math.min(10, Math.max(2, requestedBase))
// const e = decExp(1, 3, base)
Defensive patterns

Strategy: validation

Validate before calling

const safeBase = (base == null) ? 10 : Math.min(10, Math.max(2, Math.floor(base)))
const e = decExp(a, b, safeBase)

Type guard

const isValidBase = (v) => typeof v === 'number' && v >= 2 && v <= 10 && Number.isInteger(v)

Try / catch

try {
  e = decExp(a, b, base)
} catch (e) {
  if (e instanceof RangeError && /Unsupported base/.test(e.message)) {
    // base out of [2,10] — clamp and retry
    e = decExp(a, b, Math.min(10, Math.max(2, base)))
  } else throw e
}

Prevention

When it happens

Trigger: Call decExp(1, 3, 16) requesting hexadecimal (base 16); decExp(1, 3, 0) or decExp(1, 3, 1) with an out-of-range low base; omit the third positional argument incorrectly and pass a value into the base slot that was meant for another parameter.

Common situations: Assuming the function supports hexadecimal or other radix > 10 (it does not); confusing the parameter order (the function has internal params exp, d, dlen after base); passing a base from user input without clamping.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/9fd79df02fcc22dc. Report an issue: GitHub.