TheAlgorithms/JavaScript · error · TypeError

Expected integer N and finite a, b

Error message

Expected integer N and finite a, b

What it means

The integralEvaluation function approximates a definite integral using composite Simpson's rule, which requires N to be an even integer (Simpson's rule needs pairs of intervals). The guard checks for integer N and non-NaN a/b. Note: the code computes isNEven before the type check, so odd integers pass this guard and hit a separate 'N is not an even number' error; this specific error is for non-integer N or NaN a/b.

Source

Thrown at Maths/SimpsonIntegration.js:30

 * We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:
 * I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)}
 *
 * That means that the first and last indexed i f(xi) are multiplied by 1,
 * the odd indexed f(xi) by 4 and the even by 2.
 *
 * N must be even number and a<b. By increasing N, we also increase precision
 *
 * More info: [Wikipedia link](https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule)
 *
 */

function integralEvaluation(N, a, b, func) {
  // Check if N is an even integer
  let isNEven = true
  if (N % 2 !== 0) isNEven = false

  if (!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) {
    throw new TypeError('Expected integer N and finite a, b')
  }
  if (!isNEven) {
    throw Error('N is not an even number')
  }
  if (N <= 0) {
    throw Error('N has to be >= 2')
  }

  // Check if a < b
  if (a > b) {
    throw Error('a must be less or equal than b')
  }
  if (a === b) return 0

  // Calculate the step h
  const h = (b - a) / N

  // Find interpolation points

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Ensure N is an even integer.
  2. Round N with Math.round() and adjust to the nearest even number.
  3. Validate a and b are finite numbers before calling.

Example fix

// before
integralEvaluation((b - a) / h, a, b, f)
// after
const N = Math.round((b - a) / h)
integralEvaluation(N % 2 !== 0 ? N + 1 : N, a, b, f)
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isInteger(N) || N % 2 !== 0 || !Number.isFinite(a) || !Number.isFinite(b)) {
  throw new TypeError('Expected even integer N and finite a, b')
}
integralEvaluation(N, a, b, func)

Type guard

const isValidSimpsonInput = (N, a, b) =>
  Number.isInteger(N) && N % 2 === 0 && Number.isFinite(a) && Number.isFinite(b)

Prevention

When it happens

Trigger: Calling integralEvaluation(10.5, 0, 1, f) (non-integer N), integralEvaluation(10, NaN, 1, f) (NaN a), or integralEvaluation("10", 0, 1, f) (string N). Odd integer N like 11 does NOT trigger this error (it triggers the even-check instead).

Common situations: N derived from a non-integer division, NaN from missing interval bounds, or string values from configuration files.

Related errors


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