19
19
/// reference:
20
20
/// - <https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html>
21
21
/// - <https://nigeltao.github.io/blog/2020/eisel-lemire.html>
22
+ /// @alert deprecated "Decimal will be changed to private. Use `@strconv.parse_double` instead"
22
23
struct Decimal {
23
24
digits : FixedArray [Byte ]
24
25
mut digits_num : Int
@@ -41,7 +42,13 @@ let powtab = [
41
42
42
43
///|
43
44
/// Create a zero decimal.
45
+ /// @alert deprecated "Decimal will be changed to private. Use `@strconv.parse_double` instead"
44
46
pub fn Decimal ::new () -> Decimal {
47
+ Decimal ::new_priv ()
48
+ }
49
+
50
+ ///|
51
+ fn Decimal ::new_priv () -> Decimal {
45
52
{
46
53
digits : FixedArray ::make (800 , Byte ::default ()),
47
54
digits_num : 0 ,
@@ -53,8 +60,14 @@ pub fn Decimal::new() -> Decimal {
53
60
54
61
///|
55
62
/// Create a decimal with an Int64 value.
63
+ /// @alert deprecated "Decimal will be changed to private. Use `@strconv.parse_double` instead"
56
64
pub fn Decimal ::from_int64 (v : Int64 ) -> Decimal {
57
- Decimal ::new ()..assign (v )
65
+ Decimal ::from_int64_priv (v )
66
+ }
67
+
68
+ ///|
69
+ fn Decimal ::from_int64_priv (v : Int64 ) -> Decimal {
70
+ Decimal ::new_priv ()..assign (v )
58
71
}
59
72
60
73
///|
@@ -78,13 +91,19 @@ pub fn Decimal::from_int64(v : Int64) -> Decimal {
78
91
///
79
92
/// An exponent value exp scales the mantissa (significand) by 10^exp.
80
93
/// For example, "1.23e2" represents 1.23 × 10² = 123.
94
+ /// @alert deprecated "use `@strconv.parse_double` instead"
81
95
pub fn parse_decimal (str : String ) -> Decimal !StrConvError {
82
96
parse_decimal_from_view! (str [:])
83
97
}
84
98
99
+ ///|
100
+ fn parse_decimal_priv (str : String ) -> Decimal !StrConvError {
101
+ parse_decimal_from_view! (str [:])
102
+ }
103
+
85
104
///|
86
105
fn parse_decimal_from_view (str : @string .StringView ) -> Decimal !StrConvError {
87
- let d = Decimal ::new ()
106
+ let d = Decimal ::new_priv ()
88
107
let mut has_dp = false
89
108
let mut has_digits = false
90
109
// read sign
@@ -158,14 +177,20 @@ fn parse_decimal_from_view(str : @string.StringView) -> Decimal!StrConvError {
158
177
d ..trim ()
159
178
}
160
179
161
- ///| @alert deprecated "use `@strconv.parse_decimal ` instead"
180
+ ///| @alert deprecated "use `@strconv.parse_double ` instead"
162
181
pub fn Decimal ::parse_decimal (str : String ) -> Decimal !StrConvError {
163
- parse_decimal ! (str )
182
+ parse_decimal_from_view ! (str [:] )
164
183
}
165
184
166
185
///|
167
186
/// Convert the decimal to Double.
187
+ ///| @alert deprecated "use `@strconv.parse_double` instead to avoid using this method."
168
188
pub fn to_double (self : Decimal ) -> Double !StrConvError {
189
+ self .to_double_priv! ()
190
+ }
191
+
192
+ ///|
193
+ fn to_double_priv (self : Decimal ) -> Double !StrConvError {
169
194
let mut exponent = 0
170
195
let mut mantissa = 0L
171
196
// check the underflow and overflow
@@ -191,7 +216,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
191
216
} else {
192
217
n = powtab [self .decimal_point]
193
218
}
194
- self .shift (- n )
219
+ self .shift_priv (- n )
195
220
exponent + = n
196
221
}
197
222
// left shift
@@ -203,7 +228,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
203
228
} else {
204
229
n = powtab [- self .decimal_point]
205
230
}
206
- self .shift (n )
231
+ self .shift_priv (n )
207
232
exponent - = n
208
233
}
209
234
@@ -215,7 +240,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
215
240
// if the exponent is smaller, move it up and shift decimal accordingly
216
241
if exponent < double_info .bias + 1 {
217
242
let n = double_info .bias + 1 - exponent
218
- self .shift (- n )
243
+ self .shift_priv (- n )
219
244
exponent + = n
220
245
}
221
246
if exponent - double_info .bias >= (1 << double_info .exponent_bits) - 1 {
@@ -225,7 +250,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
225
250
226
251
// multiply by (2 ** precision) and round to get mantissa
227
252
// extract mantissa_bits + 1 bits
228
- self .shift (double_info .mantissa_bits + 1 )
253
+ self .shift_priv (double_info .mantissa_bits + 1 )
229
254
mantissa = self .rounded_integer ()
230
255
231
256
// rounding might have added a bit, shift down.
@@ -251,7 +276,13 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
251
276
///|
252
277
/// Binary shift left (s > 0) or right (s < 0).
253
278
/// The shift count must not larger than the max_shift to avoid overflow.
279
+ /// @alert deprecated "use `@strconv.parse_double` instead to avoid using this method."
254
280
pub fn shift (self : Decimal , s : Int ) -> Unit {
281
+ self .shift_priv (s )
282
+ }
283
+
284
+ ///|
285
+ fn shift_priv (self : Decimal , s : Int ) -> Unit {
255
286
if self .digits_num == 0 {
256
287
return
257
288
}
@@ -601,18 +632,8 @@ fn Decimal::to_string(self : Decimal) -> String {
601
632
Show ::to_string (self )
602
633
}
603
634
604
- test "from_int64" {
605
- let d = Decimal ::from_int64 (- 1 )
606
- inspect! (d .to_string (), content = "0" )
607
- }
608
-
609
- test "to_string" {
610
- let d = parse_decimal! ("-1" )
611
- inspect! (d .to_string (), content = "1" )
612
- }
613
-
614
635
test "new" {
615
- let hpd = Decimal ::from_int64 (1L )
636
+ let hpd = Decimal ::from_int64_priv (1L )
616
637
assert_eq! (hpd .digits.length (), 800 )
617
638
assert_eq! (hpd .digits_num, 1 )
618
639
assert_eq! (hpd .decimal_point, 1 )
@@ -621,28 +642,28 @@ test "new" {
621
642
}
622
643
623
644
test "from_int64" {
624
- let hpd = Decimal ::from_int64 (123456789L )
645
+ let hpd = Decimal ::from_int64_priv (123456789L )
625
646
assert_eq! (hpd .to_string (), "123456789" )
626
647
}
627
648
628
649
test "parse_decimal" {
629
650
let s = "0.0000000000000000000000000000007888609052210118054117285652827862296732064351090230047702789306640625"
630
651
let hpd = try {
631
- parse_decimal ! (s )
652
+ parse_decimal_priv ! (s )
632
653
} catch {
633
654
_ => panic ()
634
655
}
635
656
assert_eq! (hpd .to_string (), s )
636
657
let hpd = try {
637
- parse_decimal ! ("1.0e-10" )
658
+ parse_decimal_priv ! ("1.0e-10" )
638
659
} catch {
639
660
_ => panic ()
640
661
}
641
662
assert_eq! (hpd .to_string (), "0.0000000001" )
642
663
}
643
664
644
665
test "to_string" {
645
- let hpd = Decimal ::from_int64 (123456789L )
666
+ let hpd = Decimal ::from_int64_priv (123456789L )
646
667
hpd .decimal_point = 1
647
668
assert_eq! (hpd .to_string (), "1.23456789" )
648
669
hpd .decimal_point = 0
@@ -668,24 +689,24 @@ test "shift" {
668
689
]
669
690
for i = 0 ; i < tests .length (); i = i + 1 {
670
691
let t = tests [i ]
671
- let d = Decimal ::from_int64 (t .0 )
672
- d .shift (t .1 )
692
+ let d = Decimal ::from_int64_priv (t .0 )
693
+ d .shift_priv (t .1 )
673
694
assert_eq! (d .to_string (), t .2 )
674
695
}
675
696
}
676
697
677
698
test "parse decimal with underscore" {
678
- inspect! (parse_decimal ! ("1e1_2" ), content = "1000000000000" )
679
- inspect! (parse_decimal ! ("1e12" ), content = "1000000000000" )
680
- inspect! (parse_decimal ! ("1_2_3" ), content = "123" )
699
+ inspect! (parse_decimal_priv ! ("1e1_2" ), content = "1000000000000" )
700
+ inspect! (parse_decimal_priv ! ("1e12" ), content = "1000000000000" )
701
+ inspect! (parse_decimal_priv ! ("1_2_3" ), content = "123" )
681
702
}
682
703
683
704
test "parse decimal error" {
684
- inspect! (parse_decimal ? ("1e" ), content = "Err(invalid syntax)" )
685
- inspect! (parse_decimal ? ("1e+" ), content = "Err(invalid syntax)" )
686
- inspect! (parse_decimal ? ("1e_" ), content = "Err(invalid syntax)" )
687
- inspect! (parse_decimal ? ("1-23" ), content = "Err(invalid syntax)" )
688
- inspect! (parse_decimal ? ("1.2.3" ), content = "Err(invalid syntax)" )
705
+ inspect! (parse_decimal_priv ? ("1e" ), content = "Err(invalid syntax)" )
706
+ inspect! (parse_decimal_priv ? ("1e+" ), content = "Err(invalid syntax)" )
707
+ inspect! (parse_decimal_priv ? ("1e_" ), content = "Err(invalid syntax)" )
708
+ inspect! (parse_decimal_priv ? ("1-23" ), content = "Err(invalid syntax)" )
709
+ inspect! (parse_decimal_priv ? ("1.2.3" ), content = "Err(invalid syntax)" )
689
710
}
690
711
691
712
test "parse decimal with large numbers" {
@@ -694,16 +715,16 @@ test "parse decimal with large numbers" {
694
715
s + = "9"
695
716
}
696
717
inspect! (
697
- parse_decimal ! (s ),
718
+ parse_decimal_priv ! (s ),
698
719
content = "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" ,
699
720
)
700
721
}
701
722
702
723
test "should_round_up when truncated and half-way" {
703
724
// Create a decimal from "12.50"
704
- let decimal = parse_decimal ! ("12.50" )
725
+ let decimal = parse_decimal_priv ! ("12.50" )
705
726
// Convert to double
706
- inspect! (decimal .to_double ! (), content = "12.5" )
727
+ inspect! (decimal .to_double_priv ! (), content = "12.5" )
707
728
}
708
729
709
730
test "parse_decimal with large numbers and truncation" {
@@ -713,13 +734,13 @@ test "parse_decimal with large numbers and truncation" {
713
734
s + = "0"
714
735
}
715
736
inspect! (
716
- parse_decimal ! (s ),
737
+ parse_decimal_priv ! (s ),
717
738
content = "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
718
739
)
719
740
}
720
741
721
742
test "corner cases" {
722
- inspect! (parse_decimal ? (".123" ), content = "Ok(0.123)" )
723
- inspect! (parse_decimal ? ("." ), content = "Err(invalid syntax)" )
724
- inspect! (parse_decimal ? ("-" ), content = "Err(invalid syntax)" )
743
+ inspect! (parse_decimal_priv ? (".123" ), content = "Ok(0.123)" )
744
+ inspect! (parse_decimal_priv ? ("." ), content = "Err(invalid syntax)" )
745
+ inspect! (parse_decimal_priv ? ("-" ), content = "Err(invalid syntax)" )
725
746
}
0 commit comments