MCPcopy Create free account
hub / github.com/buger/jsonparser / closeEnoughFloat

Function closeEnoughFloat

reference_oracle_test.go:935–968  ·  view source on GitHub ↗

closeEnoughFloat reports whether two float64 values agree to within a relative tolerance of ~4 ULP — used only for adversarial inputs where rounding modes may cause tiny divergences. The property-based loop above uses exact equality first; this is only the fallback.

(a, b float64)

Source from the content-addressed store, hash-verified

933// rounding modes may cause tiny divergences. The property-based loop above
934// uses exact equality first; this is only the fallback.
935func closeEnoughFloat(a, b float64) bool {
936 if a == b {
937 return true
938 }
939 const ulp = 4
940 if a == 0 || b == 0 {
941 // Near zero, compare absolute difference.
942 d := a - b
943 if d < 0 {
944 d = -d
945 }
946 return d < 1e-300
947 }
948 // Relative tolerance comparison.
949 diff := a - b
950 if diff < 0 {
951 diff = -diff
952 }
953 mag := a
954 if mag < 0 {
955 mag = -mag
956 }
957 mag2 := b
958 if mag2 < 0 {
959 mag2 = -mag2
960 }
961 if mag2 > mag {
962 mag = mag2
963 }
964 if mag == 0 {
965 return diff < 1e-300
966 }
967 return diff/mag < 1e-15*float64(ulp)
968}

Callers 1

TestOracleParseFloatFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected