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