Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/pretty/to_fixed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@ pub unsafe fn format64_to_fixed(f: f64, fraction_digits: u8, result: *mut u8) ->

let mut round_up = false;

for i in 0..blocks {
result.append_bytes(b'0', 9 * min_block as usize);
for i in min_block as u32..blocks {
let p: isize = POW10_OFFSET_2[idx] as isize + i as isize - min_block as isize;
debug_assert!(p >= 0);
let p = p as usize;
Expand Down
33 changes: 28 additions & 5 deletions tests/to_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,23 +340,27 @@ fn test_max_exponent_boundry_and_full_mantissa() {
);
}

const MIN_EXPONENT: u16 = 0b010_1001_0011;
const MIN_EXPONENT_ZERO_MANTISSA: u16 = 0b010_1011_0001;
const MIN_EXPONENT_FULL_MANTISSA: u16 = 0b010_1011_0000;

#[test]
fn test_min_exponent_boundry_zero_mantissa() {
let m = 0;

let expected = "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

for exponent in 0..=MIN_EXPONENT {
for exponent in 0..=MIN_EXPONENT_ZERO_MANTISSA {
assert_eq!(
pretty_to_fixed(f64_from_parts(false, exponent, m), 100),
expected
);
}

assert_ne!(
pretty_to_fixed(f64_from_parts(false, MIN_EXPONENT + 1, m), 100),
pretty_to_fixed(
f64_from_parts(false, MIN_EXPONENT_ZERO_MANTISSA + 1, m),
100
),
expected
);
}
Expand All @@ -367,19 +371,38 @@ fn test_min_exponent_boundry_full_mantissa() {

let expected = "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

for exponent in 0..=MIN_EXPONENT {
for exponent in 0..=MIN_EXPONENT_FULL_MANTISSA {
assert_eq!(
pretty_to_fixed(f64_from_parts(false, exponent, m), 100),
expected
);
}

assert_ne!(
pretty_to_fixed(f64_from_parts(false, MIN_EXPONENT + 1, m), 100),
pretty_to_fixed(
f64_from_parts(false, MIN_EXPONENT_FULL_MANTISSA + 1, m),
100
),
expected
);
}

#[test]
fn test_leading_zero_blocks() {
assert_eq!(
pretty_to_fixed(1e-17, 40),
"0.0000000000000000100000000000000007154242"
);
assert_eq!(
pretty_to_fixed(3.386680864129311e-41, 96),
"0.000000000000000000000000000000000000000033866808641293112412157561681710000260256078959410848719"
);
assert_eq!(
pretty_to_fixed(2.0717877721678957e-18, 59),
"0.00000000000000000207178777216789569372363572982679877745972"
);
}

// Regression test for https://github.com/boa-dev/ryu-js/issues/55
// Rounding carry that propagates through all integer digits must not
// read out-of-bounds before the buffer start.
Expand Down
Loading