]> git.proxmox.com Git - rustc.git/blob - src/libcore/tests/num/flt2dec/mod.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libcore / tests / num / flt2dec / mod.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::prelude::v1::*;
12 use std::{str, i16, f32, f64, fmt};
13
14 use core::num::flt2dec::{decode, DecodableFloat, FullDecoded, Decoded};
15 use core::num::flt2dec::{MAX_SIG_DIGITS, round_up, Part, Formatted, Sign};
16 use core::num::flt2dec::{to_shortest_str, to_shortest_exp_str,
17 to_exact_exp_str, to_exact_fixed_str};
18
19 pub use test::Bencher;
20
21 mod estimator;
22 mod strategy {
23 mod dragon;
24 mod grisu;
25 }
26
27 pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
28 match decode(v).1 {
29 FullDecoded::Finite(decoded) => decoded,
30 full_decoded => panic!("expected finite, got {:?} instead", full_decoded)
31 }
32 }
33
34 macro_rules! check_shortest {
35 ($f:ident($v:expr) => $buf:expr, $exp:expr) => (
36 check_shortest!($f($v) => $buf, $exp;
37 "shortest mismatch for v={v}: actual {actual:?}, expected {expected:?}",
38 v = stringify!($v))
39 );
40
41 ($f:ident{$($k:ident: $v:expr),+} => $buf:expr, $exp:expr) => (
42 check_shortest!($f{$($k: $v),+} => $buf, $exp;
43 "shortest mismatch for {v:?}: actual {actual:?}, expected {expected:?}",
44 v = Decoded { $($k: $v),+ })
45 );
46
47 ($f:ident($v:expr) => $buf:expr, $exp:expr; $fmt:expr, $($key:ident = $val:expr),*) => ({
48 let mut buf = [b'_'; MAX_SIG_DIGITS];
49 let (len, k) = $f(&decode_finite($v), &mut buf);
50 assert!((&buf[..len], k) == ($buf, $exp),
51 $fmt, actual = (str::from_utf8(&buf[..len]).unwrap(), k),
52 expected = (str::from_utf8($buf).unwrap(), $exp),
53 $($key = $val),*);
54 });
55
56 ($f:ident{$($k:ident: $v:expr),+} => $buf:expr, $exp:expr;
57 $fmt:expr, $($key:ident = $val:expr),*) => ({
58 let mut buf = [b'_'; MAX_SIG_DIGITS];
59 let (len, k) = $f(&Decoded { $($k: $v),+ }, &mut buf);
60 assert!((&buf[..len], k) == ($buf, $exp),
61 $fmt, actual = (str::from_utf8(&buf[..len]).unwrap(), k),
62 expected = (str::from_utf8($buf).unwrap(), $exp),
63 $($key = $val),*);
64 })
65 }
66
67 macro_rules! try_exact {
68 ($f:ident($decoded:expr) => $buf:expr, $expected:expr, $expectedk:expr;
69 $fmt:expr, $($key:ident = $val:expr),*) => ({
70 let (len, k) = $f($decoded, &mut $buf[..$expected.len()], i16::MIN);
71 assert!((&$buf[..len], k) == ($expected, $expectedk),
72 $fmt, actual = (str::from_utf8(&$buf[..len]).unwrap(), k),
73 expected = (str::from_utf8($expected).unwrap(), $expectedk),
74 $($key = $val),*);
75 })
76 }
77
78 macro_rules! try_fixed {
79 ($f:ident($decoded:expr) => $buf:expr, $request:expr, $expected:expr, $expectedk:expr;
80 $fmt:expr, $($key:ident = $val:expr),*) => ({
81 let (len, k) = $f($decoded, &mut $buf[..], $request);
82 assert!((&$buf[..len], k) == ($expected, $expectedk),
83 $fmt, actual = (str::from_utf8(&$buf[..len]).unwrap(), k),
84 expected = (str::from_utf8($expected).unwrap(), $expectedk),
85 $($key = $val),*);
86 })
87 }
88
89 fn ldexp_f32(a: f32, b: i32) -> f32 {
90 ldexp_f64(a as f64, b) as f32
91 }
92
93 fn ldexp_f64(a: f64, b: i32) -> f64 {
94 extern {
95 fn ldexp(x: f64, n: i32) -> f64;
96 }
97 unsafe { ldexp(a, b) }
98 }
99
100 fn check_exact<F, T>(mut f: F, v: T, vstr: &str, expected: &[u8], expectedk: i16)
101 where T: DecodableFloat, F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
102 // use a large enough buffer
103 let mut buf = [b'_'; 1024];
104 let mut expected_ = [b'_'; 1024];
105
106 let decoded = decode_finite(v);
107 let cut = expected.iter().position(|&c| c == b' ');
108
109 // check significant digits
110 for i in 1..cut.unwrap_or(expected.len() - 1) {
111 expected_[..i].copy_from_slice(&expected[..i]);
112 let mut expectedk_ = expectedk;
113 if expected[i] >= b'5' {
114 // check if this is a rounding-to-even case.
115 // we avoid rounding ...x5000... (with infinite zeroes) to ...(x+1) when x is even.
116 if !(i+1 < expected.len() && expected[i-1] & 1 == 0 &&
117 expected[i] == b'5' &&
118 expected[i+1] == b' ') {
119 // if this returns true, expected_[..i] is all `9`s and being rounded up.
120 // we should always return `100..00` (`i` digits) instead, since that's
121 // what we can came up with `i` digits anyway. `round_up` assumes that
122 // the adjustment to the length is done by caller, which we simply ignore.
123 if let Some(_) = round_up(&mut expected_, i) { expectedk_ += 1; }
124 }
125 }
126
127 try_exact!(f(&decoded) => &mut buf, &expected_[..i], expectedk_;
128 "exact sigdigit mismatch for v={v}, i={i}: \
129 actual {actual:?}, expected {expected:?}",
130 v = vstr, i = i);
131 try_fixed!(f(&decoded) => &mut buf, expectedk_ - i as i16, &expected_[..i], expectedk_;
132 "fixed sigdigit mismatch for v={v}, i={i}: \
133 actual {actual:?}, expected {expected:?}",
134 v = vstr, i = i);
135 }
136
137 // check exact rounding for zero- and negative-width cases
138 let start;
139 if expected[0] >= b'5' {
140 try_fixed!(f(&decoded) => &mut buf, expectedk, b"1", expectedk + 1;
141 "zero-width rounding-up mismatch for v={v}: \
142 actual {actual:?}, expected {expected:?}",
143 v = vstr);
144 start = 1;
145 } else {
146 start = 0;
147 }
148 for i in start..-10 {
149 try_fixed!(f(&decoded) => &mut buf, expectedk - i, b"", expectedk;
150 "rounding-down mismatch for v={v}, i={i}: \
151 actual {actual:?}, expected {expected:?}",
152 v = vstr, i = -i);
153 }
154
155 // check infinite zero digits
156 if let Some(cut) = cut {
157 for i in cut..expected.len()-1 {
158 expected_[..cut].copy_from_slice(&expected[..cut]);
159 for c in &mut expected_[cut..i] { *c = b'0'; }
160
161 try_exact!(f(&decoded) => &mut buf, &expected_[..i], expectedk;
162 "exact infzero mismatch for v={v}, i={i}: \
163 actual {actual:?}, expected {expected:?}",
164 v = vstr, i = i);
165 try_fixed!(f(&decoded) => &mut buf, expectedk - i as i16, &expected_[..i], expectedk;
166 "fixed infzero mismatch for v={v}, i={i}: \
167 actual {actual:?}, expected {expected:?}",
168 v = vstr, i = i);
169 }
170 }
171 }
172
173 trait TestableFloat : DecodableFloat + fmt::Display {
174 /// Returns `x * 2^exp`. Almost same to `std::{f32,f64}::ldexp`.
175 /// This is used for testing.
176 fn ldexpi(f: i64, exp: isize) -> Self;
177 }
178
179 impl TestableFloat for f32 {
180 fn ldexpi(f: i64, exp: isize) -> Self { f as Self * (exp as Self).exp2() }
181 }
182
183 impl TestableFloat for f64 {
184 fn ldexpi(f: i64, exp: isize) -> Self { f as Self * (exp as Self).exp2() }
185 }
186
187 fn check_exact_one<F, T>(mut f: F, x: i64, e: isize, tstr: &str, expected: &[u8], expectedk: i16)
188 where T: TestableFloat,
189 F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
190 // use a large enough buffer
191 let mut buf = [b'_'; 1024];
192 let v: T = TestableFloat::ldexpi(x, e);
193 let decoded = decode_finite(v);
194
195 try_exact!(f(&decoded) => &mut buf, &expected, expectedk;
196 "exact mismatch for v={x}p{e}{t}: actual {actual:?}, expected {expected:?}",
197 x = x, e = e, t = tstr);
198 try_fixed!(f(&decoded) => &mut buf, expectedk - expected.len() as i16, &expected, expectedk;
199 "fixed mismatch for v={x}p{e}{t}: actual {actual:?}, expected {expected:?}",
200 x = x, e = e, t = tstr);
201 }
202
203 macro_rules! check_exact {
204 ($f:ident($v:expr) => $buf:expr, $exp:expr) => (
205 check_exact(|d,b,k| $f(d,b,k), $v, stringify!($v), $buf, $exp)
206 )
207 }
208
209 macro_rules! check_exact_one {
210 ($f:ident($x:expr, $e:expr; $t:ty) => $buf:expr, $exp:expr) => (
211 check_exact_one::<_, $t>(|d,b,k| $f(d,b,k), $x, $e, stringify!($t), $buf, $exp)
212 )
213 }
214
215 // in the following comments, three numbers are spaced by 1 ulp apart,
216 // and the second one is being formatted.
217 //
218 // some tests are derived from [1].
219 //
220 // [1] Vern Paxson, A Program for Testing IEEE Decimal-Binary Conversion
221 // ftp://ftp.ee.lbl.gov/testbase-report.ps.Z
222
223 pub fn f32_shortest_sanity_test<F>(mut f: F) where F: FnMut(&Decoded, &mut [u8]) -> (usize, i16) {
224 // 0.0999999940395355224609375
225 // 0.100000001490116119384765625
226 // 0.10000000894069671630859375
227 check_shortest!(f(0.1f32) => b"1", 0);
228
229 // 0.333333313465118408203125
230 // 0.3333333432674407958984375 (1/3 in the default rounding)
231 // 0.33333337306976318359375
232 check_shortest!(f(1.0f32/3.0) => b"33333334", 0);
233
234 // 10^1 * 0.31415917873382568359375
235 // 10^1 * 0.31415920257568359375
236 // 10^1 * 0.31415922641754150390625
237 check_shortest!(f(3.141592f32) => b"3141592", 1);
238
239 // 10^18 * 0.31415916243714048
240 // 10^18 * 0.314159196796878848
241 // 10^18 * 0.314159231156617216
242 check_shortest!(f(3.141592e17f32) => b"3141592", 18);
243
244 // regression test for decoders
245 // 10^8 * 0.3355443
246 // 10^8 * 0.33554432
247 // 10^8 * 0.33554436
248 check_shortest!(f(ldexp_f32(1.0, 25)) => b"33554432", 8);
249
250 // 10^39 * 0.340282326356119256160033759537265639424
251 // 10^39 * 0.34028234663852885981170418348451692544
252 // 10^39 * 0.340282366920938463463374607431768211456
253 check_shortest!(f(f32::MAX) => b"34028235", 39);
254
255 // 10^-37 * 0.1175494210692441075487029444849287348827...
256 // 10^-37 * 0.1175494350822287507968736537222245677818...
257 // 10^-37 * 0.1175494490952133940450443629595204006810...
258 check_shortest!(f(f32::MIN_POSITIVE) => b"11754944", -37);
259
260 // 10^-44 * 0
261 // 10^-44 * 0.1401298464324817070923729583289916131280...
262 // 10^-44 * 0.2802596928649634141847459166579832262560...
263 let minf32 = ldexp_f32(1.0, -149);
264 check_shortest!(f(minf32) => b"1", -44);
265 }
266
267 pub fn f32_exact_sanity_test<F>(mut f: F)
268 where F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
269 let minf32 = ldexp_f32(1.0, -149);
270
271 check_exact!(f(0.1f32) => b"100000001490116119384765625 ", 0);
272 check_exact!(f(0.5f32) => b"5 ", 0);
273 check_exact!(f(1.0f32/3.0) => b"3333333432674407958984375 ", 0);
274 check_exact!(f(3.141592f32) => b"31415920257568359375 ", 1);
275 check_exact!(f(3.141592e17f32) => b"314159196796878848 ", 18);
276 check_exact!(f(f32::MAX) => b"34028234663852885981170418348451692544 ", 39);
277 check_exact!(f(f32::MIN_POSITIVE) => b"1175494350822287507968736537222245677818", -37);
278 check_exact!(f(minf32) => b"1401298464324817070923729583289916131280", -44);
279
280 // [1], Table 16: Stress Inputs for Converting 24-bit Binary to Decimal, < 1/2 ULP
281 check_exact_one!(f(12676506, -102; f32) => b"2", -23);
282 check_exact_one!(f(12676506, -103; f32) => b"12", -23);
283 check_exact_one!(f(15445013, 86; f32) => b"119", 34);
284 check_exact_one!(f(13734123, -138; f32) => b"3941", -34);
285 check_exact_one!(f(12428269, -130; f32) => b"91308", -32);
286 check_exact_one!(f(15334037, -146; f32) => b"171900", -36);
287 check_exact_one!(f(11518287, -41; f32) => b"5237910", -5);
288 check_exact_one!(f(12584953, -145; f32) => b"28216440", -36);
289 check_exact_one!(f(15961084, -125; f32) => b"375243281", -30);
290 check_exact_one!(f(14915817, -146; f32) => b"1672120916", -36);
291 check_exact_one!(f(10845484, -102; f32) => b"21388945814", -23);
292 check_exact_one!(f(16431059, -61; f32) => b"712583594561", -11);
293
294 // [1], Table 17: Stress Inputs for Converting 24-bit Binary to Decimal, > 1/2 ULP
295 check_exact_one!(f(16093626, 69; f32) => b"1", 29);
296 check_exact_one!(f( 9983778, 25; f32) => b"34", 15);
297 check_exact_one!(f(12745034, 104; f32) => b"259", 39);
298 check_exact_one!(f(12706553, 72; f32) => b"6001", 29);
299 check_exact_one!(f(11005028, 45; f32) => b"38721", 21);
300 check_exact_one!(f(15059547, 71; f32) => b"355584", 29);
301 check_exact_one!(f(16015691, -99; f32) => b"2526831", -22);
302 check_exact_one!(f( 8667859, 56; f32) => b"62458507", 24);
303 check_exact_one!(f(14855922, -82; f32) => b"307213267", -17);
304 check_exact_one!(f(14855922, -83; f32) => b"1536066333", -17);
305 check_exact_one!(f(10144164, -110; f32) => b"78147796834", -26);
306 check_exact_one!(f(13248074, 95; f32) => b"524810279937", 36);
307 }
308
309 pub fn f64_shortest_sanity_test<F>(mut f: F) where F: FnMut(&Decoded, &mut [u8]) -> (usize, i16) {
310 // 0.0999999999999999777955395074968691915273...
311 // 0.1000000000000000055511151231257827021181...
312 // 0.1000000000000000333066907387546962127089...
313 check_shortest!(f(0.1f64) => b"1", 0);
314
315 // this example is explicitly mentioned in the paper.
316 // 10^3 * 0.0999999999999999857891452847979962825775...
317 // 10^3 * 0.1 (exact)
318 // 10^3 * 0.1000000000000000142108547152020037174224...
319 check_shortest!(f(100.0f64) => b"1", 3);
320
321 // 0.3333333333333332593184650249895639717578...
322 // 0.3333333333333333148296162562473909929394... (1/3 in the default rounding)
323 // 0.3333333333333333703407674875052180141210...
324 check_shortest!(f(1.0f64/3.0) => b"3333333333333333", 0);
325
326 // explicit test case for equally closest representations.
327 // Dragon has its own tie-breaking rule; Grisu should fall back.
328 // 10^1 * 0.1000007629394531027955395074968691915273...
329 // 10^1 * 0.100000762939453125 (exact)
330 // 10^1 * 0.1000007629394531472044604925031308084726...
331 check_shortest!(f(1.00000762939453125f64) => b"10000076293945313", 1);
332
333 // 10^1 * 0.3141591999999999718085064159822650253772...
334 // 10^1 * 0.3141592000000000162174274009885266423225...
335 // 10^1 * 0.3141592000000000606263483859947882592678...
336 check_shortest!(f(3.141592f64) => b"3141592", 1);
337
338 // 10^18 * 0.314159199999999936
339 // 10^18 * 0.3141592 (exact)
340 // 10^18 * 0.314159200000000064
341 check_shortest!(f(3.141592e17f64) => b"3141592", 18);
342
343 // regression test for decoders
344 // 10^20 * 0.18446744073709549568
345 // 10^20 * 0.18446744073709551616
346 // 10^20 * 0.18446744073709555712
347 check_shortest!(f(ldexp_f64(1.0, 64)) => b"18446744073709552", 20);
348
349 // pathological case: high = 10^23 (exact). tie breaking should always prefer that.
350 // 10^24 * 0.099999999999999974834176
351 // 10^24 * 0.099999999999999991611392
352 // 10^24 * 0.100000000000000008388608
353 check_shortest!(f(1.0e23f64) => b"1", 24);
354
355 // 10^309 * 0.1797693134862315508561243283845062402343...
356 // 10^309 * 0.1797693134862315708145274237317043567980...
357 // 10^309 * 0.1797693134862315907729305190789024733617...
358 check_shortest!(f(f64::MAX) => b"17976931348623157", 309);
359
360 // 10^-307 * 0.2225073858507200889024586876085859887650...
361 // 10^-307 * 0.2225073858507201383090232717332404064219...
362 // 10^-307 * 0.2225073858507201877155878558578948240788...
363 check_shortest!(f(f64::MIN_POSITIVE) => b"22250738585072014", -307);
364
365 // 10^-323 * 0
366 // 10^-323 * 0.4940656458412465441765687928682213723650...
367 // 10^-323 * 0.9881312916824930883531375857364427447301...
368 let minf64 = ldexp_f64(1.0, -1074);
369 check_shortest!(f(minf64) => b"5", -323);
370 }
371
372 pub fn f64_exact_sanity_test<F>(mut f: F)
373 where F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
374 let minf64 = ldexp_f64(1.0, -1074);
375
376 check_exact!(f(0.1f64) => b"1000000000000000055511151231257827021181", 0);
377 check_exact!(f(0.45f64) => b"4500000000000000111022302462515654042363", 0);
378 check_exact!(f(0.5f64) => b"5 ", 0);
379 check_exact!(f(0.95f64) => b"9499999999999999555910790149937383830547", 0);
380 check_exact!(f(100.0f64) => b"1 ", 3);
381 check_exact!(f(999.5f64) => b"9995000000000000000000000000000000000000", 3);
382 check_exact!(f(1.0f64/3.0) => b"3333333333333333148296162562473909929394", 0);
383 check_exact!(f(3.141592f64) => b"3141592000000000162174274009885266423225", 1);
384 check_exact!(f(3.141592e17f64) => b"3141592 ", 18);
385 check_exact!(f(1.0e23f64) => b"99999999999999991611392 ", 23);
386 check_exact!(f(f64::MAX) => b"1797693134862315708145274237317043567980", 309);
387 check_exact!(f(f64::MIN_POSITIVE) => b"2225073858507201383090232717332404064219", -307);
388 check_exact!(f(minf64) => b"4940656458412465441765687928682213723650\
389 5980261432476442558568250067550727020875\
390 1865299836361635992379796564695445717730\
391 9266567103559397963987747960107818781263\
392 0071319031140452784581716784898210368871\
393 8636056998730723050006387409153564984387\
394 3124733972731696151400317153853980741262\
395 3856559117102665855668676818703956031062\
396 4931945271591492455329305456544401127480\
397 1297099995419319894090804165633245247571\
398 4786901472678015935523861155013480352649\
399 3472019379026810710749170333222684475333\
400 5720832431936092382893458368060106011506\
401 1698097530783422773183292479049825247307\
402 7637592724787465608477820373446969953364\
403 7017972677717585125660551199131504891101\
404 4510378627381672509558373897335989936648\
405 0994116420570263709027924276754456522908\
406 7538682506419718265533447265625 ", -323);
407
408 // [1], Table 3: Stress Inputs for Converting 53-bit Binary to Decimal, < 1/2 ULP
409 check_exact_one!(f(8511030020275656, -342; f64) => b"9", -87);
410 check_exact_one!(f(5201988407066741, -824; f64) => b"46", -232);
411 check_exact_one!(f(6406892948269899, 237; f64) => b"141", 88);
412 check_exact_one!(f(8431154198732492, 72; f64) => b"3981", 38);
413 check_exact_one!(f(6475049196144587, 99; f64) => b"41040", 46);
414 check_exact_one!(f(8274307542972842, 726; f64) => b"292084", 235);
415 check_exact_one!(f(5381065484265332, -456; f64) => b"2891946", -121);
416 check_exact_one!(f(6761728585499734, -1057; f64) => b"43787718", -302);
417 check_exact_one!(f(7976538478610756, 376; f64) => b"122770163", 130);
418 check_exact_one!(f(5982403858958067, 377; f64) => b"1841552452", 130);
419 check_exact_one!(f(5536995190630837, 93; f64) => b"54835744350", 44);
420 check_exact_one!(f(7225450889282194, 710; f64) => b"389190181146", 230);
421 check_exact_one!(f(7225450889282194, 709; f64) => b"1945950905732", 230);
422 check_exact_one!(f(8703372741147379, 117; f64) => b"14460958381605", 52);
423 check_exact_one!(f(8944262675275217, -1001; f64) => b"417367747458531", -285);
424 check_exact_one!(f(7459803696087692, -707; f64) => b"1107950772878888", -196);
425 check_exact_one!(f(6080469016670379, -381; f64) => b"12345501366327440", -98);
426 check_exact_one!(f(8385515147034757, 721; f64) => b"925031711960365024", 233);
427 check_exact_one!(f(7514216811389786, -828; f64) => b"4198047150284889840", -233);
428 check_exact_one!(f(8397297803260511, -345; f64) => b"11716315319786511046", -87);
429 check_exact_one!(f(6733459239310543, 202; f64) => b"432810072844612493629", 77);
430 check_exact_one!(f(8091450587292794, -473; f64) => b"3317710118160031081518", -126);
431
432 // [1], Table 4: Stress Inputs for Converting 53-bit Binary to Decimal, > 1/2 ULP
433 check_exact_one!(f(6567258882077402, 952; f64) => b"3", 303);
434 check_exact_one!(f(6712731423444934, 535; f64) => b"76", 177);
435 check_exact_one!(f(6712731423444934, 534; f64) => b"378", 177);
436 check_exact_one!(f(5298405411573037, -957; f64) => b"4350", -272);
437 check_exact_one!(f(5137311167659507, -144; f64) => b"23037", -27);
438 check_exact_one!(f(6722280709661868, 363; f64) => b"126301", 126);
439 check_exact_one!(f(5344436398034927, -169; f64) => b"7142211", -35);
440 check_exact_one!(f(8369123604277281, -853; f64) => b"13934574", -240);
441 check_exact_one!(f(8995822108487663, -780; f64) => b"141463449", -218);
442 check_exact_one!(f(8942832835564782, -383; f64) => b"4539277920", -99);
443 check_exact_one!(f(8942832835564782, -384; f64) => b"22696389598", -99);
444 check_exact_one!(f(8942832835564782, -385; f64) => b"113481947988", -99);
445 check_exact_one!(f(6965949469487146, -249; f64) => b"7700366561890", -59);
446 check_exact_one!(f(6965949469487146, -250; f64) => b"38501832809448", -59);
447 check_exact_one!(f(6965949469487146, -251; f64) => b"192509164047238", -59);
448 check_exact_one!(f(7487252720986826, 548; f64) => b"6898586531774201", 181);
449 check_exact_one!(f(5592117679628511, 164; f64) => b"13076622631878654", 66);
450 check_exact_one!(f(8887055249355788, 665; f64) => b"136052020756121240", 217);
451 check_exact_one!(f(6994187472632449, 690; f64) => b"3592810217475959676", 224);
452 check_exact_one!(f(8797576579012143, 588; f64) => b"89125197712484551899", 193);
453 check_exact_one!(f(7363326733505337, 272; f64) => b"558769757362301140950", 98);
454 check_exact_one!(f(8549497411294502, -448; f64) => b"1176257830728540379990", -118);
455 }
456
457 pub fn more_shortest_sanity_test<F>(mut f: F) where F: FnMut(&Decoded, &mut [u8]) -> (usize, i16) {
458 check_shortest!(f{mant: 99_999_999_999_999_999, minus: 1, plus: 1,
459 exp: 0, inclusive: true} => b"1", 18);
460 check_shortest!(f{mant: 99_999_999_999_999_999, minus: 1, plus: 1,
461 exp: 0, inclusive: false} => b"99999999999999999", 17);
462 }
463
464 fn to_string_with_parts<F>(mut f: F) -> String
465 where F: for<'a> FnMut(&'a mut [u8], &'a mut [Part<'a>]) -> Formatted<'a> {
466 let mut buf = [0; 1024];
467 let mut parts = [Part::Zero(0); 16];
468 let formatted = f(&mut buf, &mut parts);
469 let mut ret = vec![0; formatted.len()];
470 assert_eq!(formatted.write(&mut ret), Some(ret.len()));
471 String::from_utf8(ret).unwrap()
472 }
473
474 pub fn to_shortest_str_test<F>(mut f_: F)
475 where F: FnMut(&Decoded, &mut [u8]) -> (usize, i16) {
476 use core::num::flt2dec::Sign::*;
477
478 fn to_string<T, F>(f: &mut F, v: T, sign: Sign, frac_digits: usize, upper: bool) -> String
479 where T: DecodableFloat, F: FnMut(&Decoded, &mut [u8]) -> (usize, i16) {
480 to_string_with_parts(|buf, parts| to_shortest_str(|d,b| f(d,b), v, sign,
481 frac_digits, upper, buf, parts))
482 }
483
484 let f = &mut f_;
485
486 assert_eq!(to_string(f, 0.0, Minus, 0, false), "0");
487 assert_eq!(to_string(f, 0.0, MinusRaw, 0, false), "0");
488 assert_eq!(to_string(f, 0.0, MinusPlus, 0, false), "+0");
489 assert_eq!(to_string(f, 0.0, MinusPlusRaw, 0, false), "+0");
490 assert_eq!(to_string(f, -0.0, Minus, 0, false), "0");
491 assert_eq!(to_string(f, -0.0, MinusRaw, 0, false), "-0");
492 assert_eq!(to_string(f, -0.0, MinusPlus, 0, false), "+0");
493 assert_eq!(to_string(f, -0.0, MinusPlusRaw, 0, false), "-0");
494 assert_eq!(to_string(f, 0.0, Minus, 1, true), "0.0");
495 assert_eq!(to_string(f, 0.0, MinusRaw, 1, true), "0.0");
496 assert_eq!(to_string(f, 0.0, MinusPlus, 1, true), "+0.0");
497 assert_eq!(to_string(f, 0.0, MinusPlusRaw, 1, true), "+0.0");
498 assert_eq!(to_string(f, -0.0, Minus, 8, true), "0.00000000");
499 assert_eq!(to_string(f, -0.0, MinusRaw, 8, true), "-0.00000000");
500 assert_eq!(to_string(f, -0.0, MinusPlus, 8, true), "+0.00000000");
501 assert_eq!(to_string(f, -0.0, MinusPlusRaw, 8, true), "-0.00000000");
502
503 assert_eq!(to_string(f, 1.0/0.0, Minus, 0, false), "inf");
504 assert_eq!(to_string(f, 1.0/0.0, MinusRaw, 0, true), "inf");
505 assert_eq!(to_string(f, 1.0/0.0, MinusPlus, 0, false), "+inf");
506 assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, 0, true), "+inf");
507 assert_eq!(to_string(f, 0.0/0.0, Minus, 0, false), "NaN");
508 assert_eq!(to_string(f, 0.0/0.0, MinusRaw, 1, true), "NaN");
509 assert_eq!(to_string(f, 0.0/0.0, MinusPlus, 8, false), "NaN");
510 assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 64, true), "NaN");
511 assert_eq!(to_string(f, -1.0/0.0, Minus, 0, false), "-inf");
512 assert_eq!(to_string(f, -1.0/0.0, MinusRaw, 1, true), "-inf");
513 assert_eq!(to_string(f, -1.0/0.0, MinusPlus, 8, false), "-inf");
514 assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, 64, true), "-inf");
515
516 assert_eq!(to_string(f, 3.14, Minus, 0, false), "3.14");
517 assert_eq!(to_string(f, 3.14, MinusRaw, 0, false), "3.14");
518 assert_eq!(to_string(f, 3.14, MinusPlus, 0, false), "+3.14");
519 assert_eq!(to_string(f, 3.14, MinusPlusRaw, 0, false), "+3.14");
520 assert_eq!(to_string(f, -3.14, Minus, 0, false), "-3.14");
521 assert_eq!(to_string(f, -3.14, MinusRaw, 0, false), "-3.14");
522 assert_eq!(to_string(f, -3.14, MinusPlus, 0, false), "-3.14");
523 assert_eq!(to_string(f, -3.14, MinusPlusRaw, 0, false), "-3.14");
524 assert_eq!(to_string(f, 3.14, Minus, 1, true), "3.14");
525 assert_eq!(to_string(f, 3.14, MinusRaw, 2, true), "3.14");
526 assert_eq!(to_string(f, 3.14, MinusPlus, 3, true), "+3.140");
527 assert_eq!(to_string(f, 3.14, MinusPlusRaw, 4, true), "+3.1400");
528 assert_eq!(to_string(f, -3.14, Minus, 8, true), "-3.14000000");
529 assert_eq!(to_string(f, -3.14, MinusRaw, 8, true), "-3.14000000");
530 assert_eq!(to_string(f, -3.14, MinusPlus, 8, true), "-3.14000000");
531 assert_eq!(to_string(f, -3.14, MinusPlusRaw, 8, true), "-3.14000000");
532
533 assert_eq!(to_string(f, 7.5e-11, Minus, 0, false), "0.000000000075");
534 assert_eq!(to_string(f, 7.5e-11, Minus, 3, false), "0.000000000075");
535 assert_eq!(to_string(f, 7.5e-11, Minus, 12, false), "0.000000000075");
536 assert_eq!(to_string(f, 7.5e-11, Minus, 13, false), "0.0000000000750");
537
538 assert_eq!(to_string(f, 1.9971e20, Minus, 0, false), "199710000000000000000");
539 assert_eq!(to_string(f, 1.9971e20, Minus, 1, false), "199710000000000000000.0");
540 assert_eq!(to_string(f, 1.9971e20, Minus, 8, false), "199710000000000000000.00000000");
541
542 assert_eq!(to_string(f, f32::MAX, Minus, 0, false), format!("34028235{:0>31}", ""));
543 assert_eq!(to_string(f, f32::MAX, Minus, 1, false), format!("34028235{:0>31}.0", ""));
544 assert_eq!(to_string(f, f32::MAX, Minus, 8, false), format!("34028235{:0>31}.00000000", ""));
545
546 let minf32 = ldexp_f32(1.0, -149);
547 assert_eq!(to_string(f, minf32, Minus, 0, false), format!("0.{:0>44}1", ""));
548 assert_eq!(to_string(f, minf32, Minus, 45, false), format!("0.{:0>44}1", ""));
549 assert_eq!(to_string(f, minf32, Minus, 46, false), format!("0.{:0>44}10", ""));
550
551 assert_eq!(to_string(f, f64::MAX, Minus, 0, false),
552 format!("17976931348623157{:0>292}", ""));
553 assert_eq!(to_string(f, f64::MAX, Minus, 1, false),
554 format!("17976931348623157{:0>292}.0", ""));
555 assert_eq!(to_string(f, f64::MAX, Minus, 8, false),
556 format!("17976931348623157{:0>292}.00000000", ""));
557
558 let minf64 = ldexp_f64(1.0, -1074);
559 assert_eq!(to_string(f, minf64, Minus, 0, false), format!("0.{:0>323}5", ""));
560 assert_eq!(to_string(f, minf64, Minus, 324, false), format!("0.{:0>323}5", ""));
561 assert_eq!(to_string(f, minf64, Minus, 325, false), format!("0.{:0>323}50", ""));
562
563 // very large output
564 assert_eq!(to_string(f, 1.1, Minus, 80000, false), format!("1.1{:0>79999}", ""));
565 }
566
567 pub fn to_shortest_exp_str_test<F>(mut f_: F)
568 where F: FnMut(&Decoded, &mut [u8]) -> (usize, i16) {
569 use core::num::flt2dec::Sign::*;
570
571 fn to_string<T, F>(f: &mut F, v: T, sign: Sign, exp_bounds: (i16, i16), upper: bool) -> String
572 where T: DecodableFloat, F: FnMut(&Decoded, &mut [u8]) -> (usize, i16) {
573 to_string_with_parts(|buf, parts| to_shortest_exp_str(|d,b| f(d,b), v, sign,
574 exp_bounds, upper, buf, parts))
575 }
576
577 let f = &mut f_;
578
579 assert_eq!(to_string(f, 0.0, Minus, (-4, 16), false), "0");
580 assert_eq!(to_string(f, 0.0, MinusRaw, (-4, 16), false), "0");
581 assert_eq!(to_string(f, 0.0, MinusPlus, (-4, 16), false), "+0");
582 assert_eq!(to_string(f, 0.0, MinusPlusRaw, (-4, 16), false), "+0");
583 assert_eq!(to_string(f, -0.0, Minus, (-4, 16), false), "0");
584 assert_eq!(to_string(f, -0.0, MinusRaw, (-4, 16), false), "-0");
585 assert_eq!(to_string(f, -0.0, MinusPlus, (-4, 16), false), "+0");
586 assert_eq!(to_string(f, -0.0, MinusPlusRaw, (-4, 16), false), "-0");
587 assert_eq!(to_string(f, 0.0, Minus, ( 0, 0), true), "0E0");
588 assert_eq!(to_string(f, 0.0, MinusRaw, ( 0, 0), false), "0e0");
589 assert_eq!(to_string(f, 0.0, MinusPlus, (-9, -5), true), "+0E0");
590 assert_eq!(to_string(f, 0.0, MinusPlusRaw, ( 5, 9), false), "+0e0");
591 assert_eq!(to_string(f, -0.0, Minus, ( 0, 0), true), "0E0");
592 assert_eq!(to_string(f, -0.0, MinusRaw, ( 0, 0), false), "-0e0");
593 assert_eq!(to_string(f, -0.0, MinusPlus, (-9, -5), true), "+0E0");
594 assert_eq!(to_string(f, -0.0, MinusPlusRaw, ( 5, 9), false), "-0e0");
595
596 assert_eq!(to_string(f, 1.0/0.0, Minus, (-4, 16), false), "inf");
597 assert_eq!(to_string(f, 1.0/0.0, MinusRaw, (-4, 16), true), "inf");
598 assert_eq!(to_string(f, 1.0/0.0, MinusPlus, (-4, 16), false), "+inf");
599 assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, (-4, 16), true), "+inf");
600 assert_eq!(to_string(f, 0.0/0.0, Minus, ( 0, 0), false), "NaN");
601 assert_eq!(to_string(f, 0.0/0.0, MinusRaw, ( 0, 0), true), "NaN");
602 assert_eq!(to_string(f, 0.0/0.0, MinusPlus, (-9, -5), false), "NaN");
603 assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, ( 5, 9), true), "NaN");
604 assert_eq!(to_string(f, -1.0/0.0, Minus, ( 0, 0), false), "-inf");
605 assert_eq!(to_string(f, -1.0/0.0, MinusRaw, ( 0, 0), true), "-inf");
606 assert_eq!(to_string(f, -1.0/0.0, MinusPlus, (-9, -5), false), "-inf");
607 assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, ( 5, 9), true), "-inf");
608
609 assert_eq!(to_string(f, 3.14, Minus, (-4, 16), false), "3.14");
610 assert_eq!(to_string(f, 3.14, MinusRaw, (-4, 16), false), "3.14");
611 assert_eq!(to_string(f, 3.14, MinusPlus, (-4, 16), false), "+3.14");
612 assert_eq!(to_string(f, 3.14, MinusPlusRaw, (-4, 16), false), "+3.14");
613 assert_eq!(to_string(f, -3.14, Minus, (-4, 16), false), "-3.14");
614 assert_eq!(to_string(f, -3.14, MinusRaw, (-4, 16), false), "-3.14");
615 assert_eq!(to_string(f, -3.14, MinusPlus, (-4, 16), false), "-3.14");
616 assert_eq!(to_string(f, -3.14, MinusPlusRaw, (-4, 16), false), "-3.14");
617 assert_eq!(to_string(f, 3.14, Minus, ( 0, 0), true), "3.14E0");
618 assert_eq!(to_string(f, 3.14, MinusRaw, ( 0, 0), false), "3.14e0");
619 assert_eq!(to_string(f, 3.14, MinusPlus, (-9, -5), true), "+3.14E0");
620 assert_eq!(to_string(f, 3.14, MinusPlusRaw, ( 5, 9), false), "+3.14e0");
621 assert_eq!(to_string(f, -3.14, Minus, ( 0, 0), true), "-3.14E0");
622 assert_eq!(to_string(f, -3.14, MinusRaw, ( 0, 0), false), "-3.14e0");
623 assert_eq!(to_string(f, -3.14, MinusPlus, (-9, -5), true), "-3.14E0");
624 assert_eq!(to_string(f, -3.14, MinusPlusRaw, ( 5, 9), false), "-3.14e0");
625
626 assert_eq!(to_string(f, 0.1, Minus, (-4, 16), false), "0.1");
627 assert_eq!(to_string(f, 0.1, MinusRaw, (-4, 16), false), "0.1");
628 assert_eq!(to_string(f, 0.1, MinusPlus, (-4, 16), false), "+0.1");
629 assert_eq!(to_string(f, 0.1, MinusPlusRaw, (-4, 16), false), "+0.1");
630 assert_eq!(to_string(f, -0.1, Minus, (-4, 16), false), "-0.1");
631 assert_eq!(to_string(f, -0.1, MinusRaw, (-4, 16), false), "-0.1");
632 assert_eq!(to_string(f, -0.1, MinusPlus, (-4, 16), false), "-0.1");
633 assert_eq!(to_string(f, -0.1, MinusPlusRaw, (-4, 16), false), "-0.1");
634 assert_eq!(to_string(f, 0.1, Minus, ( 0, 0), true), "1E-1");
635 assert_eq!(to_string(f, 0.1, MinusRaw, ( 0, 0), false), "1e-1");
636 assert_eq!(to_string(f, 0.1, MinusPlus, (-9, -5), true), "+1E-1");
637 assert_eq!(to_string(f, 0.1, MinusPlusRaw, ( 5, 9), false), "+1e-1");
638 assert_eq!(to_string(f, -0.1, Minus, ( 0, 0), true), "-1E-1");
639 assert_eq!(to_string(f, -0.1, MinusRaw, ( 0, 0), false), "-1e-1");
640 assert_eq!(to_string(f, -0.1, MinusPlus, (-9, -5), true), "-1E-1");
641 assert_eq!(to_string(f, -0.1, MinusPlusRaw, ( 5, 9), false), "-1e-1");
642
643 assert_eq!(to_string(f, 7.5e-11, Minus, ( -4, 16), false), "7.5e-11");
644 assert_eq!(to_string(f, 7.5e-11, Minus, (-11, 10), false), "0.000000000075");
645 assert_eq!(to_string(f, 7.5e-11, Minus, (-10, 11), false), "7.5e-11");
646
647 assert_eq!(to_string(f, 1.9971e20, Minus, ( -4, 16), false), "1.9971e20");
648 assert_eq!(to_string(f, 1.9971e20, Minus, (-20, 21), false), "199710000000000000000");
649 assert_eq!(to_string(f, 1.9971e20, Minus, (-21, 20), false), "1.9971e20");
650
651 // the true value of 1.0e23f64 is less than 10^23, but that shouldn't matter here
652 assert_eq!(to_string(f, 1.0e23, Minus, (22, 23), false), "1e23");
653 assert_eq!(to_string(f, 1.0e23, Minus, (23, 24), false), "100000000000000000000000");
654 assert_eq!(to_string(f, 1.0e23, Minus, (24, 25), false), "1e23");
655
656 assert_eq!(to_string(f, f32::MAX, Minus, ( -4, 16), false), "3.4028235e38");
657 assert_eq!(to_string(f, f32::MAX, Minus, (-39, 38), false), "3.4028235e38");
658 assert_eq!(to_string(f, f32::MAX, Minus, (-38, 39), false), format!("34028235{:0>31}", ""));
659
660 let minf32 = ldexp_f32(1.0, -149);
661 assert_eq!(to_string(f, minf32, Minus, ( -4, 16), false), "1e-45");
662 assert_eq!(to_string(f, minf32, Minus, (-44, 45), false), "1e-45");
663 assert_eq!(to_string(f, minf32, Minus, (-45, 44), false), format!("0.{:0>44}1", ""));
664
665 assert_eq!(to_string(f, f64::MAX, Minus, ( -4, 16), false),
666 "1.7976931348623157e308");
667 assert_eq!(to_string(f, f64::MAX, Minus, (-308, 309), false),
668 format!("17976931348623157{:0>292}", ""));
669 assert_eq!(to_string(f, f64::MAX, Minus, (-309, 308), false),
670 "1.7976931348623157e308");
671
672 let minf64 = ldexp_f64(1.0, -1074);
673 assert_eq!(to_string(f, minf64, Minus, ( -4, 16), false), "5e-324");
674 assert_eq!(to_string(f, minf64, Minus, (-324, 323), false), format!("0.{:0>323}5", ""));
675 assert_eq!(to_string(f, minf64, Minus, (-323, 324), false), "5e-324");
676
677 assert_eq!(to_string(f, 1.1, Minus, (i16::MIN, i16::MAX), false), "1.1");
678 }
679
680 pub fn to_exact_exp_str_test<F>(mut f_: F)
681 where F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
682 use core::num::flt2dec::Sign::*;
683
684 fn to_string<T, F>(f: &mut F, v: T, sign: Sign, ndigits: usize, upper: bool) -> String
685 where T: DecodableFloat, F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
686 to_string_with_parts(|buf, parts| to_exact_exp_str(|d,b,l| f(d,b,l), v, sign,
687 ndigits, upper, buf, parts))
688 }
689
690 let f = &mut f_;
691
692 assert_eq!(to_string(f, 0.0, Minus, 1, true), "0E0");
693 assert_eq!(to_string(f, 0.0, MinusRaw, 1, false), "0e0");
694 assert_eq!(to_string(f, 0.0, MinusPlus, 1, true), "+0E0");
695 assert_eq!(to_string(f, 0.0, MinusPlusRaw, 1, false), "+0e0");
696 assert_eq!(to_string(f, -0.0, Minus, 1, true), "0E0");
697 assert_eq!(to_string(f, -0.0, MinusRaw, 1, false), "-0e0");
698 assert_eq!(to_string(f, -0.0, MinusPlus, 1, true), "+0E0");
699 assert_eq!(to_string(f, -0.0, MinusPlusRaw, 1, false), "-0e0");
700 assert_eq!(to_string(f, 0.0, Minus, 2, true), "0.0E0");
701 assert_eq!(to_string(f, 0.0, MinusRaw, 2, false), "0.0e0");
702 assert_eq!(to_string(f, 0.0, MinusPlus, 2, true), "+0.0E0");
703 assert_eq!(to_string(f, 0.0, MinusPlusRaw, 2, false), "+0.0e0");
704 assert_eq!(to_string(f, -0.0, Minus, 8, true), "0.0000000E0");
705 assert_eq!(to_string(f, -0.0, MinusRaw, 8, false), "-0.0000000e0");
706 assert_eq!(to_string(f, -0.0, MinusPlus, 8, true), "+0.0000000E0");
707 assert_eq!(to_string(f, -0.0, MinusPlusRaw, 8, false), "-0.0000000e0");
708
709 assert_eq!(to_string(f, 1.0/0.0, Minus, 1, false), "inf");
710 assert_eq!(to_string(f, 1.0/0.0, MinusRaw, 1, true), "inf");
711 assert_eq!(to_string(f, 1.0/0.0, MinusPlus, 1, false), "+inf");
712 assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, 1, true), "+inf");
713 assert_eq!(to_string(f, 0.0/0.0, Minus, 8, false), "NaN");
714 assert_eq!(to_string(f, 0.0/0.0, MinusRaw, 8, true), "NaN");
715 assert_eq!(to_string(f, 0.0/0.0, MinusPlus, 8, false), "NaN");
716 assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 8, true), "NaN");
717 assert_eq!(to_string(f, -1.0/0.0, Minus, 64, false), "-inf");
718 assert_eq!(to_string(f, -1.0/0.0, MinusRaw, 64, true), "-inf");
719 assert_eq!(to_string(f, -1.0/0.0, MinusPlus, 64, false), "-inf");
720 assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, 64, true), "-inf");
721
722 assert_eq!(to_string(f, 3.14, Minus, 1, true), "3E0");
723 assert_eq!(to_string(f, 3.14, MinusRaw, 1, false), "3e0");
724 assert_eq!(to_string(f, 3.14, MinusPlus, 1, true), "+3E0");
725 assert_eq!(to_string(f, 3.14, MinusPlusRaw, 1, false), "+3e0");
726 assert_eq!(to_string(f, -3.14, Minus, 2, true), "-3.1E0");
727 assert_eq!(to_string(f, -3.14, MinusRaw, 2, false), "-3.1e0");
728 assert_eq!(to_string(f, -3.14, MinusPlus, 2, true), "-3.1E0");
729 assert_eq!(to_string(f, -3.14, MinusPlusRaw, 2, false), "-3.1e0");
730 assert_eq!(to_string(f, 3.14, Minus, 3, true), "3.14E0");
731 assert_eq!(to_string(f, 3.14, MinusRaw, 3, false), "3.14e0");
732 assert_eq!(to_string(f, 3.14, MinusPlus, 3, true), "+3.14E0");
733 assert_eq!(to_string(f, 3.14, MinusPlusRaw, 3, false), "+3.14e0");
734 assert_eq!(to_string(f, -3.14, Minus, 4, true), "-3.140E0");
735 assert_eq!(to_string(f, -3.14, MinusRaw, 4, false), "-3.140e0");
736 assert_eq!(to_string(f, -3.14, MinusPlus, 4, true), "-3.140E0");
737 assert_eq!(to_string(f, -3.14, MinusPlusRaw, 4, false), "-3.140e0");
738
739 assert_eq!(to_string(f, 0.195, Minus, 1, false), "2e-1");
740 assert_eq!(to_string(f, 0.195, MinusRaw, 1, true), "2E-1");
741 assert_eq!(to_string(f, 0.195, MinusPlus, 1, false), "+2e-1");
742 assert_eq!(to_string(f, 0.195, MinusPlusRaw, 1, true), "+2E-1");
743 assert_eq!(to_string(f, -0.195, Minus, 2, false), "-2.0e-1");
744 assert_eq!(to_string(f, -0.195, MinusRaw, 2, true), "-2.0E-1");
745 assert_eq!(to_string(f, -0.195, MinusPlus, 2, false), "-2.0e-1");
746 assert_eq!(to_string(f, -0.195, MinusPlusRaw, 2, true), "-2.0E-1");
747 assert_eq!(to_string(f, 0.195, Minus, 3, false), "1.95e-1");
748 assert_eq!(to_string(f, 0.195, MinusRaw, 3, true), "1.95E-1");
749 assert_eq!(to_string(f, 0.195, MinusPlus, 3, false), "+1.95e-1");
750 assert_eq!(to_string(f, 0.195, MinusPlusRaw, 3, true), "+1.95E-1");
751 assert_eq!(to_string(f, -0.195, Minus, 4, false), "-1.950e-1");
752 assert_eq!(to_string(f, -0.195, MinusRaw, 4, true), "-1.950E-1");
753 assert_eq!(to_string(f, -0.195, MinusPlus, 4, false), "-1.950e-1");
754 assert_eq!(to_string(f, -0.195, MinusPlusRaw, 4, true), "-1.950E-1");
755
756 assert_eq!(to_string(f, 9.5, Minus, 1, false), "1e1");
757 assert_eq!(to_string(f, 9.5, Minus, 2, false), "9.5e0");
758 assert_eq!(to_string(f, 9.5, Minus, 3, false), "9.50e0");
759 assert_eq!(to_string(f, 9.5, Minus, 30, false), "9.50000000000000000000000000000e0");
760
761 assert_eq!(to_string(f, 1.0e25, Minus, 1, false), "1e25");
762 assert_eq!(to_string(f, 1.0e25, Minus, 2, false), "1.0e25");
763 assert_eq!(to_string(f, 1.0e25, Minus, 15, false), "1.00000000000000e25");
764 assert_eq!(to_string(f, 1.0e25, Minus, 16, false), "1.000000000000000e25");
765 assert_eq!(to_string(f, 1.0e25, Minus, 17, false), "1.0000000000000001e25");
766 assert_eq!(to_string(f, 1.0e25, Minus, 18, false), "1.00000000000000009e25");
767 assert_eq!(to_string(f, 1.0e25, Minus, 19, false), "1.000000000000000091e25");
768 assert_eq!(to_string(f, 1.0e25, Minus, 20, false), "1.0000000000000000906e25");
769 assert_eq!(to_string(f, 1.0e25, Minus, 21, false), "1.00000000000000009060e25");
770 assert_eq!(to_string(f, 1.0e25, Minus, 22, false), "1.000000000000000090597e25");
771 assert_eq!(to_string(f, 1.0e25, Minus, 23, false), "1.0000000000000000905970e25");
772 assert_eq!(to_string(f, 1.0e25, Minus, 24, false), "1.00000000000000009059697e25");
773 assert_eq!(to_string(f, 1.0e25, Minus, 25, false), "1.000000000000000090596966e25");
774 assert_eq!(to_string(f, 1.0e25, Minus, 26, false), "1.0000000000000000905969664e25");
775 assert_eq!(to_string(f, 1.0e25, Minus, 27, false), "1.00000000000000009059696640e25");
776 assert_eq!(to_string(f, 1.0e25, Minus, 30, false), "1.00000000000000009059696640000e25");
777
778 assert_eq!(to_string(f, 1.0e-6, Minus, 1, false), "1e-6");
779 assert_eq!(to_string(f, 1.0e-6, Minus, 2, false), "1.0e-6");
780 assert_eq!(to_string(f, 1.0e-6, Minus, 16, false), "1.000000000000000e-6");
781 assert_eq!(to_string(f, 1.0e-6, Minus, 17, false), "9.9999999999999995e-7");
782 assert_eq!(to_string(f, 1.0e-6, Minus, 18, false), "9.99999999999999955e-7");
783 assert_eq!(to_string(f, 1.0e-6, Minus, 19, false), "9.999999999999999547e-7");
784 assert_eq!(to_string(f, 1.0e-6, Minus, 20, false), "9.9999999999999995475e-7");
785 assert_eq!(to_string(f, 1.0e-6, Minus, 30, false), "9.99999999999999954748111825886e-7");
786 assert_eq!(to_string(f, 1.0e-6, Minus, 40, false),
787 "9.999999999999999547481118258862586856139e-7");
788 assert_eq!(to_string(f, 1.0e-6, Minus, 50, false),
789 "9.9999999999999995474811182588625868561393872369081e-7");
790 assert_eq!(to_string(f, 1.0e-6, Minus, 60, false),
791 "9.99999999999999954748111825886258685613938723690807819366455e-7");
792 assert_eq!(to_string(f, 1.0e-6, Minus, 70, false),
793 "9.999999999999999547481118258862586856139387236908078193664550781250000e-7");
794
795 assert_eq!(to_string(f, f32::MAX, Minus, 1, false), "3e38");
796 assert_eq!(to_string(f, f32::MAX, Minus, 2, false), "3.4e38");
797 assert_eq!(to_string(f, f32::MAX, Minus, 4, false), "3.403e38");
798 assert_eq!(to_string(f, f32::MAX, Minus, 8, false), "3.4028235e38");
799 assert_eq!(to_string(f, f32::MAX, Minus, 16, false), "3.402823466385289e38");
800 assert_eq!(to_string(f, f32::MAX, Minus, 32, false), "3.4028234663852885981170418348452e38");
801 assert_eq!(to_string(f, f32::MAX, Minus, 64, false),
802 "3.402823466385288598117041834845169254400000000000000000000000000e38");
803
804 let minf32 = ldexp_f32(1.0, -149);
805 assert_eq!(to_string(f, minf32, Minus, 1, false), "1e-45");
806 assert_eq!(to_string(f, minf32, Minus, 2, false), "1.4e-45");
807 assert_eq!(to_string(f, minf32, Minus, 4, false), "1.401e-45");
808 assert_eq!(to_string(f, minf32, Minus, 8, false), "1.4012985e-45");
809 assert_eq!(to_string(f, minf32, Minus, 16, false), "1.401298464324817e-45");
810 assert_eq!(to_string(f, minf32, Minus, 32, false), "1.4012984643248170709237295832899e-45");
811 assert_eq!(to_string(f, minf32, Minus, 64, false),
812 "1.401298464324817070923729583289916131280261941876515771757068284e-45");
813 assert_eq!(to_string(f, minf32, Minus, 128, false),
814 "1.401298464324817070923729583289916131280261941876515771757068283\
815 8897910826858606014866381883621215820312500000000000000000000000e-45");
816
817 assert_eq!(to_string(f, f64::MAX, Minus, 1, false), "2e308");
818 assert_eq!(to_string(f, f64::MAX, Minus, 2, false), "1.8e308");
819 assert_eq!(to_string(f, f64::MAX, Minus, 4, false), "1.798e308");
820 assert_eq!(to_string(f, f64::MAX, Minus, 8, false), "1.7976931e308");
821 assert_eq!(to_string(f, f64::MAX, Minus, 16, false), "1.797693134862316e308");
822 assert_eq!(to_string(f, f64::MAX, Minus, 32, false), "1.7976931348623157081452742373170e308");
823 assert_eq!(to_string(f, f64::MAX, Minus, 64, false),
824 "1.797693134862315708145274237317043567980705675258449965989174768e308");
825 assert_eq!(to_string(f, f64::MAX, Minus, 128, false),
826 "1.797693134862315708145274237317043567980705675258449965989174768\
827 0315726078002853876058955863276687817154045895351438246423432133e308");
828 assert_eq!(to_string(f, f64::MAX, Minus, 256, false),
829 "1.797693134862315708145274237317043567980705675258449965989174768\
830 0315726078002853876058955863276687817154045895351438246423432132\
831 6889464182768467546703537516986049910576551282076245490090389328\
832 9440758685084551339423045832369032229481658085593321233482747978e308");
833 assert_eq!(to_string(f, f64::MAX, Minus, 512, false),
834 "1.797693134862315708145274237317043567980705675258449965989174768\
835 0315726078002853876058955863276687817154045895351438246423432132\
836 6889464182768467546703537516986049910576551282076245490090389328\
837 9440758685084551339423045832369032229481658085593321233482747978\
838 2620414472316873817718091929988125040402618412485836800000000000\
839 0000000000000000000000000000000000000000000000000000000000000000\
840 0000000000000000000000000000000000000000000000000000000000000000\
841 0000000000000000000000000000000000000000000000000000000000000000e308");
842
843 // okay, this is becoming tough. fortunately for us, this is almost the worst case.
844 let minf64 = ldexp_f64(1.0, -1074);
845 assert_eq!(to_string(f, minf64, Minus, 1, false), "5e-324");
846 assert_eq!(to_string(f, minf64, Minus, 2, false), "4.9e-324");
847 assert_eq!(to_string(f, minf64, Minus, 4, false), "4.941e-324");
848 assert_eq!(to_string(f, minf64, Minus, 8, false), "4.9406565e-324");
849 assert_eq!(to_string(f, minf64, Minus, 16, false), "4.940656458412465e-324");
850 assert_eq!(to_string(f, minf64, Minus, 32, false), "4.9406564584124654417656879286822e-324");
851 assert_eq!(to_string(f, minf64, Minus, 64, false),
852 "4.940656458412465441765687928682213723650598026143247644255856825e-324");
853 assert_eq!(to_string(f, minf64, Minus, 128, false),
854 "4.940656458412465441765687928682213723650598026143247644255856825\
855 0067550727020875186529983636163599237979656469544571773092665671e-324");
856 assert_eq!(to_string(f, minf64, Minus, 256, false),
857 "4.940656458412465441765687928682213723650598026143247644255856825\
858 0067550727020875186529983636163599237979656469544571773092665671\
859 0355939796398774796010781878126300713190311404527845817167848982\
860 1036887186360569987307230500063874091535649843873124733972731696e-324");
861 assert_eq!(to_string(f, minf64, Minus, 512, false),
862 "4.940656458412465441765687928682213723650598026143247644255856825\
863 0067550727020875186529983636163599237979656469544571773092665671\
864 0355939796398774796010781878126300713190311404527845817167848982\
865 1036887186360569987307230500063874091535649843873124733972731696\
866 1514003171538539807412623856559117102665855668676818703956031062\
867 4931945271591492455329305456544401127480129709999541931989409080\
868 4165633245247571478690147267801593552386115501348035264934720193\
869 7902681071074917033322268447533357208324319360923828934583680601e-324");
870 assert_eq!(to_string(f, minf64, Minus, 1024, false),
871 "4.940656458412465441765687928682213723650598026143247644255856825\
872 0067550727020875186529983636163599237979656469544571773092665671\
873 0355939796398774796010781878126300713190311404527845817167848982\
874 1036887186360569987307230500063874091535649843873124733972731696\
875 1514003171538539807412623856559117102665855668676818703956031062\
876 4931945271591492455329305456544401127480129709999541931989409080\
877 4165633245247571478690147267801593552386115501348035264934720193\
878 7902681071074917033322268447533357208324319360923828934583680601\
879 0601150616980975307834227731832924790498252473077637592724787465\
880 6084778203734469699533647017972677717585125660551199131504891101\
881 4510378627381672509558373897335989936648099411642057026370902792\
882 4276754456522908753868250641971826553344726562500000000000000000\
883 0000000000000000000000000000000000000000000000000000000000000000\
884 0000000000000000000000000000000000000000000000000000000000000000\
885 0000000000000000000000000000000000000000000000000000000000000000\
886 0000000000000000000000000000000000000000000000000000000000000000e-324");
887
888 // very large output
889 assert_eq!(to_string(f, 0.0, Minus, 80000, false), format!("0.{:0>79999}e0", ""));
890 assert_eq!(to_string(f, 1.0e1, Minus, 80000, false), format!("1.{:0>79999}e1", ""));
891 assert_eq!(to_string(f, 1.0e0, Minus, 80000, false), format!("1.{:0>79999}e0", ""));
892 assert_eq!(to_string(f, 1.0e-1, Minus, 80000, false),
893 format!("1.000000000000000055511151231257827021181583404541015625{:0>79945}\
894 e-1", ""));
895 assert_eq!(to_string(f, 1.0e-20, Minus, 80000, false),
896 format!("9.999999999999999451532714542095716517295037027873924471077157760\
897 66783064379706047475337982177734375{:0>79901}e-21", ""));
898 }
899
900 pub fn to_exact_fixed_str_test<F>(mut f_: F)
901 where F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
902 use core::num::flt2dec::Sign::*;
903
904 fn to_string<T, F>(f: &mut F, v: T, sign: Sign, frac_digits: usize, upper: bool) -> String
905 where T: DecodableFloat, F: FnMut(&Decoded, &mut [u8], i16) -> (usize, i16) {
906 to_string_with_parts(|buf, parts| to_exact_fixed_str(|d,b,l| f(d,b,l), v, sign,
907 frac_digits, upper, buf, parts))
908 }
909
910 let f = &mut f_;
911
912 assert_eq!(to_string(f, 0.0, Minus, 0, false), "0");
913 assert_eq!(to_string(f, 0.0, MinusRaw, 0, false), "0");
914 assert_eq!(to_string(f, 0.0, MinusPlus, 0, false), "+0");
915 assert_eq!(to_string(f, 0.0, MinusPlusRaw, 0, false), "+0");
916 assert_eq!(to_string(f, -0.0, Minus, 0, false), "0");
917 assert_eq!(to_string(f, -0.0, MinusRaw, 0, false), "-0");
918 assert_eq!(to_string(f, -0.0, MinusPlus, 0, false), "+0");
919 assert_eq!(to_string(f, -0.0, MinusPlusRaw, 0, false), "-0");
920 assert_eq!(to_string(f, 0.0, Minus, 1, true), "0.0");
921 assert_eq!(to_string(f, 0.0, MinusRaw, 1, true), "0.0");
922 assert_eq!(to_string(f, 0.0, MinusPlus, 1, true), "+0.0");
923 assert_eq!(to_string(f, 0.0, MinusPlusRaw, 1, true), "+0.0");
924 assert_eq!(to_string(f, -0.0, Minus, 8, true), "0.00000000");
925 assert_eq!(to_string(f, -0.0, MinusRaw, 8, true), "-0.00000000");
926 assert_eq!(to_string(f, -0.0, MinusPlus, 8, true), "+0.00000000");
927 assert_eq!(to_string(f, -0.0, MinusPlusRaw, 8, true), "-0.00000000");
928
929 assert_eq!(to_string(f, 1.0/0.0, Minus, 0, false), "inf");
930 assert_eq!(to_string(f, 1.0/0.0, MinusRaw, 1, true), "inf");
931 assert_eq!(to_string(f, 1.0/0.0, MinusPlus, 8, false), "+inf");
932 assert_eq!(to_string(f, 1.0/0.0, MinusPlusRaw, 64, true), "+inf");
933 assert_eq!(to_string(f, 0.0/0.0, Minus, 0, false), "NaN");
934 assert_eq!(to_string(f, 0.0/0.0, MinusRaw, 1, true), "NaN");
935 assert_eq!(to_string(f, 0.0/0.0, MinusPlus, 8, false), "NaN");
936 assert_eq!(to_string(f, 0.0/0.0, MinusPlusRaw, 64, true), "NaN");
937 assert_eq!(to_string(f, -1.0/0.0, Minus, 0, false), "-inf");
938 assert_eq!(to_string(f, -1.0/0.0, MinusRaw, 1, true), "-inf");
939 assert_eq!(to_string(f, -1.0/0.0, MinusPlus, 8, false), "-inf");
940 assert_eq!(to_string(f, -1.0/0.0, MinusPlusRaw, 64, true), "-inf");
941
942 assert_eq!(to_string(f, 3.14, Minus, 0, false), "3");
943 assert_eq!(to_string(f, 3.14, MinusRaw, 0, false), "3");
944 assert_eq!(to_string(f, 3.14, MinusPlus, 0, false), "+3");
945 assert_eq!(to_string(f, 3.14, MinusPlusRaw, 0, false), "+3");
946 assert_eq!(to_string(f, -3.14, Minus, 0, false), "-3");
947 assert_eq!(to_string(f, -3.14, MinusRaw, 0, false), "-3");
948 assert_eq!(to_string(f, -3.14, MinusPlus, 0, false), "-3");
949 assert_eq!(to_string(f, -3.14, MinusPlusRaw, 0, false), "-3");
950 assert_eq!(to_string(f, 3.14, Minus, 1, true), "3.1");
951 assert_eq!(to_string(f, 3.14, MinusRaw, 2, true), "3.14");
952 assert_eq!(to_string(f, 3.14, MinusPlus, 3, true), "+3.140");
953 assert_eq!(to_string(f, 3.14, MinusPlusRaw, 4, true), "+3.1400");
954 assert_eq!(to_string(f, -3.14, Minus, 8, true), "-3.14000000");
955 assert_eq!(to_string(f, -3.14, MinusRaw, 8, true), "-3.14000000");
956 assert_eq!(to_string(f, -3.14, MinusPlus, 8, true), "-3.14000000");
957 assert_eq!(to_string(f, -3.14, MinusPlusRaw, 8, true), "-3.14000000");
958
959 assert_eq!(to_string(f, 0.195, Minus, 0, false), "0");
960 assert_eq!(to_string(f, 0.195, MinusRaw, 0, false), "0");
961 assert_eq!(to_string(f, 0.195, MinusPlus, 0, false), "+0");
962 assert_eq!(to_string(f, 0.195, MinusPlusRaw, 0, false), "+0");
963 assert_eq!(to_string(f, -0.195, Minus, 0, false), "-0");
964 assert_eq!(to_string(f, -0.195, MinusRaw, 0, false), "-0");
965 assert_eq!(to_string(f, -0.195, MinusPlus, 0, false), "-0");
966 assert_eq!(to_string(f, -0.195, MinusPlusRaw, 0, false), "-0");
967 assert_eq!(to_string(f, 0.195, Minus, 1, true), "0.2");
968 assert_eq!(to_string(f, 0.195, MinusRaw, 2, true), "0.20");
969 assert_eq!(to_string(f, 0.195, MinusPlus, 3, true), "+0.195");
970 assert_eq!(to_string(f, 0.195, MinusPlusRaw, 4, true), "+0.1950");
971 assert_eq!(to_string(f, -0.195, Minus, 5, true), "-0.19500");
972 assert_eq!(to_string(f, -0.195, MinusRaw, 6, true), "-0.195000");
973 assert_eq!(to_string(f, -0.195, MinusPlus, 7, true), "-0.1950000");
974 assert_eq!(to_string(f, -0.195, MinusPlusRaw, 8, true), "-0.19500000");
975
976 assert_eq!(to_string(f, 999.5, Minus, 0, false), "1000");
977 assert_eq!(to_string(f, 999.5, Minus, 1, false), "999.5");
978 assert_eq!(to_string(f, 999.5, Minus, 2, false), "999.50");
979 assert_eq!(to_string(f, 999.5, Minus, 3, false), "999.500");
980 assert_eq!(to_string(f, 999.5, Minus, 30, false), "999.500000000000000000000000000000");
981
982 assert_eq!(to_string(f, 0.5, Minus, 0, false), "1");
983 assert_eq!(to_string(f, 0.5, Minus, 1, false), "0.5");
984 assert_eq!(to_string(f, 0.5, Minus, 2, false), "0.50");
985 assert_eq!(to_string(f, 0.5, Minus, 3, false), "0.500");
986
987 assert_eq!(to_string(f, 0.95, Minus, 0, false), "1");
988 assert_eq!(to_string(f, 0.95, Minus, 1, false), "0.9"); // because it really is less than 0.95
989 assert_eq!(to_string(f, 0.95, Minus, 2, false), "0.95");
990 assert_eq!(to_string(f, 0.95, Minus, 3, false), "0.950");
991 assert_eq!(to_string(f, 0.95, Minus, 10, false), "0.9500000000");
992 assert_eq!(to_string(f, 0.95, Minus, 30, false), "0.949999999999999955591079014994");
993
994 assert_eq!(to_string(f, 0.095, Minus, 0, false), "0");
995 assert_eq!(to_string(f, 0.095, Minus, 1, false), "0.1");
996 assert_eq!(to_string(f, 0.095, Minus, 2, false), "0.10");
997 assert_eq!(to_string(f, 0.095, Minus, 3, false), "0.095");
998 assert_eq!(to_string(f, 0.095, Minus, 4, false), "0.0950");
999 assert_eq!(to_string(f, 0.095, Minus, 10, false), "0.0950000000");
1000 assert_eq!(to_string(f, 0.095, Minus, 30, false), "0.095000000000000001110223024625");
1001
1002 assert_eq!(to_string(f, 0.0095, Minus, 0, false), "0");
1003 assert_eq!(to_string(f, 0.0095, Minus, 1, false), "0.0");
1004 assert_eq!(to_string(f, 0.0095, Minus, 2, false), "0.01");
1005 assert_eq!(to_string(f, 0.0095, Minus, 3, false), "0.009"); // really is less than 0.0095
1006 assert_eq!(to_string(f, 0.0095, Minus, 4, false), "0.0095");
1007 assert_eq!(to_string(f, 0.0095, Minus, 5, false), "0.00950");
1008 assert_eq!(to_string(f, 0.0095, Minus, 10, false), "0.0095000000");
1009 assert_eq!(to_string(f, 0.0095, Minus, 30, false), "0.009499999999999999764077607267");
1010
1011 assert_eq!(to_string(f, 7.5e-11, Minus, 0, false), "0");
1012 assert_eq!(to_string(f, 7.5e-11, Minus, 3, false), "0.000");
1013 assert_eq!(to_string(f, 7.5e-11, Minus, 10, false), "0.0000000001");
1014 assert_eq!(to_string(f, 7.5e-11, Minus, 11, false), "0.00000000007"); // ditto
1015 assert_eq!(to_string(f, 7.5e-11, Minus, 12, false), "0.000000000075");
1016 assert_eq!(to_string(f, 7.5e-11, Minus, 13, false), "0.0000000000750");
1017 assert_eq!(to_string(f, 7.5e-11, Minus, 20, false), "0.00000000007500000000");
1018 assert_eq!(to_string(f, 7.5e-11, Minus, 30, false), "0.000000000074999999999999999501");
1019
1020 assert_eq!(to_string(f, 1.0e25, Minus, 0, false), "10000000000000000905969664");
1021 assert_eq!(to_string(f, 1.0e25, Minus, 1, false), "10000000000000000905969664.0");
1022 assert_eq!(to_string(f, 1.0e25, Minus, 3, false), "10000000000000000905969664.000");
1023
1024 assert_eq!(to_string(f, 1.0e-6, Minus, 0, false), "0");
1025 assert_eq!(to_string(f, 1.0e-6, Minus, 3, false), "0.000");
1026 assert_eq!(to_string(f, 1.0e-6, Minus, 6, false), "0.000001");
1027 assert_eq!(to_string(f, 1.0e-6, Minus, 9, false), "0.000001000");
1028 assert_eq!(to_string(f, 1.0e-6, Minus, 12, false), "0.000001000000");
1029 assert_eq!(to_string(f, 1.0e-6, Minus, 22, false), "0.0000010000000000000000");
1030 assert_eq!(to_string(f, 1.0e-6, Minus, 23, false), "0.00000099999999999999995");
1031 assert_eq!(to_string(f, 1.0e-6, Minus, 24, false), "0.000000999999999999999955");
1032 assert_eq!(to_string(f, 1.0e-6, Minus, 25, false), "0.0000009999999999999999547");
1033 assert_eq!(to_string(f, 1.0e-6, Minus, 35, false), "0.00000099999999999999995474811182589");
1034 assert_eq!(to_string(f, 1.0e-6, Minus, 45, false),
1035 "0.000000999999999999999954748111825886258685614");
1036 assert_eq!(to_string(f, 1.0e-6, Minus, 55, false),
1037 "0.0000009999999999999999547481118258862586856139387236908");
1038 assert_eq!(to_string(f, 1.0e-6, Minus, 65, false),
1039 "0.00000099999999999999995474811182588625868561393872369080781936646");
1040 assert_eq!(to_string(f, 1.0e-6, Minus, 75, false),
1041 "0.000000999999999999999954748111825886258685613938723690807819366455078125000");
1042
1043 assert_eq!(to_string(f, f32::MAX, Minus, 0, false),
1044 "340282346638528859811704183484516925440");
1045 assert_eq!(to_string(f, f32::MAX, Minus, 1, false),
1046 "340282346638528859811704183484516925440.0");
1047 assert_eq!(to_string(f, f32::MAX, Minus, 2, false),
1048 "340282346638528859811704183484516925440.00");
1049
1050 let minf32 = ldexp_f32(1.0, -149);
1051 assert_eq!(to_string(f, minf32, Minus, 0, false), "0");
1052 assert_eq!(to_string(f, minf32, Minus, 1, false), "0.0");
1053 assert_eq!(to_string(f, minf32, Minus, 2, false), "0.00");
1054 assert_eq!(to_string(f, minf32, Minus, 4, false), "0.0000");
1055 assert_eq!(to_string(f, minf32, Minus, 8, false), "0.00000000");
1056 assert_eq!(to_string(f, minf32, Minus, 16, false), "0.0000000000000000");
1057 assert_eq!(to_string(f, minf32, Minus, 32, false), "0.00000000000000000000000000000000");
1058 assert_eq!(to_string(f, minf32, Minus, 64, false),
1059 "0.0000000000000000000000000000000000000000000014012984643248170709");
1060 assert_eq!(to_string(f, minf32, Minus, 128, false),
1061 "0.0000000000000000000000000000000000000000000014012984643248170709\
1062 2372958328991613128026194187651577175706828388979108268586060149");
1063 assert_eq!(to_string(f, minf32, Minus, 256, false),
1064 "0.0000000000000000000000000000000000000000000014012984643248170709\
1065 2372958328991613128026194187651577175706828388979108268586060148\
1066 6638188362121582031250000000000000000000000000000000000000000000\
1067 0000000000000000000000000000000000000000000000000000000000000000");
1068
1069 assert_eq!(to_string(f, f64::MAX, Minus, 0, false),
1070 "1797693134862315708145274237317043567980705675258449965989174768\
1071 0315726078002853876058955863276687817154045895351438246423432132\
1072 6889464182768467546703537516986049910576551282076245490090389328\
1073 9440758685084551339423045832369032229481658085593321233482747978\
1074 26204144723168738177180919299881250404026184124858368");
1075 assert_eq!(to_string(f, f64::MAX, Minus, 10, false),
1076 "1797693134862315708145274237317043567980705675258449965989174768\
1077 0315726078002853876058955863276687817154045895351438246423432132\
1078 6889464182768467546703537516986049910576551282076245490090389328\
1079 9440758685084551339423045832369032229481658085593321233482747978\
1080 26204144723168738177180919299881250404026184124858368.0000000000");
1081
1082 let minf64 = ldexp_f64(1.0, -1074);
1083 assert_eq!(to_string(f, minf64, Minus, 0, false), "0");
1084 assert_eq!(to_string(f, minf64, Minus, 1, false), "0.0");
1085 assert_eq!(to_string(f, minf64, Minus, 10, false), "0.0000000000");
1086 assert_eq!(to_string(f, minf64, Minus, 100, false),
1087 "0.0000000000000000000000000000000000000000000000000000000000000000\
1088 000000000000000000000000000000000000");
1089 assert_eq!(to_string(f, minf64, Minus, 1000, false),
1090 "0.0000000000000000000000000000000000000000000000000000000000000000\
1091 0000000000000000000000000000000000000000000000000000000000000000\
1092 0000000000000000000000000000000000000000000000000000000000000000\
1093 0000000000000000000000000000000000000000000000000000000000000000\
1094 0000000000000000000000000000000000000000000000000000000000000000\
1095 0004940656458412465441765687928682213723650598026143247644255856\
1096 8250067550727020875186529983636163599237979656469544571773092665\
1097 6710355939796398774796010781878126300713190311404527845817167848\
1098 9821036887186360569987307230500063874091535649843873124733972731\
1099 6961514003171538539807412623856559117102665855668676818703956031\
1100 0624931945271591492455329305456544401127480129709999541931989409\
1101 0804165633245247571478690147267801593552386115501348035264934720\
1102 1937902681071074917033322268447533357208324319360923828934583680\
1103 6010601150616980975307834227731832924790498252473077637592724787\
1104 4656084778203734469699533647017972677717585125660551199131504891\
1105 1014510378627381672509558373897335989937");
1106
1107 // very large output
1108 assert_eq!(to_string(f, 0.0, Minus, 80000, false), format!("0.{:0>80000}", ""));
1109 assert_eq!(to_string(f, 1.0e1, Minus, 80000, false), format!("10.{:0>80000}", ""));
1110 assert_eq!(to_string(f, 1.0e0, Minus, 80000, false), format!("1.{:0>80000}", ""));
1111 assert_eq!(to_string(f, 1.0e-1, Minus, 80000, false),
1112 format!("0.1000000000000000055511151231257827021181583404541015625{:0>79945}", ""));
1113 assert_eq!(to_string(f, 1.0e-20, Minus, 80000, false),
1114 format!("0.0000000000000000000099999999999999994515327145420957165172950370\
1115 2787392447107715776066783064379706047475337982177734375{:0>79881}", ""));
1116 }
1117