{"record":{"id":"d337dfdf06449f6f","repo":"TheAlgorithms/JavaScript","slug":"expected-integer-n-and-finite-a-b","errorCode":null,"errorMessage":"Expected integer N and finite a, b","messagePattern":"Expected integer N and finite a, b","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/MidpointIntegration.js","lineNumber":23,"sourceCode":" * @brief Calculate definite integrals with midpoint method\n *\n * @details The idea is to split the interval in a number N of intervals and use as interpolation points the xi\n * 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\n * first and last points of the interval of the integration [a, b].\n *\n * We create a table of the xi and their corresponding f(xi) values and we evaluate the integral by the formula:\n * I = h * {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}\n *\n * N must be > 0 and a<b. By increasing N, we also increase precision\n *\n * [More info link](https://tutorial.math.lamar.edu/classes/calcii/approximatingdefintegrals.aspx)\n *\n */\n\nfunction integralEvaluation(N, a, b, func) {\n  // Check if all restrictions are satisfied for the given N, a, b\n  if (!Number.isInteger(N) || Number.isNaN(a) || Number.isNaN(b)) {\n    throw new TypeError('Expected integer N and finite a, b')\n  }\n  if (N <= 0) {\n    throw Error('N has to be >= 2')\n  } // check if N > 0\n  if (a > b) {\n    throw Error('a must be less or equal than b')\n  } // Check if a < b\n  if (a === b) return 0 // If a === b integral is zero\n\n  // Calculate the step h\n  const h = (b - a) / N\n\n  // Find interpolation points\n  let xi = a // initialize xi = x0\n  const pointsArray = []\n\n  // Find the sum {f(x0+h/2) + f(x1+h/2) + ... + f(xN-1+h/2)}\n  let temp","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/MidpointIntegration.js#L5-L41","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nintegralEvaluation((b - a) / h, a, b, f)\n// after\nintegralEvaluation(Math.round((b - a) / h), a, b, f)","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(N) || !Number.isFinite(a) || !Number.isFinite(b)) {\n  throw new TypeError('Expected integer N and finite a, b')\n}\nintegralEvaluation(N, a, b, func)","typeGuard":"const isValidIntegrationInput = (N, a, b) => Number.isInteger(N) && Number.isFinite(a) && Number.isFinite(b)","tryCatchPattern":null,"preventionTips":["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."],"tags":["math","calculus","type-validation","numerical-integration"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}