trekhleb/javascript-algorithms · error · Error

The method supports only positive integers

Error message

The method supports only positive integers

What it means

squareRoot(number, tolerance) computes real square roots only: the first guard throws when number is negative because the implementation explicitly avoids complex-number manipulation. Zero is handled as a special case (returns 0), and despite the message saying 'positive integers' the actual check is just number < 0, so any non-negative float is accepted. The message wording is broader than the enforced rule.

Source

Thrown at src/algorithms/math/square-root/squareRoot.js:12

/**
 * Calculates the square root of the number with given tolerance (precision)
 * by using Newton's method.
 *
 * @param number - the number we want to find a square root for.
 * @param [tolerance] - how many precise numbers after the floating point we want to get.
 * @return {number}
 */
export default function squareRoot(number, tolerance = 0) {
  // For now we won't support operations that involves manipulation with complex numbers.
  if (number < 0) {
    throw new Error('The method supports only positive integers');
  }

  // Handle edge case with finding the square root of zero.
  if (number === 0) {
    return 0;
  }

  // We will start approximation from value 1.
  let root = 1;

  // Delta is a desired distance between the number and the square of the root.
  // - if tolerance=0 then delta=1
  // - if tolerance=1 then delta=0.1
  // - if tolerance=2 then delta=0.01
  // - and so on...
  const requiredDelta = 1 / (10 ** tolerance);

  // Approximating the root value to the point when we get a desired precision.

View on GitHub (pinned to 85293e3e2b)

Solutions

  1. Check the value before calling: if (n < 0) handle the no-real-root case explicitly (return NaN, branch, or report)
  2. If negativity signals a bug, fix the upstream math (e.g. wrong operand order in a difference of squares)
  3. For discriminants, branch on the sign before taking the root
  4. If complex results are genuinely needed, use a complex-number library instead of this function

Example fix

// before
import squareRoot from './src/algorithms/math/square-root/squareRoot';
const root = squareRoot(b * b - 4 * a * c);
// throws when the discriminant is negative

// after
const discriminant = b * b - 4 * a * c;
const root = discriminant >= 0 ? squareRoot(discriminant) : NaN;
Defensive patterns

Strategy: validation

Validate before calling

if (typeof value !== 'number' || Number.isNaN(value) || value < 0) {
  return NaN; // no real root
}
const root = squareRoot(value);

Type guard

const isRealRootable = (n) => typeof n === 'number' && !Number.isNaN(n) && n >= 0;

Try / catch

try {
  r = squareRoot(x);
} catch (e) {
  if (e.message === 'The method supports only positive integers') {
    r = NaN; // negative input: no real root
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: squareRoot(-4); squareRoot(a * a - b * b) when b > a; squareRoot(b * b - 4 * a * c) with a negative discriminant; passing unclamped user or sensor input that can go below zero.

Common situations: Numeric code where a value that should be non-negative turns negative through rounding, operand ordering, or bad data (squared distances, variances, discriminants), and unit tests that enumerate negative inputs.

Related errors


AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24). Data as JSON: /api/errors/29b2909c36fa85d5. Report an issue: GitHub.