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 the midpoint rule with N subintervals. The guard requires N to be an integer and a, b to be non-NaN. Important caveat: the code uses Number.isNaN(a) rather than !Number.isFinite(a), so Infinity would pass this check and could cause silent numerical issues downstream. Subsequent checks enforce N > 0 and a <= b.
Source
Thrown at Maths/MidpointIntegration.js:23
* @brief Calculate definite integrals with midpoint method
*
* @details The idea is to split the interval in a number N of intervals and use as interpolation points the xi
* for which it applies that xi = x0 + i*h, where h is a step defined as h = (b-a)/N where a and b are the
* first and last points of the interval of the integration [a, b].
*
* We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:
* I = h * {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
*
* N must be > 0 and a<b. By increasing N, we also increase precision
*
* [More info link](https://tutorial.math.lamar.edu/classes/calcii/approximatingdefintegrals.aspx)
*
*/
function integralEvaluation(N, a, b, func) {
// Check if all restrictions are satisfied for the given N, a, b
if (!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) {
throw new TypeError('Expected integer N and finite a, b')
}
if (N <= 0) {
throw Error('N has to be >= 2')
} // check if N > 0
if (a > b) {
throw Error('a must be less or equal than b')
} // Check if a < b
if (a === b) return 0 // If a === b integral is zero
// Calculate the step h
const h = (b - a) / N
// Find interpolation points
let xi = a // initialize xi = x0
const pointsArray = []
// Find the sum {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}
let tempView on GitHub (pinned to 5c39e87a9a)
Solutions
- Ensure N is an integer (use Math.round or parseInt).
- Validate a and b are finite numbers before calling.
- Parse string config values to numbers before invoking.
Example fix
// before integralEvaluation((b - a) / h, a, b, f) // after integralEvaluation(Math.round((b - a) / h), a, b, f)
Defensive patterns
Strategy: validation
Validate before calling
if (!Number.isInteger(N) || !Number.isFinite(a) || !Number.isFinite(b)) {
throw new TypeError('Expected integer N and finite a, b')
}
integralEvaluation(N, a, b, func) Type guard
const isValidIntegrationInput = (N, a, b) => Number.isInteger(N) && Number.isFinite(a) && Number.isFinite(b)
Prevention
- Round N to an integer with Math.round() before integration calls.
- Validate interval bounds a and b are finite (use Number.isFinite, not just !isNaN, to catch Infinity).
- Parse string configuration values to numbers before passing.
When it happens
Trigger: Calling integralEvaluation(1.5, 0, 1, f) (non-integer N), integralEvaluation(10, NaN, 1, f) (NaN a), or integralEvaluation("10", 0, 1, f) (string N, since Number.isInteger("10") is false).
Common situations: N computed as a non-integer from a division (e.g., (b-a)/h), NaN from undefined interval bounds, or string arguments read from a configuration file.
Related errors
- Expected integer N and finite a, b
- Index cannot be a Decimal
- Expected a number, received ${typeof num}
- Expected a number, received ${typeof precision}
- Input data must be numbers
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/d337dfdf06449f6f.
Report an issue: GitHub.