{"id":"9d07340ced90b494","repo":"stretchr/testify","slug":"actual-value-must-not-be-nan","errorCode":null,"errorMessage":"actual value must not be NaN","messagePattern":"actual value must not be NaN","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"assert/assertions.go","lineNumber":1570,"sourceCode":"}\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...)\n\t}\n\tactualEpsilon, err := calcRelativeError(expected, actual)\n\tif err != nil {\n\t\treturn Fail(t, err.Error(), msgAndArgs...)\n\t}\n\tif math.IsNaN(actualEpsilon) {","sourceCodeStart":1552,"sourceCodeEnd":1588,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/assert/assertions.go#L1552-L1588","documentation":"Returned by calcRelativeError (assert/assertions.go:1569) when the actual value converts to math.NaN() while expected is a valid (non-NaN, non-zero) number. A NaN actual makes the relative error meaningless, so testify rejects it. Surfaced through assert.InEpsilon.","triggerScenarios":"Calling assert.InEpsilon(t, expected, math.NaN(), eps) where actual is NaN — typically the output of a buggy computation (0/0, invalid sqrt, unchecked float parse).","commonSituations":"Production code under test produces NaN from an unchecked arithmetic path (divide by zero, log of negative), and that value flows into a tolerance assertion. Common in numerical/financial/ML test suites.","solutions":["Add a math.IsNaN guard on the actual value before asserting and fail with a descriptive message.","Fix the upstream code path that produced NaN (validate inputs, guard divisions).","If NaN actual is expected behavior, assert it explicitly: assert.True(t, math.IsNaN(actual))."],"exampleFix":"// before\nassert.InEpsilon(t, want, result, 0.01) // result may be NaN\n// after\nif math.IsNaN(result) {\n    t.Fatalf(\"result is NaN, want %v\", want)\n}\nassert.InEpsilon(t, want, result, 0.01)","handlingStrategy":"validation","validationCode":"if f, ok := asFloat(actual); ok && math.IsNaN(f) {\n    t.Fatalf(\"actual is NaN; cannot compute relative error\")\n}\nassert.InEpsilon(t, expected, actual, eps)","typeGuard":"func actualIsNotNaN(v interface{}) bool {\n    f, ok := asFloat(v)\n    return ok && !math.IsNaN(f)\n}","tryCatchPattern":"// Guard the actual value before asserting:\nif f, ok := asFloat(actual); ok && math.IsNaN(f) {\n    t.Fatalf(\"actual is NaN\")\n}\nassert.InEpsilon(t, expected, actual, eps)","preventionTips":["Add IsNaN checks at the output boundary of numerical functions.","Write property tests that verify no NaN can leave your computation.","Log/abort on NaN in production code paths under test."],"tags":["assertion","numeric","nan","in-epsilon"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}