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

Function closeEnough

property_test.go:1101–1120  ·  view source on GitHub ↗

closeEnough reports whether two floats agree to within a relative tolerance suitable for decimal round-trip comparisons.

(a, b float64)

Source from the content-addressed store, hash-verified

1099// closeEnough reports whether two floats agree to within a relative tolerance
1100// suitable for decimal round-trip comparisons.
1101func closeEnough(a, b float64) bool {
1102 if a == b {
1103 return true
1104 }
1105 const rel = 1e-15
1106 diff := a - b
1107 if diff < 0 {
1108 diff = -diff
1109 }
1110 if a < 0 {
1111 a = -a
1112 }
1113 if b < 0 {
1114 b = -b
1115 }
1116 if a < b {
1117 a, b = b, a
1118 }
1119 return diff/a < rel
1120}

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected