{"record":{"id":"9e89d740edf4d7fa","repo":"hashicorp/terraform","slug":"can-t-compute-sum-of-opposing-infinities","errorCode":null,"errorMessage":"can't compute sum of opposing infinities","messagePattern":"can't compute sum of opposing infinities","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lang/funcs/collection.go","lineNumber":532,"sourceCode":"\t\targ := args[0].AsValueSlice()\n\t\tty := args[0].Type()\n\n\t\tif !ty.IsListType() && !ty.IsSetType() && !ty.IsTupleType() {\n\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple. Received %s\", ty.FriendlyName())\n\t\t}\n\n\t\tif !args[0].IsWhollyKnown() {\n\t\t\treturn cty.UnknownVal(cty.Number), nil\n\t\t}\n\n\t\t// big.Float.Add can panic if the input values are opposing infinities,\n\t\t// so we must catch that here in order to remain within\n\t\t// the cty Function abstraction.\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tif _, ok := r.(big.ErrNaN); ok {\n\t\t\t\t\tret = cty.NilVal\n\t\t\t\t\terr = fmt.Errorf(\"can't compute sum of opposing infinities\")\n\t\t\t\t} else {\n\t\t\t\t\t// not a panic we recognize\n\t\t\t\t\tpanic(r)\n\t\t\t\t}\n\t\t\t}\n\t\t}()\n\n\t\ts := arg[0]\n\t\tif s.IsNull() {\n\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple of number values\")\n\t\t}\n\t\ts, err = convert.Convert(s, cty.Number)\n\t\tif err != nil {\n\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple of number values\")\n\t\t}\n\t\tfor _, v := range arg[1:] {\n\t\t\tif v.IsNull() {\n\t\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple of number values\")","sourceCodeStart":514,"sourceCodeEnd":550,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/lang/funcs/collection.go#L514-L550","documentation":"sum() function error: summing numbers produced an opposing-infinity result (+Inf + -Inf), which Go's big.Float.Add signals by panicking with big.ErrNaN. The deferred recover catches that specific panic type and converts it to this controlled error; any other panic is re-thrown. This keeps the cty Function abstraction panic-free.","triggerScenarios":"Calling sum() on a list containing both positive and negative infinity (e.g. [math.inf, -math.inf] equivalent cty values), so big.Float.Add panics with ErrNaN.","commonSituations":"Computed values derived from division-by-zero producing +/-Inf and then aggregated with sum(); data pipelines that mix unbounded-positive and unbounded-negative signals.","solutions":["Filter or clamp infinities before summing (e.g. exclude infinite values with a for-expression).","Trace which upstream computation yields +/-Inf and fix the root cause (often a divide by zero).","Replace infinite values with large finite bounds if approximation is acceptable."],"exampleFix":"// before\nlocals { total = sum([var.a, var.b]) } # a=+Inf, b=-Inf\n\n// after\nlocals { total = sum([for v in [var.a, var.b] : v if v != math.inf && v != -math.inf]) }","handlingStrategy":"validation","validationCode":"// Filter infinities before summing.\nlocals {\n  finite = [for v in var.nums : v if v != \"Infinity\" && v != \"-Infinity\"]\n  total  = sum(local.finite)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Trace and eliminate the source of +/-Inf (often a divide-by-zero upstream).","Filter or clamp infinite values before aggregation.","Validate numeric inputs fall within finite ranges."],"tags":["terraform","hcl-functions","sum","numeric","infinity","recover"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}