{"id":"ae92357b64adb89d","repo":"stretchr/testify","slug":"expected-value-must-not-be-nan","errorCode":null,"errorMessage":"expected value must not be NaN","messagePattern":"expected value must not be NaN","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"assert/assertions.go","lineNumber":1564,"sourceCode":"\t\t) {\n\t\t\treturn false\n\t\t}\n\t}\n\n\treturn true\n}\n\nfunc calcRelativeError(expected, actual interface{}) (float64, error) {\n\taf, aok := toFloat(expected)\n\tbf, bok := toFloat(actual)\n\tif !aok || !bok {\n\t\treturn 0, fmt.Errorf(\"Parameters must be numerical\")\n\t}\n\tif math.IsNaN(af) && math.IsNaN(bf) {\n\t\treturn 0, nil\n\t}\n\tif math.IsNaN(af) {\n\t\treturn 0, errors.New(\"expected value must not be NaN\")\n\t}\n\tif af == 0 {\n\t\treturn 0, fmt.Errorf(\"expected value must have a value other than zero to calculate the relative error\")\n\t}\n\tif math.IsNaN(bf) {\n\t\treturn 0, errors.New(\"actual value must not be NaN\")\n\t}\n\n\treturn math.Abs(af-bf) / math.Abs(af), nil\n}\n\n// InEpsilon asserts that expected and actual have a relative error less than epsilon\nfunc InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool {\n\tif h, ok := t.(tHelper); ok {\n\t\th.Helper()\n\t}\n\tif math.IsNaN(epsilon) {\n\t\treturn Fail(t, \"epsilon must not be NaN\", msgAndArgs...)","sourceCodeStart":1546,"sourceCodeEnd":1582,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/assert/assertions.go#L1546-L1582","documentation":"Returned by calcRelativeError (assert/assertions.go:1563) when the expected value, after conversion via toFloat, is math.NaN() (and actual is not also NaN). A NaN expected value makes relative-error math meaningless, so testify rejects it explicitly. Surfaced through assert.InEpsilon.","triggerScenarios":"Calling assert.InEpsilon(t, math.NaN(), actual, eps) where expected evaluates to NaN — e.g. the result of 0.0/0.0 or an unchecked math operation that produced NaN.","commonSituations":"Divide-by-zero or sqrt-of-negative producing NaN that flows unchecked into a tolerance assertion; results from external computation (CSV parsing, ML model output) that occasionally yields NaN.","solutions":["Guard the expected value with math.IsNaN before calling InEpsilon and skip/fail the test explicitly.","Fix the upstream computation that produced NaN (check for divide-by-zero, invalid log/sqrt inputs).","If NaN-expected is legitimately possible, use assert.True(t, math.IsNaN(actual)) instead."],"exampleFix":"// before\nassert.InEpsilon(t, computed, want, 0.01) // computed may be NaN\n// after\nif math.IsNaN(computed) {\n    t.Fatal(\"computed is NaN\")\n}\nassert.InEpsilon(t, computed, want, 0.01)","handlingStrategy":"validation","validationCode":"ef, ok := toFloatLike(expected)\nif !ok || math.IsNaN(ef) {\n    t.Fatalf(\"expected is NaN, cannot compute relative error\")\n}\nassert.InEpsilon(t, expected, actual, eps)","typeGuard":"func isNotNaNFloat(v interface{}) bool {\n    f, ok := toFloatPublic(v) // mirror of assert.toFloat\n    return ok && !math.IsNaN(f)\n}","tryCatchPattern":"// Guard the expected value before asserting:\nif f, ok := asFloat(expected); ok && math.IsNaN(f) {\n    t.Fatalf(\"expected is NaN\")\n}\nassert.InEpsilon(t, expected, actual, eps)","preventionTips":["Sanitize all float computations for NaN before they reach assertions.","Add property checks (math.IsNaN/math.IsInf) at the boundary of numerical code.","Use a table-driven test helper that rejects NaN inputs upfront."],"tags":["assertion","numeric","nan","in-epsilon"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}