fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

FastFloat's parsing of scientific notation strings for integers with specific digit counts can lead to precision loss

Open forezp opened this issue 8 months ago • 0 comments

hi, FastFloat's parsing of scientific notation strings for integers with specific digit counts can lead to precision loss. For example, when parsing '2.0250405e+07', the resulting value is 2.0250404999999996e+07.

Below are test cases:

func TestParse(t *testing.T) {
	s := "2.0250405e+07" //20250405
	t.Log("raw:", s)
	parsed, err := Parse(s)
	if err != nil {
		t.Fatal(err)
	}
	t.Log("Parse:", parsed)
	parsed = ParseBestEffort(s)
	t.Log("ParseBestEffort:", parsed)
	parsed, err = strconv.ParseFloat(s, 64)
	if err != nil {
		t.Fatal(err)
	}
	t.Log("strconv.ParseFloat", parsed)
}

output:

=== RUN   TestParse
    parse_test.go:12: raw: 2.0250405e+07
    parse_test.go:17: Parse: 2.0250404999999996e+07
    parse_test.go:19: ParseBestEffort: 2.0250404999999996e+07
    parse_test.go:24: strconv.ParseFloat 2.0250405e+07
--- PASS: TestParse (0.00s)
PASS

forezp avatar Apr 25 '25 08:04 forezp