{"id":"e7ccd369eb2e665c","repo":"vitest-dev/vitest","slug":"negative-numbers-are-not-supported","errorCode":null,"errorMessage":"Negative numbers are not supported","messagePattern":"Negative numbers are not supported","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"examples/profiling/src/prime-number.ts","lineNumber":62,"sourceCode":"  if (number === 3n) {\n    return true\n  }\n\n  const squareRoot = bigIntSquareRoot(number)\n\n  // Intentionally inefficient to highlight performance issues\n  for (let i = 3n; i < squareRoot; i += 2n) {\n    if (number % i === 0n) {\n      return false\n    }\n  }\n\n  return true\n}\n\nfunction bigIntSquareRoot(number: bigint): bigint {\n  if (number < 0n) {\n    throw new Error('Negative numbers are not supported')\n  }\n  if (number < 2n) {\n    return number\n  }\n\n  function iterate(value: bigint, guess: bigint): bigint {\n    const nextGuess = (value / guess + guess) >> 1n\n\n    if (guess === nextGuess) {\n      return guess\n    }\n    if (guess === nextGuess - 1n) {\n      return guess\n    }\n\n    return iterate(value, nextGuess)\n  }\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/examples/profiling/src/prime-number.ts#L44-L80","documentation":"Thrown by the internal bigIntSquareRoot() helper in examples/profiling when its argument is a negative BigInt. The Newton's-method square root iteration is only defined for non-negative values, so the guard fails fast. Note: bigIntSquareRoot is not exported and is unreachable through getPrimeNumbers (which only produces positive BigInts), so this fires only if the helper is reused directly with a negative input.","triggerScenarios":"Calling bigIntSquareRoot(-1n) or any negative BigInt. Through the public getPrimeNumbers path this is effectively unreachable because randomBigInt always yields positive values.","commonSituations":"Copying the helper into another project and feeding it a signed difference (e.g. a - b where a < b), or unit-testing the internal helper directly with negative fixtures.","solutions":["Pass only non-negative BigInts to the square-root helper.","If you may receive signed values, clamp or take the absolute value first: bigIntSquareRoot(value < 0n ? -value : value).","Add an upstream guard so callers cannot reach this path with negative data."],"exampleFix":"// before\nbigIntSquareRoot(a - b) // a < b -> negative -> throws\n\n// after\nconst diff = a < b ? b - a : a - b\nbigIntSquareRoot(diff)","handlingStrategy":"validation","validationCode":"function safeSqrt(n: bigint): bigint {\n  if (n < 0n) throw new RangeError(`expected non-negative bigint, got ${n}`)\n  return bigIntSquareRoot(n)\n}","typeGuard":"function isNonNegativeBigInt(v: unknown): v is bigint {\n  return typeof v === 'bigint' && v >= 0n\n}","tryCatchPattern":null,"preventionTips":["Normalize signed differences to absolute values before square-root.","Add upstream range guards so negative inputs never reach math helpers.","Note bigIntSquareRoot is internal; validate at the public API boundary instead."],"tags":["bigint","math","validation","example-code","unreachable"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}