TheAlgorithms/JavaScript · error · Error

n cannot be a floating point number

Error message

n cannot be a floating point number

What it means

Thrown by isAutomorphic(n) (AutomorphicNumber.js:22) as a plain Error when n is a number but not an integer (Number.isInteger returns false). The algorithm extracts digits via modulo and floor division, which is meaningless for fractional inputs. Per the documented convention, negative integers return false (they are whole numbers but not considered automorphic), while floating-point values throw. Whitespace floats like 5.0 are accepted by Number.isInteger since they coerce to 5.

Source

Thrown at Maths/AutomorphicNumber.js:23

 * @see [Automorphic] (https://en.wikipedia.org/wiki/Automorphic_number)
 * @description This script will check whether a number is Automorphic or not
 * @description A number n is said to be a Automorphic number if the square of n ends in the same digits as n itself.
 *
 * @param {Integer} n - the n for nth Catalan Number
 * @return {Integer} - the nth Catalan Number
 * @complexity Time: O(log10(n)) , Space: O(1)
 *
 * @convention We define Automorphic only for whole number integers. For negetive integer we return False. For float or String we show error.
 * @examples 0, 1, 5, 6, 25, 76, 376, 625, 9376 are some Automorphic numbers
 */

// n is the number to be checked
export const isAutomorphic = (n) => {
  if (typeof n !== 'number') {
    throw new Error('Type of n must be number')
  }
  if (!Number.isInteger(n)) {
    throw new Error('n cannot be a floating point number')
  }
  if (n < 0) {
    return false
  }

  // now n is a whole number integer >= 0
  let n_sq = n * n
  while (n > 0) {
    if (n % 10 !== n_sq % 10) {
      return false
    }
    n = Math.floor(n / 10)
    n_sq = Math.floor(n_sq / 10)
  }

  return true
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Round or truncate with Math.round / Math.trunc before calling if fractional parts are noise.
  2. Validate with Number.isInteger(n) at the call site and reject or round accordingly.
  3. If Infinity is possible, guard with Number.isFinite() first.
  4. Check upstream divisions or sqrt operations that may have produced a non-integer.

Example fix

// before
const r = isAutomorphic(computed) // computed may be 25.0000001

// after
const n = Math.round(computed)
if (!Number.isInteger(n)) throw new Error('non-integer input')
const r = isAutomorphic(n)
Defensive patterns

Strategy: validation

Validate before calling

const n = Math.trunc(value)
if (!Number.isInteger(n)) {
  throw new Error('input must be an integer')
}
const result = isAutomorphic(n)

Type guard

const isWholeInteger = (v) => Number.isInteger(v)

Try / catch

try {
  result = isAutomorphic(n)
} catch (e) {
  if (e instanceof Error && /floating point/.test(e.message)) {
    // float passed — round or reject
  } else throw e
}

Prevention

When it happens

Trigger: Call isAutomorphic(25.5); pass a value from a calculation that introduced a fractional component (e.g. division result); isAutomorphic(NaN) actually fails the typeof check first, but isAutomorphic(Infinity) reaches here since Infinity is typeof number but not an integer.

Common situations: Arithmetic that assumes integer results but produces floats (e.g. 0.1 + 0.2); data typed as float in a database column; accidental decimal input from a numeric field with step settings.

Related errors


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