| 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 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn all_irregular() { |
| 33 |
for exp in 1..0x3ff { |
| 34 |
let bits = exp << 52; |
| 35 |
let value = f64::from_bits(bits); |
| 36 |
|
| 37 |
assert_eq!(dtoa(value), ryu::Buffer::new().format(value)); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn all_exponents() { |
| 43 |
for exp in 0..=0x3ff { |
| 44 |
let bits = (exp << 52) | 1; |
| 45 |
let value = f64::from_bits(bits); |
| 46 |
|
| 47 |
assert_eq!(dtoa(value), ryu::Buffer::new().format(value)); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn small_int() { |
| 53 |
assert_eq!(dtoa(1.0), "1.0"); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn zero() { |
| 58 |
assert_eq!(dtoa(0.0), "0.0"); |
| 59 |
assert_eq!(dtoa(-0.0), "-0.0"); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn inf() { |
| 64 |
assert_eq!(dtoa(f64::INFINITY), "inf"); |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn nan() { |
| 69 |
assert_eq!(dtoa(f64::NAN.copysign(-1.0)), "NaN"); |
| 70 |
} |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn shorter() { |
| 74 |
// A possibly shorter underestimate is picked (u' in Schubfach). |
| 75 |
assert_eq!(dtoa(-4.932096661796888e-226), "-4.932096661796888e-226"); |
| 76 |
|
| 77 |
// A possibly shorter overestimate is picked (w' in Schubfach). |
| 78 |
assert_eq!(dtoa(3.439070283483335e+35), "3.439070283483335e+35"); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn single_candidate() { |
| 83 |
// Only an underestimate is in the rounding region (u in Schubfach). |
| 84 |
assert_eq!(dtoa(6.606854224493745e-17), "6.606854224493745e-17"); |
| 85 |
|
| 86 |
// Only an overestimate is in the rounding region (w in Schubfach). |
| 87 |
assert_eq!(dtoa(6.079537928711555e+61), "6.079537928711555e+61"); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn null_terminated() { |
| 92 |
assert_eq!(dtoa(9.061488e15), "9061488000000000.0"); |
| 93 |
assert_eq!(dtoa(f64::NAN.copysign(1.0)), "NaN"); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn no_buffer() { |
| 98 |
assert_eq!(dtoa(6.62607015e-34), "6.62607015e-34"); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
mod ftoa_test { |
| 103 |
use super::ftoa; |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn normal() { |
| 107 |
assert_eq!(ftoa(6.62607e-34), "6.62607e-34"); |
| 108 |
assert_eq!(ftoa(1.342178e+08), "134217800.0"); |
| 109 |
assert_eq!(ftoa(1.3421781e+08), "134217810.0"); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn subnormal() { |
| 114 |
assert_eq!(ftoa(0.0f32.next_up()), "1e-45"); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn no_buffer() { |
| 119 |
assert_eq!(ftoa(6.62607e-34), "6.62607e-34"); |
| 120 |
} |
| 121 |
} |