Skip to content

Commit 97ea7c1

Browse files
committed
core: fixed a slight bug.
The bug involves the incorrect logic for `core::num::flt2dec::decoder`. This makes some numbers in the form of 2^n missing one final digits, which breaks the bijectivity criterion. The regression tests have been added, and f32 exhaustive test is rerun to get the updated result.
1 parent 8a195f0 commit 97ea7c1

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/libcore/num/flt2dec/decoder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ pub fn decode<T: DecodableFloat>(v: T) -> (/*negative?*/ bool, FullDecoded) {
7575
FpCategory::Infinite => FullDecoded::Infinite,
7676
FpCategory::Zero => FullDecoded::Zero,
7777
FpCategory::Subnormal => {
78-
// (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
78+
// neighbors: (mant - 2, exp) -- (mant, exp) -- (mant + 2, exp)
7979
// Float::integer_decode always preserves the exponent,
8080
// so the mantissa is scaled for subnormals.
8181
FullDecoded::Finite(Decoded { mant: mant, minus: 1, plus: 1,
8282
exp: exp, inclusive: even })
8383
}
8484
FpCategory::Normal => {
8585
let minnorm = <T as DecodableFloat>::min_pos_norm_value().integer_decode();
86-
if mant == minnorm.0 && exp == minnorm.1 {
87-
// (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
86+
if mant == minnorm.0 {
87+
// neighbors: (maxmant, exp - 1) -- (minnormmant, exp) -- (minnormmant + 1, exp)
8888
// where maxmant = minnormmant * 2 - 1
89-
FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 2,
90-
exp: exp - 1, inclusive: even })
89+
FullDecoded::Finite(Decoded { mant: mant << 2, minus: 1, plus: 2,
90+
exp: exp - 2, inclusive: even })
9191
} else {
92-
// (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
92+
// neighbors: (mant - 1, exp) -- (mant, exp) -- (mant + 1, exp)
9393
FullDecoded::Finite(Decoded { mant: mant << 1, minus: 1, plus: 1,
9494
exp: exp - 1, inclusive: even })
9595
}

src/libcoretest/num/flt2dec/mod.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ pub fn f32_shortest_sanity_test<F>(mut f: F) where F: FnMut(&Decoded, &mut [u8])
216216
// 10^18 * 0.314159231156617216
217217
check_shortest!(f(3.141592e17f32) => b"3141592", 18);
218218

219+
// regression test for decoders
220+
// 10^8 * 0.3355443
221+
// 10^8 * 0.33554432
222+
// 10^8 * 0.33554436
223+
let twoto25: f32 = StdFloat::ldexp(1.0, 25);
224+
check_shortest!(f(twoto25) => b"33554432", 8);
225+
219226
// 10^39 * 0.340282326356119256160033759537265639424
220227
// 10^39 * 0.34028234663852885981170418348451692544
221228
// 10^39 * 0.340282366920938463463374607431768211456
@@ -308,6 +315,13 @@ pub fn f64_shortest_sanity_test<F>(mut f: F) where F: FnMut(&Decoded, &mut [u8])
308315
// 10^18 * 0.314159200000000064
309316
check_shortest!(f(3.141592e17f64) => b"3141592", 18);
310317

318+
// regression test for decoders
319+
// 10^20 * 0.18446744073709549568
320+
// 10^20 * 0.18446744073709551616
321+
// 10^20 * 0.18446744073709555712
322+
let twoto64: f64 = StdFloat::ldexp(1.0, 64);
323+
check_shortest!(f(twoto64) => b"18446744073709552", 20);
324+
311325
// pathological case: high = 10^23 (exact). tie breaking should always prefer that.
312326
// 10^24 * 0.099999999999999974834176
313327
// 10^24 * 0.099999999999999991611392
@@ -492,15 +506,15 @@ pub fn f32_exhaustive_equivalence_test<F, G>(f: F, g: G, k: usize)
492506
// so why not simply testing all of them?
493507
//
494508
// this is of course very stressful (and thus should be behind an `#[ignore]` attribute),
495-
// but with `-O3 -C lto` this only takes about two hours or so.
509+
// but with `-C opt-level=3 -C lto` this only takes about an hour or so.
496510

497511
// iterate from 0x0000_0001 to 0x7f7f_ffff, i.e. all finite ranges
498512
let (npassed, nignored) = iterate("f32_exhaustive_equivalence_test",
499513
k, 0x7f7f_ffff, f, g, |i: usize| {
500514
let x: f32 = unsafe {mem::transmute(i as u32 + 1)};
501515
decode_finite(x)
502516
});
503-
assert_eq!((npassed, nignored), (2121451879, 17643160));
517+
assert_eq!((npassed, nignored), (2121451881, 17643158));
504518
}
505519

506520
fn to_string_with_parts<F>(mut f: F) -> String

src/libcoretest/num/flt2dec/strategy/grisu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn shortest_f32_exhaustive_equivalence_test() {
6060
//
6161
// this reports the progress and the number of f32 values returned `None`.
6262
// with `--nocapture` (and plenty of time and appropriate rustc flags), this should print:
63-
// `done, ignored=17643160 passed=2121451879 failed=0`.
63+
// `done, ignored=17643158 passed=2121451881 failed=0`.
6464

6565
use core::num::flt2dec::strategy::dragon::format_shortest as fallback;
6666
f32_exhaustive_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS);

0 commit comments

Comments
 (0)