Git Product home page Git Product logo

Comments (1)

mattburman avatar mattburman commented on June 18, 2024

I can get the new test case to pass.

diff --git a/fastfloat/parse.go b/fastfloat/parse.go
index b37838d..b8a0bbe 100644
--- a/fastfloat/parse.go
+++ b/fastfloat/parse.go
@@ -500,6 +500,15 @@ func Parse(s string) (float64, error) {
 		if expMinus {
 			exp = -exp
 		}
+		if d != uint64(f) {
+			// TODO comment.
+			// Fall back to standard parsing.
+			f, err := strconv.ParseFloat(s, 64)
+			if err != nil && !math.IsInf(f, 0) {
+				return 0, fmt.Errorf("cannot parse exponent in %q: %s", s, err)
+			}
+			return f, nil
+		}
 		f *= math.Pow10(int(exp))
 		if i >= uint(len(s)) {
 			if minus {
diff --git a/fastfloat/parse_test.go b/fastfloat/parse_test.go
index e4dfe2f..e06912a 100644
--- a/fastfloat/parse_test.go
+++ b/fastfloat/parse_test.go
@@ -445,6 +445,7 @@ func TestParseSuccess(t *testing.T) {
 	f("-123e456", math.Inf(-1)) // too big exponent
 	f("1e4", 1e4)
 	f("-1E-10", -1e-10)
+	f("8.54E-4", 8.54e-4)
 
 	// Fractional + exponent part
 	f("0.123e4", 0.123e4)

However, it does seem to go down the fallback route for some of the other test cases, which is not ideal and perhaps adversely affecting perf.

cc @valyala - would you be able to address this case? Or, let me know if my proposed fix is good enough and I can raise it as a PR. I believe a better option may exist but I'm not seeing it.

So far I've found mulitple apis I use which indeed respond with JSON number values like 8.54E-4. I would have to use MarshalTo to then parse it with strconv to get around it.

from fastjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.