From 866f72a4379959e8fb5e7c9adaca2f519ffcaade Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sat, 10 May 2025 11:31:33 +0200 Subject: [PATCH] Favor decimal notation over scientific notation for floats e.g. ``` JSON.dump(1746861937.7842371) ``` master: ``` "1.7468619377842371e+9" ``` This branch and older json versions: ``` 1746861937.7842371 ``` In the end it's shorter, and according to `canada.json` benchmark performance is the same. --- ext/json/ext/vendor/fpconv.c | 2 +- test/json/json_generator_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ext/json/ext/vendor/fpconv.c b/ext/json/ext/vendor/fpconv.c index 854cae288..440759e5a 100644 --- a/ext/json/ext/vendor/fpconv.c +++ b/ext/json/ext/vendor/fpconv.c @@ -340,7 +340,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg) } /* write decimal w/o scientific notation */ - if(K < 0 && (K > -7 || exp < 4)) { + if(K < 0 && (K > -7 || exp < 10)) { int offset = ndigits - absv(K); /* fp < 1.0 -> write leading zero */ if(offset <= 0) { diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 3192b555a..55a3065ae 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -771,6 +771,14 @@ def test_json_generate_float values = [-1.0, 1.0, 0.0, 12.2, 7.5 / 3.2, 12.0, 100.0, 1000.0] expecteds = ["-1.0", "1.0", "0.0", "12.2", "2.34375", "12.0", "100.0", "1000.0"] + if RUBY_ENGINE == "jruby" + values << 1746861937.7842371 + expecteds << "1.7468619377842371E9" + else + values << 1746861937.7842371 + expecteds << "1746861937.7842371" + end + values.zip(expecteds).each do |value, expected| assert_equal expected, value.to_json end