1 | // Translated from C to Rust. The original C code can be found at |
2 | // https://github.com/ulfjack/ryu and carries the following license: |
3 | // |
4 | // Copyright 2018 Ulf Adams |
5 | // |
6 | // The contents of this file may be used under the terms of the Apache License, |
7 | // Version 2.0. |
8 | // |
9 | // (See accompanying file LICENSE-Apache or copy at |
10 | // http://www.apache.org/licenses/LICENSE-2.0) |
11 | // |
12 | // Alternatively, the contents of this file may be used under the terms of |
13 | // the Boost Software License, Version 1.0. |
14 | // (See accompanying file LICENSE-Boost or copy at |
15 | // https://www.boost.org/LICENSE_1_0.txt) |
16 | // |
17 | // Unless required by applicable law or agreed to in writing, this software |
18 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
19 | // KIND, either express or implied. |
20 | |
21 | #![allow ( |
22 | clippy::approx_constant, |
23 | clippy::cast_lossless, |
24 | clippy::float_cmp, |
25 | clippy::int_plus_one, |
26 | clippy::non_ascii_literal, |
27 | clippy::unreadable_literal, |
28 | clippy::unseparated_literal_suffix |
29 | )] |
30 | |
31 | #[macro_use ] |
32 | mod macros; |
33 | |
34 | use std::f64; |
35 | |
36 | fn pretty(f: f64) -> String { |
37 | ryu::Buffer::new().format(f).to_owned() |
38 | } |
39 | |
40 | fn ieee_parts_to_double(sign: bool, ieee_exponent: u32, ieee_mantissa: u64) -> f64 { |
41 | assert!(ieee_exponent <= 2047); |
42 | assert!(ieee_mantissa <= (1u64 << 53) - 1); |
43 | f64::from_bits(((sign as u64) << 63) | ((ieee_exponent as u64) << 52) | ieee_mantissa) |
44 | } |
45 | |
46 | #[test] |
47 | fn test_ryu() { |
48 | check!(0.3); |
49 | check!(1234000000000000.0); |
50 | check!(1.234e16); |
51 | check!(2.71828); |
52 | check!(1.1e128); |
53 | check!(1.1e-64); |
54 | check!(2.718281828459045); |
55 | check!(5e-324); |
56 | check!(1.7976931348623157e308); |
57 | } |
58 | |
59 | #[test] |
60 | fn test_random() { |
61 | let n = if cfg!(miri) { 100 } else { 1000000 }; |
62 | let mut buffer = ryu::Buffer::new(); |
63 | for _ in 0..n { |
64 | let f: f64 = rand::random(); |
65 | assert_eq!(f, buffer.format_finite(f).parse().unwrap()); |
66 | } |
67 | } |
68 | |
69 | #[test] |
70 | #[cfg_attr (miri, ignore)] |
71 | fn test_non_finite() { |
72 | for i in 0u64..1 << 23 { |
73 | let f = f64::from_bits((((1 << 11) - 1) << 52) + (i << 29)); |
74 | assert!(!f.is_finite(), "f={}" , f); |
75 | ryu::Buffer::new().format_finite(f); |
76 | } |
77 | } |
78 | |
79 | #[test] |
80 | fn test_basic() { |
81 | check!(0.0); |
82 | check!(-0.0); |
83 | check!(1.0); |
84 | check!(-1.0); |
85 | assert_eq!(pretty(f64::NAN.copysign(1.0)), "NaN" ); |
86 | assert_eq!(pretty(f64::NAN.copysign(-1.0)), "NaN" ); |
87 | assert_eq!(pretty(f64::INFINITY), "inf" ); |
88 | assert_eq!(pretty(f64::NEG_INFINITY), "-inf" ); |
89 | } |
90 | |
91 | #[test] |
92 | fn test_switch_to_subnormal() { |
93 | check!(2.2250738585072014e-308); |
94 | } |
95 | |
96 | #[test] |
97 | fn test_min_and_max() { |
98 | assert_eq!(f64::from_bits(0x7fefffffffffffff), 1.7976931348623157e308); |
99 | check!(1.7976931348623157e308); |
100 | assert_eq!(f64::from_bits(1), 5e-324); |
101 | check!(5e-324); |
102 | } |
103 | |
104 | #[test] |
105 | fn test_lots_of_trailing_zeros() { |
106 | check!(2.9802322387695312e-8); |
107 | } |
108 | |
109 | #[test] |
110 | fn test_regression() { |
111 | check!(-2.109808898695963e16); |
112 | check!(4.940656e-318); |
113 | check!(1.18575755e-316); |
114 | check!(2.989102097996e-312); |
115 | check!(9060801153433600.0); |
116 | check!(4.708356024711512e18); |
117 | check!(9.409340012568248e18); |
118 | check!(1.2345678); |
119 | } |
120 | |
121 | #[test] |
122 | fn test_looks_like_pow5() { |
123 | // These numbers have a mantissa that is a multiple of the largest power of |
124 | // 5 that fits, and an exponent that causes the computation for q to result |
125 | // in 22, which is a corner case for Ryƫ. |
126 | assert_eq!(f64::from_bits(0x4830F0CF064DD592), 5.764607523034235e39); |
127 | check!(5.764607523034235e39); |
128 | assert_eq!(f64::from_bits(0x4840F0CF064DD592), 1.152921504606847e40); |
129 | check!(1.152921504606847e40); |
130 | assert_eq!(f64::from_bits(0x4850F0CF064DD592), 2.305843009213694e40); |
131 | check!(2.305843009213694e40); |
132 | } |
133 | |
134 | #[test] |
135 | fn test_output_length() { |
136 | check!(1.0); // already tested in Basic |
137 | check!(1.2); |
138 | check!(1.23); |
139 | check!(1.234); |
140 | check!(1.2345); |
141 | check!(1.23456); |
142 | check!(1.234567); |
143 | check!(1.2345678); // already tested in Regression |
144 | check!(1.23456789); |
145 | check!(1.234567895); // 1.234567890 would be trimmed |
146 | check!(1.2345678901); |
147 | check!(1.23456789012); |
148 | check!(1.234567890123); |
149 | check!(1.2345678901234); |
150 | check!(1.23456789012345); |
151 | check!(1.234567890123456); |
152 | check!(1.2345678901234567); |
153 | |
154 | // Test 32-bit chunking |
155 | check!(4.294967294); // 2^32 - 2 |
156 | check!(4.294967295); // 2^32 - 1 |
157 | check!(4.294967296); // 2^32 |
158 | check!(4.294967297); // 2^32 + 1 |
159 | check!(4.294967298); // 2^32 + 2 |
160 | } |
161 | |
162 | // Test min, max shift values in shiftright128 |
163 | #[test] |
164 | fn test_min_max_shift() { |
165 | let max_mantissa = (1u64 << 53) - 1; |
166 | |
167 | // 32-bit opt-size=0: 49 <= dist <= 50 |
168 | // 32-bit opt-size=1: 30 <= dist <= 50 |
169 | // 64-bit opt-size=0: 50 <= dist <= 50 |
170 | // 64-bit opt-size=1: 30 <= dist <= 50 |
171 | assert_eq!(1.7800590868057611E-307, ieee_parts_to_double(false, 4, 0)); |
172 | check!(1.7800590868057611e-307); |
173 | // 32-bit opt-size=0: 49 <= dist <= 49 |
174 | // 32-bit opt-size=1: 28 <= dist <= 49 |
175 | // 64-bit opt-size=0: 50 <= dist <= 50 |
176 | // 64-bit opt-size=1: 28 <= dist <= 50 |
177 | assert_eq!( |
178 | 2.8480945388892175E-306, |
179 | ieee_parts_to_double(false, 6, max_mantissa) |
180 | ); |
181 | check!(2.8480945388892175e-306); |
182 | // 32-bit opt-size=0: 52 <= dist <= 53 |
183 | // 32-bit opt-size=1: 2 <= dist <= 53 |
184 | // 64-bit opt-size=0: 53 <= dist <= 53 |
185 | // 64-bit opt-size=1: 2 <= dist <= 53 |
186 | assert_eq!(2.446494580089078E-296, ieee_parts_to_double(false, 41, 0)); |
187 | check!(2.446494580089078e-296); |
188 | // 32-bit opt-size=0: 52 <= dist <= 52 |
189 | // 32-bit opt-size=1: 2 <= dist <= 52 |
190 | // 64-bit opt-size=0: 53 <= dist <= 53 |
191 | // 64-bit opt-size=1: 2 <= dist <= 53 |
192 | assert_eq!( |
193 | 4.8929891601781557E-296, |
194 | ieee_parts_to_double(false, 40, max_mantissa) |
195 | ); |
196 | check!(4.8929891601781557e-296); |
197 | |
198 | // 32-bit opt-size=0: 57 <= dist <= 58 |
199 | // 32-bit opt-size=1: 57 <= dist <= 58 |
200 | // 64-bit opt-size=0: 58 <= dist <= 58 |
201 | // 64-bit opt-size=1: 58 <= dist <= 58 |
202 | assert_eq!(1.8014398509481984E16, ieee_parts_to_double(false, 1077, 0)); |
203 | check!(1.8014398509481984e16); |
204 | // 32-bit opt-size=0: 57 <= dist <= 57 |
205 | // 32-bit opt-size=1: 57 <= dist <= 57 |
206 | // 64-bit opt-size=0: 58 <= dist <= 58 |
207 | // 64-bit opt-size=1: 58 <= dist <= 58 |
208 | assert_eq!( |
209 | 3.6028797018963964E16, |
210 | ieee_parts_to_double(false, 1076, max_mantissa) |
211 | ); |
212 | check!(3.6028797018963964e16); |
213 | // 32-bit opt-size=0: 51 <= dist <= 52 |
214 | // 32-bit opt-size=1: 51 <= dist <= 59 |
215 | // 64-bit opt-size=0: 52 <= dist <= 52 |
216 | // 64-bit opt-size=1: 52 <= dist <= 59 |
217 | assert_eq!(2.900835519859558E-216, ieee_parts_to_double(false, 307, 0)); |
218 | check!(2.900835519859558e-216); |
219 | // 32-bit opt-size=0: 51 <= dist <= 51 |
220 | // 32-bit opt-size=1: 51 <= dist <= 59 |
221 | // 64-bit opt-size=0: 52 <= dist <= 52 |
222 | // 64-bit opt-size=1: 52 <= dist <= 59 |
223 | assert_eq!( |
224 | 5.801671039719115E-216, |
225 | ieee_parts_to_double(false, 306, max_mantissa) |
226 | ); |
227 | check!(5.801671039719115e-216); |
228 | |
229 | // https://github.com/ulfjack/ryu/commit/19e44d16d80236f5de25800f56d82606d1be00b9#commitcomment-30146483 |
230 | // 32-bit opt-size=0: 49 <= dist <= 49 |
231 | // 32-bit opt-size=1: 44 <= dist <= 49 |
232 | // 64-bit opt-size=0: 50 <= dist <= 50 |
233 | // 64-bit opt-size=1: 44 <= dist <= 50 |
234 | assert_eq!( |
235 | 3.196104012172126E-27, |
236 | ieee_parts_to_double(false, 934, 0x000FA7161A4D6E0C) |
237 | ); |
238 | check!(3.196104012172126e-27); |
239 | } |
240 | |
241 | #[test] |
242 | fn test_small_integers() { |
243 | check!(9007199254740991.0); // 2^53-1 |
244 | check!(9007199254740992.0); // 2^53 |
245 | |
246 | check!(1.0); |
247 | check!(12.0); |
248 | check!(123.0); |
249 | check!(1234.0); |
250 | check!(12345.0); |
251 | check!(123456.0); |
252 | check!(1234567.0); |
253 | check!(12345678.0); |
254 | check!(123456789.0); |
255 | check!(1234567890.0); |
256 | check!(1234567895.0); |
257 | check!(12345678901.0); |
258 | check!(123456789012.0); |
259 | check!(1234567890123.0); |
260 | check!(12345678901234.0); |
261 | check!(123456789012345.0); |
262 | check!(1234567890123456.0); |
263 | |
264 | // 10^i |
265 | check!(1.0); |
266 | check!(10.0); |
267 | check!(100.0); |
268 | check!(1000.0); |
269 | check!(10000.0); |
270 | check!(100000.0); |
271 | check!(1000000.0); |
272 | check!(10000000.0); |
273 | check!(100000000.0); |
274 | check!(1000000000.0); |
275 | check!(10000000000.0); |
276 | check!(100000000000.0); |
277 | check!(1000000000000.0); |
278 | check!(10000000000000.0); |
279 | check!(100000000000000.0); |
280 | check!(1000000000000000.0); |
281 | |
282 | // 10^15 + 10^i |
283 | check!(1000000000000001.0); |
284 | check!(1000000000000010.0); |
285 | check!(1000000000000100.0); |
286 | check!(1000000000001000.0); |
287 | check!(1000000000010000.0); |
288 | check!(1000000000100000.0); |
289 | check!(1000000001000000.0); |
290 | check!(1000000010000000.0); |
291 | check!(1000000100000000.0); |
292 | check!(1000001000000000.0); |
293 | check!(1000010000000000.0); |
294 | check!(1000100000000000.0); |
295 | check!(1001000000000000.0); |
296 | check!(1010000000000000.0); |
297 | check!(1100000000000000.0); |
298 | |
299 | // Largest power of 2 <= 10^(i+1) |
300 | check!(8.0); |
301 | check!(64.0); |
302 | check!(512.0); |
303 | check!(8192.0); |
304 | check!(65536.0); |
305 | check!(524288.0); |
306 | check!(8388608.0); |
307 | check!(67108864.0); |
308 | check!(536870912.0); |
309 | check!(8589934592.0); |
310 | check!(68719476736.0); |
311 | check!(549755813888.0); |
312 | check!(8796093022208.0); |
313 | check!(70368744177664.0); |
314 | check!(562949953421312.0); |
315 | check!(9007199254740992.0); |
316 | |
317 | // 1000 * (Largest power of 2 <= 10^(i+1)) |
318 | check!(8000.0); |
319 | check!(64000.0); |
320 | check!(512000.0); |
321 | check!(8192000.0); |
322 | check!(65536000.0); |
323 | check!(524288000.0); |
324 | check!(8388608000.0); |
325 | check!(67108864000.0); |
326 | check!(536870912000.0); |
327 | check!(8589934592000.0); |
328 | check!(68719476736000.0); |
329 | check!(549755813888000.0); |
330 | check!(8796093022208000.0); |
331 | } |
332 | |