Skip to content

Commit 4444a23

Browse files
committed
1 parent 2d4ef16 commit 4444a23

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ numbers to decimal strings.
1111
This Rust implementation is a line-by-line port of Victor Zverovich's
1212
implementation in C++, [https://github.com/vitaut/zmij][upstream].
1313

14-
[upstream]: https://github.com/vitaut/zmij/tree/df35b9c2f62c02c4e2e3437c4d6d9b29bbb73331
14+
[upstream]: https://github.com/vitaut/zmij/tree/f087a34e9342e1ab2049f125ff09556d24b04cdf
1515

1616
## Example
1717

src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ where
862862
// Converts a binary FP number bin_sig * 2**bin_exp to the shortest decimal
863863
// representation, where bin_exp = raw_exp - exp_offset.
864864
#[cfg_attr(feature = "no-panic", no_panic)]
865-
fn to_decimal_normal<Float, UInt>(bin_sig: UInt, raw_exp: i64, regular: bool) -> ToDecimalResult
865+
fn to_decimal_fast<Float, UInt>(bin_sig: UInt, raw_exp: i64, regular: bool) -> ToDecimalResult
866866
where
867867
Float: FloatTraits,
868868
UInt: traits::UInt,
@@ -1000,6 +1000,11 @@ where
10001000
buffer = unsafe { buffer.add(usize::from(Float::is_negative(bits))) };
10011001

10021002
let mut dec;
1003+
let threshold = if Float::NUM_BITS == 64 {
1004+
10_000_000_000_000_000
1005+
} else {
1006+
100_000_000
1007+
};
10031008
if bin_exp == 0 {
10041009
if bin_sig == Float::SigType::from(0) {
10051010
return unsafe {
@@ -1010,13 +1015,7 @@ where
10101015
};
10111016
}
10121017
dec = to_decimal_schubfach(bin_sig, i64::from(1 - Float::EXP_OFFSET), true);
1013-
while dec.sig
1014-
< if Float::NUM_BITS == 64 {
1015-
10_000_000_000_000_000
1016-
} else {
1017-
100_000_000
1018-
}
1019-
{
1018+
while dec.sig < threshold {
10201019
dec.sig *= 10;
10211020
dec.exp -= 1;
10221021
}
@@ -1025,19 +1024,14 @@ where
10251024
dec.sig_div10 = dec.sig / 10;
10261025
}
10271026
} else {
1028-
dec = to_decimal_normal::<Float, Float::SigType>(
1027+
dec = to_decimal_fast::<Float, Float::SigType>(
10291028
bin_sig | Float::IMPLICIT_BIT,
10301029
bin_exp,
10311030
bin_sig != Float::SigType::from(0),
10321031
);
10331032
}
10341033
let mut dec_exp = dec.exp;
1035-
let extra_digit = dec.sig
1036-
>= if Float::NUM_BITS == 64 {
1037-
10_000_000_000_000_000
1038-
} else {
1039-
100_000_000
1040-
};
1034+
let extra_digit = dec.sig >= threshold;
10411035
dec_exp += Float::MAX_DIGITS10 as i32 - 2 + i32::from(extra_digit);
10421036
if Float::NUM_BITS == 32 && dec.sig < 10_000_000 {
10431037
dec.sig *= 10;

0 commit comments

Comments
 (0)