{"record":{"id":"7b12cb25cb84f79b","repo":"TheAlgorithms/JavaScript","slug":"n-cannot-be-a-floating-point-number","errorCode":null,"errorMessage":"n cannot be a floating point number","messagePattern":"n cannot be a floating point number","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/AutomorphicNumber.js","lineNumber":23,"sourceCode":" * @see [Automorphic] (https://en.wikipedia.org/wiki/Automorphic_number)\n * @description This script will check whether a number is Automorphic or not\n * @description A number n is said to be a Automorphic number if the square of n ends in the same digits as n itself.\n *\n * @param {Integer} n - the n for nth Catalan Number\n * @return {Integer} - the nth Catalan Number\n * @complexity Time: O(log10(n)) , Space: O(1)\n *\n * @convention We define Automorphic only for whole number integers. For negetive integer we return False. For float or String we show error.\n * @examples 0, 1, 5, 6, 25, 76, 376, 625, 9376 are some Automorphic numbers\n */\n\n// n is the number to be checked\nexport const isAutomorphic = (n) => {\n  if (typeof n !== 'number') {\n    throw new Error('Type of n must be number')\n  }\n  if (!Number.isInteger(n)) {\n    throw new Error('n cannot be a floating point number')\n  }\n  if (n < 0) {\n    return false\n  }\n\n  // now n is a whole number integer >= 0\n  let n_sq = n * n\n  while (n > 0) {\n    if (n % 10 !== n_sq % 10) {\n      return false\n    }\n    n = Math.floor(n / 10)\n    n_sq = Math.floor(n_sq / 10)\n  }\n\n  return true\n}\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/AutomorphicNumber.js#L5-L41","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Round or truncate with Math.round / Math.trunc before calling if fractional parts are noise.","Validate with Number.isInteger(n) at the call site and reject or round accordingly.","If Infinity is possible, guard with Number.isFinite() first.","Check upstream divisions or sqrt operations that may have produced a non-integer."],"exampleFix":"// before\nconst r = isAutomorphic(computed) // computed may be 25.0000001\n\n// after\nconst n = Math.round(computed)\nif (!Number.isInteger(n)) throw new Error('non-integer input')\nconst r = isAutomorphic(n)","handlingStrategy":"validation","validationCode":"const n = Math.trunc(value)\nif (!Number.isInteger(n)) {\n  throw new Error('input must be an integer')\n}\nconst result = isAutomorphic(n)","typeGuard":"const isWholeInteger = (v) => Number.isInteger(v)","tryCatchPattern":"try {\n  result = isAutomorphic(n)\n} catch (e) {\n  if (e instanceof Error && /floating point/.test(e.message)) {\n    // float passed — round or reject\n  } else throw e\n}","preventionTips":["Round results of divisions before passing if integers are expected.","Guard against Infinity with Number.isFinite before the integer check.","Be aware that negative integers are accepted and return false, not throw."],"tags":["validation","integer","numeric","floating-point"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}