| 1 |
#![allow(clippy::float_cmp, clippy::unreadable_literal)] |
| 2 |
|
| 3 |
fn dtoa(value: f64) -> String { |
| 4 |
zmij::Buffer::new().format(value).to_owned() |
| 5 |
} |
| 6 |
|
| 7 |
fn ftoa(value: f32) -> String { |
| 8 |
zmij::Buffer::new().format(value).to_owned() |
| 9 |
} |
| 10 |
|
| 11 |
mod dtoa_test { |
| 12 |
use super::dtoa; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn normal() { |
| 16 |
assert_eq!(dtoa(6.62607015e-34), "6.62607015e-34"); |
| 17 |
|
| 18 |
// Exact half-ulp tie when rounding to nearest integer. |
| 19 |
assert_eq!(dtoa(5.444310685350916e+14), "544431068535091.6"); |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn subnormal() { |
| 24 |
assert_eq!(dtoa(0.0f64.next_up()), "5e-324"); |
| 25 |
assert_eq!(dtoa(1e-323), "1e-323"); |
| 26 |
assert_eq!(dtoa(1.2e-322), "1.2e-322"); |
| 27 |
assert_eq!(dtoa(1.24e-322), "1.24e-322"); |
| 28 |
assert_eq!(dtoa(1.234e-320), "1.234e-320"); |
| 29 |
assert_eq!(dtoa(2.2250738585072004e-308), "2.2250738585072004e-308"); |
| 30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn all_irregular() { |
| 34 |
for exp in 1..0x3ff { |
| 35 |
let bits = exp << 52; |
| 36 |
let value = f64::from_bits(bits); |
| 37 |
|
| 38 |
assert_eq!(dtoa(value), ryu::Buffer::new().format(value)); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn all_exponents() { |
| 44 |
for exp in 0..=0x3ff { |
| 45 |
let bits = (exp << 52) | 1; |
| 46 |
let value = f64::from_bits(bits); |
| 47 |
|
| 48 |
assert_eq!(dtoa(value), ryu::Buffer::new().format(value)); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn small_int() { |
| 54 |
assert_eq!(dtoa(1.0), "1.0"); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn zero() { |
| 59 |
assert_eq!(dtoa(0.0), "0.0"); |
| 60 |
assert_eq!(dtoa(-0.0), "-0.0"); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn inf() { |
| 65 |
assert_eq!(dtoa(f64::INFINITY), "inf"); |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn nan() { |
| 70 |
assert_eq!(dtoa(f64::NAN.copysign(-1.0)), "NaN"); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn shorter() { |
| 75 |
// A possibly shorter underestimate is picked (u' in Schubfach). |
| 76 |
assert_eq!(dtoa(-4.932096661796888e-226), "-4.932096661796888e-226"); |
| 77 |
|
| 78 |
// A possibly shorter overestimate is picked (w' in Schubfach). |
| 79 |
assert_eq!(dtoa(3.439070283483335e+35), "3.439070283483335e+35"); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn single_candidate() { |
| 84 |
// Only an underestimate is in the rounding region (u in Schubfach). |
| 85 |
assert_eq!(dtoa(6.606854224493745e-17), "6.606854224493745e-17"); |
| 86 |
|
| 87 |
// Only an overestimate is in the rounding region (w in Schubfach). |
| 88 |
assert_eq!(dtoa(6.079537928711555e+61), "6.079537928711555e+61"); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn boundary_cases() { |
| 93 |
assert_eq!(dtoa(1.3588129002659584e-245), "1.3588129002659584e-245"); |
| 94 |
assert_eq!(dtoa(2.9802322387695312e-08), "2.9802322387695312e-8"); |
| 95 |
assert_eq!(dtoa(5.960464477539063e-08), "5.960464477539063e-8"); |
| 96 |
assert_eq!(dtoa(1.3076622631878654e+65), "1.3076622631878654e+65"); |
| 97 |
assert_eq!(dtoa(9.03725590277404e+159), "9.03725590277404e+159"); |
| 98 |
assert_eq!(dtoa(9.03725590277404e+160), "9.03725590277404e+160"); |
| 99 |
assert_eq!(dtoa(9.03725590277404e+161), "9.03725590277404e+161"); |
| 100 |
assert_eq!(dtoa(9.03725590277404e+162), "9.03725590277404e+162"); |
| 101 |
assert_eq!(dtoa(5.960464477539062e-07), "5.960464477539062e-7"); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn fixed_with_zeros() { |
| 106 |
assert_eq!(dtoa(43210.0), "43210.0"); |
| 107 |
assert_eq!(dtoa(43210.1), "43210.1"); |
| 108 |
assert_eq!(dtoa(10000.0), "10000.0"); |
| 109 |
assert_eq!(dtoa(-5942736479622170.0), "-5942736479622170.0"); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn no_buffer() { |
| 114 |
assert_eq!(dtoa(6.62607015e-34), "6.62607015e-34"); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
mod ftoa_test { |
| 119 |
use super::ftoa; |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn fixed_with_zeros() { |
| 123 |
assert_eq!(ftoa(43210.0), "43210.0"); |
| 124 |
assert_eq!(ftoa(43210.1), "43210.1"); |
| 125 |
assert_eq!(ftoa(10000.0), "10000.0"); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn normal() { |
| 130 |
assert_eq!(ftoa(6.62607e-34), "6.62607e-34"); |
| 131 |
assert_eq!(ftoa(1.342178e+08), "134217800.0"); |
| 132 |
assert_eq!(ftoa(1.3421781e+08), "134217810.0"); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn subnormal() { |
| 137 |
assert_eq!(ftoa(0.0f32.next_up()), "1e-45"); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn no_buffer() { |
| 142 |
assert_eq!(ftoa(6.62607e-34), "6.62607e-34"); |
| 143 |
} |
| 144 |
} |