Skip to content

Commit 5bb83d8

Browse files
authored
Replace the NumberToString implementation
Replace the NumberToString implementation with the Schubfach algorithm. This is a relatively new algorithm used in many standard libraries including Java. We use code by Rafaello Guilietti that has also been adapted for Jackson and for the JDK.
1 parent 7418234 commit 5bb83d8

14 files changed

Lines changed: 101621 additions & 1072 deletions

File tree

NOTICE.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,29 @@ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3232
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3333
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3434
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36+
37+
----
38+
39+
Portions of the number-to-string code are subject to the following:
40+
41+
Copyright 2018-2020 Raffaello Giulietti
42+
43+
Permission is hereby granted, free of charge, to any person obtaining a copy
44+
of this software and associated documentation files (the "Software"), to deal
45+
in the Software without restriction, including without limitation the rights
46+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
47+
copies of the Software, and to permit persons to whom the Software is
48+
furnished to do so, subject to the following conditions:
49+
50+
The above copyright notice and this permission notice shall be included in
51+
all copies or substantial portions of the Software.
52+
53+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
59+
THE SOFTWARE.
60+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.mozilla.javascript.benchmarks;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import org.mozilla.javascript.ScriptRuntime;
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.OutputTimeUnit;
7+
8+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
9+
public class NumberFormatBenchmark {
10+
11+
private static final double DENORMAL = 4.47118444E-314;
12+
13+
@Benchmark
14+
public Object scriptRuntimePi() {
15+
return ScriptRuntime.toString(Math.PI);
16+
}
17+
18+
@Benchmark
19+
public Object scriptRuntimeOne() {
20+
return ScriptRuntime.toString(1.0);
21+
}
22+
23+
@Benchmark
24+
public Object scriptRuntimeDenormal() {
25+
return ScriptRuntime.toString(DENORMAL);
26+
}
27+
28+
@Benchmark
29+
public Object scriptRuntimeSmall() {
30+
return ScriptRuntime.toString(0.00001);
31+
}
32+
33+
@Benchmark
34+
public Object scriptRuntimeBig() {
35+
return ScriptRuntime.toString(314000000000000000000.0);
36+
}
37+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
assertEquals('1', (1).toString());
2+
assertEquals('3.141592653589793', (Math.PI).toString());
3+
assertEquals('3.1416', (Math.PI).toFixed(4));
4+
assertEquals('3.142', (Math.PI).toPrecision(4));
5+
'success';

rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.function.BiConsumer;
2424
import java.util.function.Consumer;
2525
import org.mozilla.javascript.ast.FunctionNode;
26+
import org.mozilla.javascript.dtoa.DoubleFormatter;
2627
import org.mozilla.javascript.lc.type.TypeInfo;
2728
import org.mozilla.javascript.lc.type.impl.factory.ConcurrentFactory;
2829
import org.mozilla.javascript.typedarrays.NativeArrayBuffer;
@@ -39,7 +40,6 @@
3940
import org.mozilla.javascript.typedarrays.NativeUint8Array;
4041
import org.mozilla.javascript.typedarrays.NativeUint8ClampedArray;
4142
import org.mozilla.javascript.v8dtoa.DoubleConversion;
42-
import org.mozilla.javascript.v8dtoa.FastDtoa;
4343
import org.mozilla.javascript.xml.XMLLib;
4444
import org.mozilla.javascript.xml.XMLLoader;
4545
import org.mozilla.javascript.xml.XMLObject;
@@ -1051,27 +1051,20 @@ public static String toString(double val) {
10511051
}
10521052

10531053
public static String numberToString(double d, int base) {
1054+
if (base == 10) {
1055+
// Common case: DoubleFormatter efficiently identifies non-finite
1056+
// numbers. Do this before other checks.
1057+
return DoubleFormatter.toString(d);
1058+
}
1059+
10541060
if ((base < 2) || (base > 36)) {
10551061
throw ScriptRuntime.rangeErrorById("msg.bad.radix", Integer.toString(base));
10561062
}
1057-
10581063
if (Double.isNaN(d)) return "NaN";
10591064
if (d == Double.POSITIVE_INFINITY) return "Infinity";
10601065
if (d == Double.NEGATIVE_INFINITY) return "-Infinity";
10611066
if (d == 0.0) return "0";
1062-
1063-
if (base != 10) {
1064-
return DToA.JS_dtobasestr(base, d);
1065-
}
1066-
// V8 FastDtoa can't convert all numbers, so try it first but
1067-
// fall back to old DToA in case it fails
1068-
String result = FastDtoa.numberToString(d);
1069-
if (result != null) {
1070-
return result;
1071-
}
1072-
StringBuilder buffer = new StringBuilder();
1073-
DToA.JS_dtostr(buffer, DToA.DTOSTR_STANDARD, 0, d);
1074-
return buffer.toString();
1067+
return DToA.JS_dtobasestr(base, d);
10751068
}
10761069

10771070
public static String bigIntToString(BigInteger n, int base) {

0 commit comments

Comments
 (0)