Skip to content

Commit 97c76cb

Browse files
committed
core: fixed more comments and slight bug.
The bug involves the incorrect logic for `core::num::flt2dec::decoder`. Fortunately, we have been already testing this case (the minimal normalized value) so there is no regression for this slight bug.
1 parent c58785d commit 97c76cb

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/libcore/num/flt2dec/bignum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ macro_rules! define_bignum {
216216
self.base[i] = 0;
217217
}
218218

219-
// shift by `nbits` bits
219+
// shift by `bits` bits
220220
let mut sz = self.size + digits;
221221
if bits > 0 {
222222
let last = sz;

src/libcore/num/flt2dec/decoder.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn decode<T: DecodableFloat>(v: T) -> (/*negative?*/ bool, FullDecoded) {
7373
FpCategory::Infinite => FullDecoded::Infinite,
7474
FpCategory::Zero => FullDecoded::Zero,
7575
FpCategory::Subnormal => {
76-
// (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
76+
// neighbors: (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
7777
// Float::integer_decode always preserves the exponent,
7878
// so the mantissa is scaled for subnormals.
7979
FullDecoded::Finite(Decoded { mant: mant, minus: 1, plus: 1,
@@ -82,12 +82,12 @@ pub fn decode<T: DecodableFloat>(v: T) -> (/*negative?*/ bool, FullDecoded) {
8282
FpCategory::Normal => {
8383
let minnorm = <T as DecodableFloat>::min_pos_norm_value().integer_decode();
8484
if mant == minnorm.0 && exp == minnorm.1 {
85-
// (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
85+
// neighbors: (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
8686
// where maxmant = minnormmant * 2 - 1
87-
FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 2,
88-
exp: exp - 1, inclusive: even })
87+
FullDecoded::Finite(Decoded { mant: mant << 2, minus: 1, plus: 2,
88+
exp: exp - 2, inclusive: even })
8989
} else {
90-
// (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
90+
// neighbors: (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
9191
FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 1,
9292
exp: exp - 1, inclusive: even })
9393
}

src/libcore/num/flt2dec/estimator.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
pub fn estimate_scaling_factor(mant: u64, exp: i16) -> i16 {
1919
// 2^(nbits-1) < mant <= 2^nbits if mant > 0
2020
let nbits = 64 - (mant - 1).leading_zeros() as i64;
21+
// 1292913986 = floor(2^32 * log_10 2)
22+
// therefore this always underestimates (or is exact), but not much.
2123
(((nbits + exp as i64) * 1292913986) >> 32) as i16
2224
}
2325

0 commit comments

Comments
 (0)