{"record":{"id":"2d82e4be011d4046","repo":"TheAlgorithms/JavaScript","slug":"input-cannot-be-a-decimal","errorCode":null,"errorMessage":"Input cannot be a Decimal","messagePattern":"Input cannot be a Decimal","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/AliquotSum.js","lineNumber":20,"sourceCode":"  A program to calculate the Aliquot Sum of a number.\n  The aliquot sum of a number n, is the sum of all the proper divisors of n apart from n itself\n  For example, for the number 6\n  The divisors are 1, 2, 3 (we don't consider 6), so its aliquot sum is 1 + 2 + 3 = 6\n  1 is the only number whose aliquot sum is 0 (since its only divisor is 1 and aliquot sum of a number couldn't have itself)\n  For all prime numbers, the aliquot sum is 1, since their only divisor apart from themselves is 1\n  Article on Aliquot Sum: https://en.wikipedia.org/wiki/Aliquot_sum\n */\n\n/**\n * @param {Number} input The number whose aliquot sum you want to calculate\n */\nfunction aliquotSum(input) {\n  // input can't be negative\n  if (input < 0) throw new TypeError('Input cannot be Negative')\n\n  // input can't be a decimal\n  if (Math.floor(input) !== input)\n    throw new TypeError('Input cannot be a Decimal')\n\n  // Dealing with 1, which isn't a prime\n  if (input === 1) return 0\n\n  let sum = 0\n  for (let i = 1; i <= input / 2; i++) {\n    if (input % i === 0) sum += i\n  }\n\n  return sum\n}\n\nexport { aliquotSum }\n","sourceCodeStart":2,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/AliquotSum.js#L2-L34","documentation":"Thrown by aliquotSum(input) as a TypeError when Math.floor(input) !== input, i.e. the value has a fractional part. The aliquot-sum loop indexes divisors by integer steps, so fractional inputs are rejected. Note this check uses Math.floor (not Number.isInteger), so non-number inputs (e.g. strings, NaN) also trip this guard because Math.floor returns NaN and NaN !== NaN.","triggerScenarios":"aliquotSum(6.5); aliquotSum(3.14); aliquotSum('abc') (Math.floor -> NaN, NaN !== 'abc' is true -> throws this message).","commonSituations":"Float division results fed in without rounding; currency/measurements with decimals; unparsed string inputs that slipped past the negative guard.","solutions":["Round to an integer before calling: const n = Math.trunc(input); validate Number.isInteger(n).","Validate at the boundary with Number.isInteger(Number(input)) for a clearer message than the library's.","Reject strings early; coerce with Number() and check finiteness.","Decide a domain rounding policy (floor/round) for fractional inputs before invoking."],"exampleFix":"// before\nconst s = aliquotSum(value) // throws when value is 6.5 or a non-numeric string\n\n// after\nconst n = Math.trunc(Number(value))\nif (!Number.isInteger(n) || n < 0) throw new TypeError('input must be a non-negative integer')\nconst s = aliquotSum(n)","handlingStrategy":"type-guard","validationCode":"function safeAliquotSum(input) {\n  const n = Math.trunc(Number(input))\n  if (!Number.isInteger(n) || n < 0) {\n    throw new TypeError('input must be a non-negative integer')\n  }\n  return aliquotSum(n)\n}","typeGuard":"const isNonNegInt = (v) =>\n  typeof v === 'number' && Number.isInteger(v) && v >= 0","tryCatchPattern":"try {\n  return aliquotSum(input)\n} catch (e) {\n  if (e instanceof TypeError && /decimal/i.test(e.message)) {\n    return aliquotSum(Math.trunc(Number(input)))\n  }\n  throw e\n}","preventionTips":["Round with Math.trunc and validate isInteger before calling.","Coerce strings with Number() and check finiteness, since Math.floor(NaN) trips this guard.","Choose a domain rounding policy (floor vs round) for fractional inputs upstream.","Validate at the boundary so the library never sees decimals."],"tags":["math","aliquot-sum","type-error","integer-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}