{"record":{"id":"2839e3f508ceda7e","repo":"TheAlgorithms/JavaScript","slug":"the-geometric-progression-is-diverging-and-its-su","errorCode":null,"errorMessage":"The geometric progression is diverging, and its sum cannot be calculated","messagePattern":"The geometric progression is diverging, and its sum cannot be calculated","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/SumOfGeometricProgression.js","lineNumber":26,"sourceCode":"  55.5\n  > sumOfGeometricProgression(0.5, 10, Infinity)\n  Error: The geometric progression is diverging, and its sum cannot be calculated\n*/\n\n/**\n *\n * @param {Number} firstTerm The first term of the geometric progression\n * @param {Number} commonRatio The common ratio of the geometric progression\n * @param {Number} numOfTerms The number of terms in the progression\n */\nfunction sumOfGeometricProgression(firstTerm, commonRatio, numOfTerms) {\n  if (!Number.isFinite(numOfTerms)) {\n    /*\n      If the number of Terms is Infinity, the common ratio needs to be less than 1 to be a convergent geometric progression\n      Article on Convergent Series: https://en.wikipedia.org/wiki/Convergent_series\n    */\n    if (Math.abs(commonRatio) < 1) return firstTerm / (1 - commonRatio)\n    throw new Error(\n      'The geometric progression is diverging, and its sum cannot be calculated'\n    )\n  }\n\n  if (commonRatio === 1) return firstTerm * numOfTerms\n\n  return (\n    (firstTerm * (Math.pow(commonRatio, numOfTerms) - 1)) / (commonRatio - 1)\n  )\n}\n\nexport { sumOfGeometricProgression }\n","sourceCodeStart":8,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/SumOfGeometricProgression.js#L8-L39","documentation":"Thrown by `sumOfGeometricProgression(firstTerm, commonRatio, numOfTerms)` only when `numOfTerms` is non-finite (Infinity) AND `|commonRatio| >= 1`. A convergent infinite geometric series requires `|r| < 1`; outside that, the partial sums have no limit. Pass a finite `numOfTerms`, or ensure `|commonRatio| < 1` for the infinite case.","triggerScenarios":"Call `sumOfGeometricProgression(2, 2, Infinity)`, `sumOfGeometricProgression(1, 1, Infinity)`, `sumOfGeometricProgression(5, -3, Infinity)`. The Infinity case with `|ratio| >= 1` is the only trigger.","commonSituations":"Computing a series sum where the term count was derived from a division that produced Infinity (`1/0`), forwarding `Number.POSITIVE_INFINITY` from a config that meant 'all terms', or accidentally inverting the ratio argument.","solutions":["If you want the infinite sum, ensure `Math.abs(commonRatio) < 1` before calling.","If `commonRatio >= 1` is intentional, pass a finite `numOfTerms`.","Sanitise upstream sources of Infinity (division by zero, `Math.pow` overflows)."],"exampleFix":"// before\nsumOfGeometricProgression(2, 2, Infinity) // diverges -> throws\n\n// after\n// finite truncation:\nsumOfGeometricProgression(2, 2, 1000)\n// OR a convergent infinite series:\nsumOfGeometricProgression(2, 0.5, Infinity)","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(numOfTerms) && Math.abs(commonRatio) >= 1) {\n  throw new Error('infinite geometric sum diverges for |commonRatio| >= 1')\n}\nsumOfGeometricProgression(firstTerm, commonRatio, numOfTerms)","typeGuard":null,"tryCatchPattern":"try {\n  sumOfGeometricProgression(firstTerm, commonRatio, numOfTerms)\n} catch (e) {\n  if (e.message.includes('diverging')) {\n    // fall back to a finite truncation\n    sumOfGeometricProgression(firstTerm, commonRatio, 1000)\n  } else throw e\n}","preventionTips":["Decide upfront whether you want an infinite or truncated sum.","For infinite sums, enforce Math.abs(commonRatio) < 1 in your validator.","Sanitise sources of Infinity (1/0, Math.pow overflow) at the boundary."],"tags":["math","domain-validation","infinite-series","convergence"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}