TheAlgorithms/JavaScript · error · Error

The geometric progression is diverging, and its sum cannot b

Error message

The geometric progression is diverging, and its sum cannot be calculated

What it means

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.

Source

Thrown at Maths/SumOfGeometricProgression.js:26

  55.5
  > sumOfGeometricProgression(0.5, 10, Infinity)
  Error: The geometric progression is diverging, and its sum cannot be calculated
*/

/**
 *
 * @param {Number} firstTerm The first term of the geometric progression
 * @param {Number} commonRatio The common ratio of the geometric progression
 * @param {Number} numOfTerms The number of terms in the progression
 */
function sumOfGeometricProgression(firstTerm, commonRatio, numOfTerms) {
  if (!Number.isFinite(numOfTerms)) {
    /*
      If the number of Terms is Infinity, the common ratio needs to be less than 1 to be a convergent geometric progression
      Article on Convergent Series: https://en.wikipedia.org/wiki/Convergent_series
    */
    if (Math.abs(commonRatio) < 1) return firstTerm / (1 - commonRatio)
    throw new Error(
      'The geometric progression is diverging, and its sum cannot be calculated'
    )
  }

  if (commonRatio === 1) return firstTerm * numOfTerms

  return (
    (firstTerm * (Math.pow(commonRatio, numOfTerms) - 1)) / (commonRatio - 1)
  )
}

export { sumOfGeometricProgression }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. If you want the infinite sum, ensure `Math.abs(commonRatio) < 1` before calling.
  2. If `commonRatio >= 1` is intentional, pass a finite `numOfTerms`.
  3. Sanitise upstream sources of Infinity (division by zero, `Math.pow` overflows).

Example fix

// before
sumOfGeometricProgression(2, 2, Infinity) // diverges -> throws

// after
// finite truncation:
sumOfGeometricProgression(2, 2, 1000)
// OR a convergent infinite series:
sumOfGeometricProgression(2, 0.5, Infinity)
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isFinite(numOfTerms) && Math.abs(commonRatio) >= 1) {
  throw new Error('infinite geometric sum diverges for |commonRatio| >= 1')
}
sumOfGeometricProgression(firstTerm, commonRatio, numOfTerms)

Try / catch

try {
  sumOfGeometricProgression(firstTerm, commonRatio, numOfTerms)
} catch (e) {
  if (e.message.includes('diverging')) {
    // fall back to a finite truncation
    sumOfGeometricProgression(firstTerm, commonRatio, 1000)
  } else throw e
}

Prevention

When it happens

Trigger: Call `sumOfGeometricProgression(2, 2, Infinity)`, `sumOfGeometricProgression(1, 1, Infinity)`, `sumOfGeometricProgression(5, -3, Infinity)`. The Infinity case with `|ratio| >= 1` is the only trigger.

Common situations: 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.

Related errors


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