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