{"record":{"id":"0043ba5fbbfdd40b","repo":"TheAlgorithms/Go","slug":"interval-boundaries-should-be-finite-numbers","errorCode":null,"errorMessage":"interval boundaries should be finite numbers","messagePattern":"interval boundaries should be finite numbers","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"search/ternary.go","lineNumber":12,"sourceCode":"package search\n\nimport (\n\t\"fmt\"\n\t\"math\"\n)\n\n// TernaryMax is a function to search for maximum value of a uni-modal function `f`\n// in the interval [a, b]. a and b should be finit numbers\nfunc TernaryMax(a, b, epsilon float64, f func(x float64) float64) (float64, error) {\n\tif a == math.Inf(-1) || b == math.Inf(1) {\n\t\treturn -1, fmt.Errorf(\"interval boundaries should be finite numbers\")\n\t}\n\tif math.Abs(a-b) <= epsilon {\n\t\treturn f((a + b) / 2), nil\n\t}\n\tleft := (2*a + b) / 3\n\tright := (a + 2*b) / 3\n\tif f(left) < f(right) {\n\t\treturn TernaryMax(left, b, epsilon, f)\n\t}\n\treturn TernaryMax(a, right, epsilon, f)\n}\n\n// TernaryMin is a function to search for minimum value of a uni-modal function `f`\n// in the interval [a, b]. a and b should be finit numbers.\nfunc TernaryMin(a, b, epsilon float64, f func(x float64) float64) (float64, error) {\n\tif a == math.Inf(-1) || b == math.Inf(1) {\n\t\treturn -1, fmt.Errorf(\"interval boundaries should be finite numbers\")\n\t}","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/search/ternary.go#L1-L30","documentation":"TernaryMax performs ternary search for the maximum of a unimodal function over [a, b], which requires finite bounds. The library rejects infinite boundaries (a == -Inf or b == +Inf) because the recursive interval-splitting would never converge. It is thrown as a sentinel-style validation error at the top of the function.","triggerScenarios":"Calling TernaryMax(math.Inf(-1), b, eps, f) or TernaryMax(a, math.Inf(1), eps, f) — i.e. passing an unbounded interval such as searching over the entire real line or using -Inf/+Inf placeholders for 'no limit'.","commonSituations":"Developers modeling 'search from negative infinity' for an unbounded optimization, mistakenly defaulting unset config values to math.Inf, or porting code from pseudo-code that assumes infinite bounds are supported.","solutions":["Pass finite numeric bounds a and b that actually bracket the unimodal region (e.g. -1e9/1e9 instead of ±Inf).","If the domain is unbounded, first run an exponential/search expansion phase to find finite brackets, then call TernaryMax.","Validate inputs with math.IsInf(a, ...) / math.IsInf(b, ...) before calling and return a clearer domain-specific error."],"exampleFix":"// before\nmax, err := search.TernaryMax(math.Inf(-1), 100, 1e-6, f)\n// after\nmax, err := search.TernaryMax(-1000, 100, 1e-6, f)","handlingStrategy":"validation","validationCode":"if math.IsInf(a, -1) || math.IsInf(b, 1) {\n    return errors.New(\"ternary search requires finite interval bounds\")\n}\nmax, err := search.TernaryMax(a, b, eps, f)","typeGuard":"func finiteBounds(a, b float64) bool {\n    return !math.IsInf(a, -1) && !math.IsInf(b, 1) && !math.IsNaN(a) && !math.IsNaN(b)\n}","tryCatchPattern":"max, err := search.TernaryMax(a, b, eps, f)\nif err != nil && strings.Contains(err.Error(), \"finite\") {\n    // fall back to finite bracketing, then retry\n}","preventionTips":["Never pass math.Inf as a search bound; use large finite sentinels instead.","Validate interval arguments with math.IsInf/IsNaN before any optimization call.","Implement a bracketing step (exponential search) when the domain is unbounded."],"tags":["go","math","ternary-search","input-validation"],"backgroundTag":"non-finite-argument","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}