Skip to content

Commit 2c59dd2

Browse files
committed
refactor(NumberUtils, Scientific): Finalize fields and update multiplication logic
Finalized `output` and `count` fields in `Scientific` class to improve immutability. Updated multiplication logic in `NumberUtils` to use `MULTIPLY_HIGH` method consistently.
1 parent 3b365f2 commit 2c59dd2

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

core/src/main/java/com/alibaba/fastjson2/util/NumberUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ public static Scientific floatToScientific(float floatValue) {
595595

596596
if (accurate) {
597597
// If we pursue performance, we can return it here, but it may return a non-shortest sequence of numbers (the result is correct)
598-
// rawOutput = EnvUtils.JDK_AGENT_INSTANCE.multiplyHighKaratsuba(rawOutput, 0x6b5fca6af2bd215fL) >> 22; // rawOutput / 10000000;
598+
// rawOutput = MULTIPLY_HIGH.multiplyHigh(rawOutput, 0x6b5fca6af2bd215fL) >> 22; // rawOutput / 10000000;
599599
// if (adl == 7) {
600600
// --adl;
601601
// rawOutput = (rawOutput + 5) / 10; // rawOutput = rawOutput / 10 + ((rawOutput % 10) >= 5 ? 1 : 0);
@@ -604,7 +604,7 @@ public static Scientific floatToScientific(float floatValue) {
604604
}
605605

606606
if (rawOutput < 1000000000) {
607-
return new Scientific(rawOutput / 10000000, 2, e10);
607+
return new Scientific(MULTIPLY_HIGH.multiplyHigh(rawOutput, 0x6b5fca6af2bd215fL) >> 22, 2, e10); // rawOutput / 10000000
608608
}
609609
long div = MULTIPLY_HIGH.multiplyHigh(rawOutput, 0x44b82fa09b5a52ccL) >> 28; // rawOutput / 1000000000;
610610
long rem = rawOutput - div * 1000000000;

core/src/main/java/com/alibaba/fastjson2/util/Scientific.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Created by wangyc
66
*/
77
public class Scientific {
8-
public long output;
9-
public int count;
8+
public final long output;
9+
public final int count;
1010
public final int e10;
1111
public final boolean b;
1212

@@ -23,6 +23,8 @@ public Scientific(long output, int count, int e10) {
2323
public Scientific(int e10, boolean b) {
2424
this.e10 = e10;
2525
this.b = b;
26+
this.output = 0;
27+
this.count = 0;
2628
}
2729

2830
@Override

0 commit comments

Comments
 (0)