Debian Codegraph @

libphobos/src/std/format/internal/floats.d

raw
1 // Written in the D programming language.
2
3 /*
4 Helper functions for formatting floating point numbers.
5
6 Copyright: Copyright The D Language Foundation 2019 -
7
8 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
9
10 Authors: Bernhard Seckinger
11
12 Source: $(PHOBOSSRC std/format/internal/floats.d)
13 */
14
15 module std.format.internal.floats;
16
17 import std.format.spec : FormatSpec;
18
19 // wrapper for unittests
20 private auto printFloat(T, Char)(const(T) val, FormatSpec!Char f)
21 if (is(T == float) || is(T == double)
22 || (is(T == real) && (T.mant_dig == double.mant_dig || T.mant_dig == 64)))
23 {
24 import std.array : appender;
25 auto w = appender!string();
26
27 printFloat(w, val, f);
28 return w.data;
29 }
30
31 /// Returns: whether `c` is a supported format specifier for floats
32 package(std.format) bool isFloatSpec(char c) nothrow @nogc pure @safe
33 {
34 return c == 'a' || c == 'A'
35 || c == 'e' || c == 'E'
36 || c == 'f' || c == 'F'
37 || c == 'g' || c == 'G';
38 }
39
40 package(std.format) void printFloat(Writer, T, Char)(auto ref Writer w, const(T) val, FormatSpec!Char f)
41 if (is(T == float) || is(T == double)
42 || (is(T == real) && (T.mant_dig == double.mant_dig || T.mant_dig == 64)))
43 {
44 import std.math.operations : extractBitpattern, FloatingPointBitpattern;
45
46 auto bp = extractBitpattern(val);
47
48 ulong mnt = bp.mantissa;
49 int exp = bp.exponent;
50 string sgn = bp.negative ? "-" : "";
51
52 if (sgn == "" && f.flPlus) sgn = "+";
53 if (sgn == "" && f.flSpace) sgn = " ";
54
55 assert(isFloatSpec(f.spec), "unsupported format specifier");
56 bool is_upper = f.spec == 'A' || f.spec == 'E' || f.spec=='F' || f.spec=='G';
57
58 // special treatment for nan and inf
59 if (exp == T.max_exp)
60 {
61 import std.format.internal.write : writeAligned;
62
63 f.flZero = false;
64 writeAligned(w, sgn, "", (mnt == 0) ? ( is_upper ? "INF" : "inf" ) : ( is_upper ? "NAN" : "nan" ), f);
65 return;
66 }
67
68 final switch (f.spec)
69 {
70 case 'a': case 'A':
71 printFloatA(w, val, f, sgn, exp, mnt, is_upper);
72 break;
73 case 'e': case 'E':
74 printFloatE!false(w, val, f, sgn, exp, mnt, is_upper);
75 break;
76 case 'f': case 'F':
77 printFloatF!false(w, val, f, sgn, exp, mnt, is_upper);
78 break;
79 case 'g': case 'G':
80 printFloatG(w, val, f, sgn, exp, mnt, is_upper);
81 break;
82 }
83 }
84
85 private void printFloatA(Writer, T, Char)(auto ref Writer w, const(T) val,
86 FormatSpec!Char f, string sgn, int exp, ulong mnt, bool is_upper)
87 if (is(T == float) || is(T == double)
88 || (is(T == real) && (T.mant_dig == double.mant_dig || T.mant_dig == 64)))
89 {
90 import std.algorithm.comparison : max;
91 import std.format.internal.write : writeAligned, PrecisionType;
92
93 char[3] prefix;
94 if (sgn != "") prefix[0] = sgn[0];
95 prefix[1] = '0';
96 prefix[2] = is_upper ? 'X' : 'x';
97
98 // print exponent
99 if (mnt == 0)
100 {
101 if (f.precision == f.UNSPECIFIED)
102 f.precision = 0;
103 writeAligned(w, prefix[1 - sgn.length .. $], "0", ".", is_upper ? "P+0" : "p+0",
104 f, PrecisionType.fractionalDigits);
105 return;
106 }
107
108 // save integer part
109 char first = '0' + ((mnt >> (T.mant_dig - 1)) & 1);
110 mnt &= (1L << (T.mant_dig - 1)) - 1;
111
112 static if (is(T == float) || (is(T == real) && T.mant_dig == 64))
113 {
114 mnt <<= 1; // make mnt dividable by 4
115 enum mant_len = T.mant_dig;
116 }
117 else
118 enum mant_len = T.mant_dig - 1;
119 static assert(mant_len % 4 == 0, "mantissa with wrong length");
120
121 // print full mantissa
122 char[(mant_len - 1) / 4 + 3] hex_mant;
123 size_t hex_mant_pos = 2;
124 size_t pos = mant_len;
125
126 auto gap = 39 - 32 * is_upper;
127 while (pos >= 4 && (mnt & (((1L << (pos - 1)) - 1) << 1) + 1) != 0)
128 {
129 pos -= 4;
130 size_t tmp = (mnt >> pos) & 15;
131 // For speed reasons the better readable
132 // ... = tmp < 10 ? ('0' + tmp) : ((is_upper ? 'A' : 'a') + tmp - 10))
133 // has been replaced with an expression without branches, doing the same
134 hex_mant[hex_mant_pos++] = cast(char) (tmp + gap * ((tmp + 6) >> 4) + '0');
135 }
136 hex_mant[0] = first;
137 hex_mant[1] = '.';
138
139 if (f.precision == f.UNSPECIFIED)
140 f.precision = cast(int) hex_mant_pos - 2;
141
142 auto exp_sgn = exp >= 0 ? '+' : '-';
143 if (exp < 0) exp = -exp;
144
145 static if (is(T == real) && real.mant_dig == 64)
146 enum max_exp_digits = 8;
147 else static if (is(T == float))
148 enum max_exp_digits = 5;
149 else
150 enum max_exp_digits = 6;
151
152 char[max_exp_digits] exp_str;
153 size_t exp_pos = max_exp_digits;
154
155 do
156 {
157 exp_str[--exp_pos] = '0' + exp % 10;
158 exp /= 10;
159 } while (exp > 0);
160
161 exp_str[--exp_pos] = exp_sgn;
162 exp_str[--exp_pos] = is_upper ? 'P' : 'p';
163
164 if (f.precision < hex_mant_pos - 2)
165 {
166 import std.format.internal.write : RoundingClass, round;
167
168 RoundingClass rc;
169
170 if (hex_mant[f.precision + 2] == '0')
171 rc = RoundingClass.ZERO;
172 else if (hex_mant[f.precision + 2] < '8')
173 rc = RoundingClass.LOWER;
174 else if (hex_mant[f.precision + 2] > '8')
175 rc = RoundingClass.UPPER;
176 else
177 rc = RoundingClass.FIVE;
178
179 if (rc == RoundingClass.ZERO || rc == RoundingClass.FIVE)
180 {
181 foreach (i;f.precision + 3 .. hex_mant_pos)
182 {
183 if (hex_mant[i] > '0')
184 {
185 rc = rc == RoundingClass.ZERO ? RoundingClass.LOWER : RoundingClass.UPPER;
186 break;
187 }
188 }
189 }
190
191 hex_mant_pos = f.precision + 2;
192
193 round(hex_mant, 0, hex_mant_pos, rc, sgn == "-", is_upper ? 'F' : 'f');
194 }
195
196 writeAligned(w, prefix[1 - sgn.length .. $], hex_mant[0 .. 1], hex_mant[1 .. hex_mant_pos],
197 exp_str[exp_pos .. $], f, PrecisionType.fractionalDigits);
198 }
199
200 @safe unittest
201 {
202 auto f = FormatSpec!dchar("");
203 f.spec = 'a';
204 assert(printFloat(float.nan, f) == "nan");
205 assert(printFloat(-float.nan, f) == "-nan");
206 assert(printFloat(float.infinity, f) == "inf");
207 assert(printFloat(-float.infinity, f) == "-inf");
208 assert(printFloat(0.0f, f) == "0x0p+0");
209 assert(printFloat(-0.0f, f) == "-0x0p+0");
210
211 assert(printFloat(double.nan, f) == "nan");
212 assert(printFloat(-double.nan, f) == "-nan");
213 assert(printFloat(double.infinity, f) == "inf");
214 assert(printFloat(-double.infinity, f) == "-inf");
215 assert(printFloat(0.0, f) == "0x0p+0");
216 assert(printFloat(-0.0, f) == "-0x0p+0");
217
218 static if (real.mant_dig > 64)
219 {
220 pragma(msg, "printFloat tests disabled because of unsupported `real` format");
221 }
222 else
223 {
224 assert(printFloat(real.nan, f) == "nan");
225 assert(printFloat(-real.nan, f) == "-nan");
226 assert(printFloat(real.infinity, f) == "inf");
227 assert(printFloat(-real.infinity, f) == "-inf");
228 assert(printFloat(0.0L, f) == "0x0p+0");
229 assert(printFloat(-0.0L, f) == "-0x0p+0");
230 }
231
232 import std.math.operations : nextUp;
233
234 assert(printFloat(nextUp(0.0f), f) == "0x0.000002p-126");
235 assert(printFloat(float.epsilon, f) == "0x1p-23");
236 assert(printFloat(float.min_normal, f) == "0x1p-126");
237 assert(printFloat(float.max, f) == "0x1.fffffep+127");
238
239 assert(printFloat(nextUp(0.0), f) == "0x0.0000000000001p-1022");
240 assert(printFloat(double.epsilon, f) == "0x1p-52");
241 assert(printFloat(double.min_normal, f) == "0x1p-1022");
242 assert(printFloat(double.max, f) == "0x1.fffffffffffffp+1023");
243
244 static if (real.mant_dig == 64)
245 {
246 assert(printFloat(nextUp(0.0L), f) == "0x0.0000000000000002p-16382");
247 assert(printFloat(real.epsilon, f) == "0x1p-63");
248 assert(printFloat(real.min_normal, f) == "0x1p-16382");
249 assert(printFloat(real.max, f) == "0x1.fffffffffffffffep+16383");
250 }
251
252 import std.math.constants : E, PI, PI_2, PI_4, M_1_PI, M_2_PI, M_2_SQRTPI,
253 LN10, LN2, LOG2, LOG2E, LOG2T, LOG10E, SQRT2, SQRT1_2;
254
255 assert(printFloat(cast(float) E, f) == "0x1.5bf0a8p+1");
256 assert(printFloat(cast(float) PI, f) == "0x1.921fb6p+1");
257 assert(printFloat(cast(float) PI_2, f) == "0x1.921fb6p+0");
258 assert(printFloat(cast(float) PI_4, f) == "0x1.921fb6p-1");
259 assert(printFloat(cast(float) M_1_PI, f) == "0x1.45f306p-2");
260 assert(printFloat(cast(float) M_2_PI, f) == "0x1.45f306p-1");
261 assert(printFloat(cast(float) M_2_SQRTPI, f) == "0x1.20dd76p+0");
262 assert(printFloat(cast(float) LN10, f) == "0x1.26bb1cp+1");
263 assert(printFloat(cast(float) LN2, f) == "0x1.62e43p-1");
264 assert(printFloat(cast(float) LOG2, f) == "0x1.344136p-2");
265 assert(printFloat(cast(float) LOG2E, f) == "0x1.715476p+0");
266 assert(printFloat(cast(float) LOG2T, f) == "0x1.a934fp+1");
267 assert(printFloat(cast(float) LOG10E, f) == "0x1.bcb7b2p-2");
268 assert(printFloat(cast(float) SQRT2, f) == "0x1.6a09e6p+0");
269 assert(printFloat(cast(float) SQRT1_2, f) == "0x1.6a09e6p-1");
270
271 assert(printFloat(cast(double) E, f) == "0x1.5bf0a8b145769p+1");
272 assert(printFloat(cast(double) PI, f) == "0x1.921fb54442d18p+1");
273 assert(printFloat(cast(double) PI_2, f) == "0x1.921fb54442d18p+0");
274 assert(printFloat(cast(double) PI_4, f) == "0x1.921fb54442d18p-1");
275 assert(printFloat(cast(double) M_1_PI, f) == "0x1.45f306dc9c883p-2");
276 assert(printFloat(cast(double) M_2_PI, f) == "0x1.45f306dc9c883p-1");
277 assert(printFloat(cast(double) M_2_SQRTPI, f) == "0x1.20dd750429b6dp+0");
278 assert(printFloat(cast(double) LN10, f) == "0x1.26bb1bbb55516p+1");
279 assert(printFloat(cast(double) LN2, f) == "0x1.62e42fefa39efp-1");
280 assert(printFloat(cast(double) LOG2, f) == "0x1.34413509f79ffp-2");
281 assert(printFloat(cast(double) LOG2E, f) == "0x1.71547652b82fep+0");
282 assert(printFloat(cast(double) LOG2T, f) == "0x1.a934f0979a371p+1");
283 assert(printFloat(cast(double) LOG10E, f) == "0x1.bcb7b1526e50ep-2");
284 assert(printFloat(cast(double) SQRT2, f) == "0x1.6a09e667f3bcdp+0");
285 assert(printFloat(cast(double) SQRT1_2, f) == "0x1.6a09e667f3bcdp-1");
286
287 static if (real.mant_dig == 64)
288 {
289 assert(printFloat(E, f) == "0x1.5bf0a8b145769536p+1");
290 assert(printFloat(PI, f) == "0x1.921fb54442d1846ap+1");
291 assert(printFloat(PI_2, f) == "0x1.921fb54442d1846ap+0");
292 assert(printFloat(PI_4, f) == "0x1.921fb54442d1846ap-1");
293 assert(printFloat(M_1_PI, f) == "0x1.45f306dc9c882a54p-2");
294 assert(printFloat(M_2_PI, f) == "0x1.45f306dc9c882a54p-1");
295 assert(printFloat(M_2_SQRTPI, f) == "0x1.20dd750429b6d11ap+0");
296 assert(printFloat(LN10, f) == "0x1.26bb1bbb5551582ep+1");
297 assert(printFloat(LN2, f) == "0x1.62e42fefa39ef358p-1");
298 assert(printFloat(LOG2, f) == "0x1.34413509f79fef32p-2");
299 assert(printFloat(LOG2E, f) == "0x1.71547652b82fe178p+0");
300 assert(printFloat(LOG2T, f) == "0x1.a934f0979a3715fcp+1");
301 assert(printFloat(LOG10E, f) == "0x1.bcb7b1526e50e32ap-2");
302 assert(printFloat(SQRT2, f) == "0x1.6a09e667f3bcc908p+0");
303 assert(printFloat(SQRT1_2, f) == "0x1.6a09e667f3bcc908p-1");
304 }
305 }
306
307 @safe unittest
308 {
309 auto f = FormatSpec!dchar("");
310 f.spec = 'a';
311 f.precision = 3;
312
313 assert(printFloat(1.0f, f) == "0x1.000p+0");
314 assert(printFloat(3.3f, f) == "0x1.a66p+1");
315 assert(printFloat(2.9f, f) == "0x1.733p+1");
316
317 assert(printFloat(1.0, f) == "0x1.000p+0");
318 assert(printFloat(3.3, f) == "0x1.a66p+1");
319 assert(printFloat(2.9, f) == "0x1.733p+1");
320
321 static if (real.mant_dig == 64)
322 {
323 assert(printFloat(1.0L, f) == "0x1.000p+0");
324 assert(printFloat(3.3L, f) == "0x1.a66p+1");
325 assert(printFloat(2.9L, f) == "0x1.733p+1");
326 }
327 }
328
329 @safe unittest
330 {
331 auto f = FormatSpec!dchar("");
332 f.spec = 'a';
333 f.precision = 0;
334
335 assert(printFloat(1.0f, f) == "0x1p+0");
336 assert(printFloat(3.3f, f) == "0x2p+1");
337 assert(printFloat(2.9f, f) == "0x1p+1");
338
339 assert(printFloat(1.0, f) == "0x1p+0");
340 assert(printFloat(3.3, f) == "0x2p+1");
341 assert(printFloat(2.9, f) == "0x1p+1");
342
343 static if (real.mant_dig == 64)
344 {
345 assert(printFloat(1.0L, f) == "0x1p+0");
346 assert(printFloat(3.3L, f) == "0x2p+1");
347 assert(printFloat(2.9L, f) == "0x1p+1");
348 }
349 }
350
351 @safe unittest
352 {
353 auto f = FormatSpec!dchar("");
354 f.spec = 'a';
355 f.precision = 0;
356 f.flHash = true;
357
358 assert(printFloat(1.0f, f) == "0x1.p+0");
359 assert(printFloat(3.3f, f) == "0x2.p+1");
360 assert(printFloat(2.9f, f) == "0x1.p+1");
361
362 assert(printFloat(1.0, f) == "0x1.p+0");
363 assert(printFloat(3.3, f) == "0x2.p+1");
364 assert(printFloat(2.9, f) == "0x1.p+1");
365
366 static if (real.mant_dig == 64)
367 {
368 assert(printFloat(1.0L, f) == "0x1.p+0");
369 assert(printFloat(3.3L, f) == "0x2.p+1");
370 assert(printFloat(2.9L, f) == "0x1.p+1");
371 }
372 }
373
374 @safe unittest
375 {
376 auto f = FormatSpec!dchar("");
377 f.spec = 'a';
378 f.width = 22;
379
380 assert(printFloat(1.0f, f) == " 0x1p+0");
381 assert(printFloat(3.3f, f) == " 0x1.a66666p+1");
382 assert(printFloat(2.9f, f) == " 0x1.733334p+1");
383
384 assert(printFloat(1.0, f) == " 0x1p+0");
385 assert(printFloat(3.3, f) == " 0x1.a666666666666p+1");
386 assert(printFloat(2.9, f) == " 0x1.7333333333333p+1");
387
388 static if (real.mant_dig == 64)
389 {
390 f.width = 25;
391 assert(printFloat(1.0L, f) == " 0x1p+0");
392 assert(printFloat(3.3L, f) == " 0x1.a666666666666666p+1");
393 assert(printFloat(2.9L, f) == " 0x1.7333333333333334p+1");
394 }
395 }
396
397 @safe unittest
398 {
399 auto f = FormatSpec!dchar("");
400 f.spec = 'a';
401 f.width = 22;
402 f.flDash = true;
403
404 assert(printFloat(1.0f, f) == "0x1p+0 ");
405 assert(printFloat(3.3f, f) == "0x1.a66666p+1 ");
406 assert(printFloat(2.9f, f) == "0x1.733334p+1 ");
407
408 assert(printFloat(1.0, f) == "0x1p+0 ");
409 assert(printFloat(3.3, f) == "0x1.a666666666666p+1 ");
410 assert(printFloat(2.9, f) == "0x1.7333333333333p+1 ");
411
412 static if (real.mant_dig == 64)
413 {
414 f.width = 25;
415 assert(printFloat(1.0L, f) == "0x1p+0 ");
416 assert(printFloat(3.3L, f) == "0x1.a666666666666666p+1 ");
417 assert(printFloat(2.9L, f) == "0x1.7333333333333334p+1 ");
418 }
419 }
420
421 @safe unittest
422 {
423 auto f = FormatSpec!dchar("");
424 f.spec = 'a';
425 f.width = 22;
426 f.flZero = true;
427
428 assert(printFloat(1.0f, f) == "0x00000000000000001p+0");
429 assert(printFloat(3.3f, f) == "0x0000000001.a66666p+1");
430 assert(printFloat(2.9f, f) == "0x0000000001.733334p+1");
431
432 assert(printFloat(1.0, f) == "0x00000000000000001p+0");
433 assert(printFloat(3.3, f) == "0x001.a666666666666p+1");
434 assert(printFloat(2.9, f) == "0x001.7333333333333p+1");
435
436 static if (real.mant_dig == 64)
437 {
438 f.width = 25;
439 assert(printFloat(1.0L, f) == "0x00000000000000000001p+0");
440 assert(printFloat(3.3L, f) == "0x001.a666666666666666p+1");
441 assert(printFloat(2.9L, f) == "0x001.7333333333333334p+1");
442 }
443 }
444
445 @safe unittest
446 {
447 auto f = FormatSpec!dchar("");
448 f.spec = 'a';
449 f.width = 22;
450 f.flPlus = true;
451
452 assert(printFloat(1.0f, f) == " +0x1p+0");
453 assert(printFloat(3.3f, f) == " +0x1.a66666p+1");
454 assert(printFloat(2.9f, f) == " +0x1.733334p+1");
455
456 assert(printFloat(1.0, f) == " +0x1p+0");
457 assert(printFloat(3.3, f) == " +0x1.a666666666666p+1");
458 assert(printFloat(2.9, f) == " +0x1.7333333333333p+1");
459
460 static if (real.mant_dig == 64)
461 {
462 f.width = 25;
463 assert(printFloat(1.0L, f) == " +0x1p+0");
464 assert(printFloat(3.3L, f) == " +0x1.a666666666666666p+1");
465 assert(printFloat(2.9L, f) == " +0x1.7333333333333334p+1");
466 }
467 }
468
469 @safe unittest
470 {
471 auto f = FormatSpec!dchar("");
472 f.spec = 'a';
473 f.width = 22;
474 f.flDash = true;
475 f.flSpace = true;
476
477 assert(printFloat(1.0f, f) == " 0x1p+0 ");
478 assert(printFloat(3.3f, f) == " 0x1.a66666p+1 ");
479 assert(printFloat(2.9f, f) == " 0x1.733334p+1 ");
480
481 assert(printFloat(1.0, f) == " 0x1p+0 ");
482 assert(printFloat(3.3, f) == " 0x1.a666666666666p+1 ");
483 assert(printFloat(2.9, f) == " 0x1.7333333333333p+1 ");
484
485 static if (real.mant_dig == 64)
486 {
487 f.width = 25;
488 assert(printFloat(1.0L, f) == " 0x1p+0 ");
489 assert(printFloat(3.3L, f) == " 0x1.a666666666666666p+1 ");
490 assert(printFloat(2.9L, f) == " 0x1.7333333333333334p+1 ");
491 }
492 }
493
494 @safe unittest
495 {
496 import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
497
498 // std.math's FloatingPointControl isn't available on all target platforms
499 static if (is(FloatingPointControl))
500 {
501 FloatingPointControl fpctrl;
502
503 auto f = FormatSpec!dchar("");
504 f.spec = 'a';
505 f.precision = 1;
506
507 fpctrl.rounding = FloatingPointControl.roundToNearest;
508
509 /* tiesAwayFromZero currently not supported
510 assert(printFloat(0x1.18p0, f) == "0x1.2p+0");
511 assert(printFloat(0x1.28p0, f) == "0x1.3p+0");
512 assert(printFloat(0x1.1ap0, f) == "0x1.2p+0");
513 assert(printFloat(0x1.16p0, f) == "0x1.1p+0");
514 assert(printFloat(0x1.10p0, f) == "0x1.1p+0");
515 assert(printFloat(-0x1.18p0, f) == "-0x1.2p+0");
516 assert(printFloat(-0x1.28p0, f) == "-0x1.3p+0");
517 assert(printFloat(-0x1.1ap0, f) == "-0x1.2p+0");
518 assert(printFloat(-0x1.16p0, f) == "-0x1.1p+0");
519 assert(printFloat(-0x1.10p0, f) == "-0x1.1p+0");
520 */
521
522 assert(printFloat(0x1.18p0, f) == "0x1.2p+0");
523 assert(printFloat(0x1.28p0, f) == "0x1.2p+0");
524 assert(printFloat(0x1.1ap0, f) == "0x1.2p+0");
525 assert(printFloat(0x1.16p0, f) == "0x1.1p+0");
526 assert(printFloat(0x1.10p0, f) == "0x1.1p+0");
527 assert(printFloat(-0x1.18p0, f) == "-0x1.2p+0");
528 assert(printFloat(-0x1.28p0, f) == "-0x1.2p+0");
529 assert(printFloat(-0x1.1ap0, f) == "-0x1.2p+0");
530 assert(printFloat(-0x1.16p0, f) == "-0x1.1p+0");
531 assert(printFloat(-0x1.10p0, f) == "-0x1.1p+0");
532
533 fpctrl.rounding = FloatingPointControl.roundToZero;
534
535 assert(printFloat(0x1.18p0, f) == "0x1.1p+0");
536 assert(printFloat(0x1.28p0, f) == "0x1.2p+0");
537 assert(printFloat(0x1.1ap0, f) == "0x1.1p+0");
538 assert(printFloat(0x1.16p0, f) == "0x1.1p+0");
539 assert(printFloat(0x1.10p0, f) == "0x1.1p+0");
540 assert(printFloat(-0x1.18p0, f) == "-0x1.1p+0");
541 assert(printFloat(-0x1.28p0, f) == "-0x1.2p+0");
542 assert(printFloat(-0x1.1ap0, f) == "-0x1.1p+0");
543 assert(printFloat(-0x1.16p0, f) == "-0x1.1p+0");
544 assert(printFloat(-0x1.10p0, f) == "-0x1.1p+0");
545
546 fpctrl.rounding = FloatingPointControl.roundUp;
547
548 assert(printFloat(0x1.18p0, f) == "0x1.2p+0");
549 assert(printFloat(0x1.28p0, f) == "0x1.3p+0");
550 assert(printFloat(0x1.1ap0, f) == "0x1.2p+0");
551 assert(printFloat(0x1.16p0, f) == "0x1.2p+0");
552 assert(printFloat(0x1.10p0, f) == "0x1.1p+0");
553 assert(printFloat(-0x1.18p0, f) == "-0x1.1p+0");
554 assert(printFloat(-0x1.28p0, f) == "-0x1.2p+0");
555 assert(printFloat(-0x1.1ap0, f) == "-0x1.1p+0");
556 assert(printFloat(-0x1.16p0, f) == "-0x1.1p+0");
557 assert(printFloat(-0x1.10p0, f) == "-0x1.1p+0");
558
559 fpctrl.rounding = FloatingPointControl.roundDown;
560
561 assert(printFloat(0x1.18p0, f) == "0x1.1p+0");
562 assert(printFloat(0x1.28p0, f) == "0x1.2p+0");
563 assert(printFloat(0x1.1ap0, f) == "0x1.1p+0");
564 assert(printFloat(0x1.16p0, f) == "0x1.1p+0");
565 assert(printFloat(0x1.10p0, f) == "0x1.1p+0");
566 assert(printFloat(-0x1.18p0, f) == "-0x1.2p+0");
567 assert(printFloat(-0x1.28p0, f) == "-0x1.3p+0");
568 assert(printFloat(-0x1.1ap0, f) == "-0x1.2p+0");
569 assert(printFloat(-0x1.16p0, f) == "-0x1.2p+0");
570 assert(printFloat(-0x1.10p0, f) == "-0x1.1p+0");
571 }
572 }
573
574 // for 100% coverage
575 @safe unittest
576 {
577 auto f = FormatSpec!dchar("");
578 f.spec = 'a';
579 f.precision = 3;
580
581 assert(printFloat(0x1.19f81p0, f) == "0x1.1a0p+0");
582 assert(printFloat(0x1.19f01p0, f) == "0x1.19fp+0");
583 }
584
585 @safe unittest
586 {
587 auto f = FormatSpec!dchar("");
588 f.spec = 'A';
589 f.precision = 3;
590
591 assert(printFloat(0x1.19f81p0, f) == "0X1.1A0P+0");
592 assert(printFloat(0x1.19f01p0, f) == "0X1.19FP+0");
593 }
594
595 private void printFloatE(bool g, Writer, T, Char)(auto ref Writer w, const(T) val,
596 FormatSpec!Char f, string sgn, int exp, ulong mnt, bool is_upper)
597 if (is(T == float) || is(T == double)
598 || (is(T == real) && (T.mant_dig == double.mant_dig || T.mant_dig == 64)))
599 {
600 import std.format.internal.write : writeAligned, PrecisionType, RoundingClass, round;
601
602 static if (!g)
603 {
604 if (f.precision == f.UNSPECIFIED)
605 f.precision = 6;
606 }
607
608 // special treatment for 0.0
609 if (mnt == 0)
610 {
611 static if (g)
612 writeAligned(w, sgn, "0", ".", "", f, PrecisionType.allDigits);
613 else
614 writeAligned(w, sgn, "0", ".", is_upper ? "E+00" : "e+00", f, PrecisionType.fractionalDigits);
615 return;
616 }
617
618 char[T.mant_dig + T.max_exp] dec_buf;
619 char[T.max_10_exp.stringof.length + 2] exp_buf;
620
621 int final_exp = 0;
622
623 RoundingClass rc;
624
625 // Depending on exp, we will use one of three algorithms:
626 //
627 // Algorithm A: For large exponents (exp >= T.mant_dig)
628 // Algorithm B: For small exponents (exp < T.mant_dig - 61)
629 // Algorithm C: For exponents close to 0.
630 //
631 // Algorithm A:
632 // The number to print looks like this: mantissa followed by several zeros.
633 //
634 // We know, that there is no fractional part, so we can just use integer division,
635 // consecutivly dividing by 10 and writing down the remainder from right to left.
636 // Unfortunately the integer is too large to fit in an ulong, so we use something
637 // like BigInt: An array of ulongs. We only use 60 bits of that ulongs, because
638 // this simplifies (and speeds up) the division to come.
639 //
640 // For the division we use integer division with reminder for each ulong and put
641 // the reminder of each step in the first 4 bits of ulong of the next step (think of
642 // long division for the rationale behind this). The final reminder is the next
643 // digit (from right to left).
644 //
645 // This results in the output we would have for the %f specifier. We now adjust this
646 // for %e: First we calculate the place, where the exponent should be printed, filling
647 // up with zeros if needed and second we move the leftmost digit one to the left
648 // and inserting a dot.
649 //
650 // After that we decide on the rounding type, using the digits right of the position,
651 // where the exponent will be printed (currently they are still there, but will be
652 // overwritten later).
653 //
654 // Algorithm B:
655 // The number to print looks like this: zero dot several zeros followed by the mantissa
656 //
657 // We know, that the number has no integer part. The algorithm consecutivly multiplies
658 // by 10. The integer part (rounded down) after the multiplication is the next digit
659 // (from left to right). This integer part is removed after each step.
660 // Again, the number is represented as an array of ulongs, with only 60 bits used of
661 // every ulong.
662 //
663 // For the multiplication we use normal integer multiplication, which can result in digits
664 // in the uppermost 4 bits. These 4 digits are the carry which is added to the result
665 // of the next multiplication and finally the last carry is the next digit.
666 //
667 // Other than for the %f specifier, this multiplication is splitted into two almost
668 // identical parts. The first part lasts as long as we find zeros. We need to do this
669 // to calculate the correct exponent.
670 //
671 // The second part will stop, when only zeros remain or when we've got enough digits
672 // for the requested precision. In the second case, we have to find out, which rounding
673 // we have. Aside from special cases we do this by calculating one more digit.
674 //
675 // Algorithm C:
676 // This time, we know, that the integral part and the fractional part each fit into a
677 // ulong. The mantissa might be partially in both parts or completely in the fractional
678 // part.
679 //
680 // We first calculate the integral part by consecutive division by 10. Depending on the
681 // precision this might result in more digits, than we need. In that case we calculate
682 // the position of the exponent and the rounding type.
683 //
684 // If there is no integral part, we need to find the first non zero digit. We do this by
685 // consecutive multiplication by 10, saving the first non zero digit followed by a dot.
686 //
687 // In either case, we continue filling up with the fractional part until we have enough
688 // digits. If still necessary, we decide the rounding type, mainly by looking at the
689 // next digit.
690
691 size_t right = 1;
692 size_t start = 1;
693 size_t left = 1;
694
695 static if (is(T == real) && real.mant_dig == 64)
696 {
697 enum small_bound = 0;
698 enum max_buf = 275;
699 }
700 else
701 {
702 enum small_bound = T.mant_dig - 61;
703 static if (is(T == float))
704 enum max_buf = 4;
705 else
706 enum max_buf = 18;
707 }
708
709 ulong[max_buf] bigbuf;
710 if (exp >= T.mant_dig)
711 {
712 start = left = right = dec_buf.length;
713
714 // large number without fractional digits
715 //
716 // As this number does not fit in a ulong, we use an array of ulongs. We only use 60 of the 64 bits,
717 // because this makes it much more easy to implement the division by 10.
718 int count = exp / 60 + 1;
719
720 // only the first few ulongs contain the mantiassa. The rest are zeros.
721 int lower = 60 - (exp - T.mant_dig + 1) % 60;
722
723 static if (is(T == real) && real.mant_dig == 64)
724 {
725 // for x87 reals, the lowest ulong may contain more than 60 bits,
726 // because the mantissa is 63 (>60) bits long
727 // therefore we need one ulong less
728 if (lower <= 3) count--;
729 }
730
731 // saved in big endian format
732 ulong[] mybig = bigbuf[0 .. count];
733
734 if (lower < T.mant_dig)
735 {
736 mybig[0] = mnt >> lower;
737 mybig[1] = (mnt & ((1L << lower) - 1)) << 60 - lower;
738 }
739 else
740 mybig[0] = (mnt & ((1L << lower) - 1)) << 60 - lower;
741
742 // Generation of digits by consecutive division with reminder by 10.
743 int msu = 0; // Most significant ulong; when it get's zero, we can ignore it further on
744 while (msu < count - 1 || mybig[$ - 1] != 0)
745 {
746 ulong mod = 0;
747 foreach (i;msu .. count)
748 {
749 mybig[i] |= mod << 60;
750 mod = mybig[i] % 10;
751 mybig[i] /= 10;
752 }
753 if (mybig[msu] == 0)
754 ++msu;
755
756 dec_buf[--left] = cast(byte) ('0' + mod);
757 ++final_exp;
758 }
759 --final_exp;
760
761 static if (g)
762 start = left + f.precision;
763 else
764 start = left + f.precision + 1;
765
766 // move leftmost digit one more left and add dot between
767 dec_buf[left - 1] = dec_buf[left];
768 dec_buf[left] = '.';
769 --left;
770
771 // rounding type
772 if (start >= right)
773 rc = RoundingClass.ZERO;
774 else if (dec_buf[start] != '0' && dec_buf[start] != '5')
775 rc = dec_buf[start] > '5' ? RoundingClass.UPPER : RoundingClass.LOWER;
776 else
777 {
778 rc = dec_buf[start] == '5' ? RoundingClass.FIVE : RoundingClass.ZERO;
779 foreach (i; start + 1 .. right)
780 if (dec_buf[i] > '0')
781 {
782 rc = rc == RoundingClass.FIVE ? RoundingClass.UPPER : RoundingClass.LOWER;
783 break;
784 }
785 }
786
787 if (start < right) right = start;
788 }
789 else if (exp < small_bound)
790 {
791 // small number without integer digits
792 //
793 // Again this number does not fit in a ulong and we use an array of ulongs. And again we
794 // only use 60 bits, because this simplifies the multiplication by 10.
795 int count = (T.mant_dig - exp - 2) / 60 + 1;
796
797 // saved in little endian format
798 ulong[] mybig = bigbuf[0 .. count];
799
800 // only the last few ulongs contain the mantiassa. Because of little endian
801 // format these are the ulongs at index 0 and 1 (and 2 in case of x87 reals).
802 // The rest are zeros.
803 int upper = 60 - (-exp - 1) % 60;
804
805 static if (is(T == real) && real.mant_dig == 64)
806 {
807 if (upper < 4)
808 {
809 mybig[0] = (mnt & ((1L << (4 - upper)) - 1)) << 56 + upper;
810 mybig[1] = (mnt >> (4 - upper)) & ((1L << 60) - 1);
811 mybig[2] = mnt >> 64 - upper;
812 }
813 else
814 {
815 mybig[0] = (mnt & ((1L << (T.mant_dig - upper)) - 1)) << 60 - (T.mant_dig - upper);
816 mybig[1] = mnt >> (T.mant_dig - upper);
817 }
818 }
819 else
820 {
821 if (upper < T.mant_dig)
822 {
823 mybig[0] = (mnt & ((1L << (T.mant_dig - upper)) - 1)) << 60 - (T.mant_dig - upper);
824 mybig[1] = mnt >> (T.mant_dig - upper);
825 }
826 else
827 mybig[0] = mnt << (upper - T.mant_dig);
828 }
829
830 int lsu = 0; // Least significant ulong; when it get's zero, we can ignore it further on
831
832 // adding zeros, until we reach first nonzero
833 while (lsu < count - 1 || mybig[$ - 1]!=0)
834 {
835 ulong over = 0;
836 foreach (i; lsu .. count)
837 {
838 mybig[i] = mybig[i] * 10 + over;
839 over = mybig[i] >> 60;
840 mybig[i] &= (1L << 60) - 1;
841 }
842 if (mybig[lsu] == 0)
843 ++lsu;
844 --final_exp;
845
846 if (over != 0)
847 {
848 dec_buf[right++] = cast(byte) ('0' + over);
849 dec_buf[right++] = '.';
850 break;
851 }
852 }
853
854 // adding more digits
855 static if (g)
856 start = right - 1;
857 else
858 start = right;
859 while ((lsu < count - 1 || mybig[$ - 1] != 0) && right - start < f.precision)
860 {
861 ulong over = 0;
862 foreach (i;lsu .. count)
863 {
864 mybig[i] = mybig[i] * 10 + over;
865 over = mybig[i] >> 60;
866 mybig[i] &= (1L << 60) - 1;
867 }
868 if (mybig[lsu] == 0)
869 ++lsu;
870
871 dec_buf[right++] = cast(byte) ('0' + over);
872 }
873
874 // rounding type
875 if (lsu >= count - 1 && mybig[count - 1] == 0)
876 rc = RoundingClass.ZERO;
877 else if (lsu == count - 1 && mybig[lsu] == 1L << 59)
878 rc = RoundingClass.FIVE;
879 else
880 {
881 ulong over = 0;
882 foreach (i;lsu .. count)
883 {
884 mybig[i] = mybig[i] * 10 + over;
885 over = mybig[i] >> 60;
886 mybig[i] &= (1L << 60) - 1;
887 }
888 rc = over >= 5 ? RoundingClass.UPPER : RoundingClass.LOWER;
889 }
890 }
891 else
892 {
893 // medium sized number, probably with integer and fractional digits
894 // this is fastest, because both parts fit into a ulong each
895 ulong int_part = mnt >> (T.mant_dig - 1 - exp);
896 ulong frac_part = mnt & ((1L << (T.mant_dig - 1 - exp)) - 1);
897
898 // for x87 reals the mantiassa might be up to 3 bits too long
899 // we need to save these bits as a tail and handle this separately
900 static if (is(T == real) && real.mant_dig == 64)
901 {
902 ulong tail = 0;
903 ulong tail_length = 0;
904 if (exp < 3)
905 {
906 tail = frac_part & ((1L << (3 - exp)) - 1);
907 tail_length = 3 - exp;
908 frac_part >>= 3 - exp;
909 exp = 3;
910 }
911 }
912
913 start = 0;
914
915 // could we already decide on the rounding mode in the integer part?
916 bool found = false;
917
918 if (int_part > 0)
919 {
920 import core.bitop : bsr;
921 left = right = int_part.bsr * 100 / 332 + 4;
922
923 // integer part, if there is something to print
924 while (int_part >= 10)
925 {
926 dec_buf[--left] = '0' + (int_part % 10);
927 int_part /= 10;
928 ++final_exp;
929 ++start;
930 }
931
932 dec_buf[--left] = '.';
933 dec_buf[--left] = cast(byte) ('0' + int_part);
934
935 static if (g)
936 auto limit = f.precision + 1;
937 else
938 auto limit = f.precision + 2;
939
940 if (right - left > limit)
941 {
942 auto old_right = right;
943 right = left + limit;
944
945 if (dec_buf[right] == '5' || dec_buf[right] == '0')
946 {
947 rc = dec_buf[right] == '5' ? RoundingClass.FIVE : RoundingClass.ZERO;
948 if (frac_part != 0)
949 rc = rc == RoundingClass.FIVE ? RoundingClass.UPPER : RoundingClass.LOWER;
950 else
951 foreach (i;right + 1 .. old_right)
952 if (dec_buf[i] > '0')
953 {
954 rc = rc == RoundingClass.FIVE ? RoundingClass.UPPER : RoundingClass.LOWER;
955 break;
956 }
957 }
958 else
959 rc = dec_buf[right] > '5' ? RoundingClass.UPPER : RoundingClass.LOWER;
960 found = true;
961 }
962 }
963 else
964 {
965 // fractional part, skipping leading zeros
966 while (frac_part != 0)
967 {
968 --final_exp;
969 frac_part *= 10;
970 static if (is(T == real) && real.mant_dig == 64)
971 {
972 if (tail_length > 0)
973 {
974 // together this is *= 10;
975 tail *= 5;
976 tail_length--;
977
978 frac_part += tail >> tail_length;
979 if (tail_length > 0)
980 tail &= (1L << tail_length) - 1;
981 }
982 }
983 auto tmp = frac_part >> (T.mant_dig - 1 - exp);
984 frac_part &= ((1L << (T.mant_dig - 1 - exp)) - 1);
985 if (tmp > 0)
986 {
987 dec_buf[right++] = cast(byte) ('0' + tmp);
988 dec_buf[right++] = '.';
989 break;
990 }
991 }
992
993 rc = RoundingClass.ZERO;
994 }
995
996 static if (g)
997 size_t limit = f.precision - 1;
998 else
999 size_t limit = f.precision;
1000
1001 // the fractional part after the zeros
1002 while (frac_part != 0 && start < limit)
1003 {
1004 frac_part *= 10;
1005 static if (is(T == real) && real.mant_dig == 64)
1006 {
1007 if (tail_length > 0)
1008 {
1009 // together this is *= 10;
1010 tail *= 5;
1011 tail_length--;
1012
1013 frac_part += tail >> tail_length;
1014 if (tail_length > 0)
1015 tail &= (1L << tail_length) - 1;
1016 }
1017 }
1018 dec_buf[right++] = cast(byte) ('0' + (frac_part >> (T.mant_dig - 1 - exp)));
1019 frac_part &= ((1L << (T.mant_dig - 1 - exp)) - 1);
1020 ++start;
1021 }
1022
1023 static if (g)
1024 limit = right - left - 1;
1025 else
1026 limit = start;
1027
1028 // rounding mode, if not allready known
1029 if (frac_part != 0 && !found)
1030 {
1031 frac_part *= 10;
1032 auto nextDigit = frac_part >> (T.mant_dig - 1 - exp);
1033 frac_part &= ((1L << (T.mant_dig - 1 - exp)) - 1);
1034
1035 if (nextDigit == 5 && frac_part == 0)
1036 rc = RoundingClass.FIVE;
1037 else if (nextDigit >= 5)
1038 rc = RoundingClass.UPPER;
1039 else
1040 rc = RoundingClass.LOWER;
1041 }
1042 }
1043
1044 if (round(dec_buf, left, right, rc, sgn == "-"))
1045 {
1046 left--;
1047 right--;
1048 dec_buf[left + 2] = dec_buf[left + 1];
1049 dec_buf[left + 1] = '.';
1050 final_exp++;
1051 }
1052
1053 // printing exponent
1054 auto neg = final_exp < 0;
1055 if (neg) final_exp = -final_exp;
1056
1057 size_t exp_pos = exp_buf.length;
1058
1059 do
1060 {
1061 exp_buf[--exp_pos] = '0' + final_exp%10;
1062 final_exp /= 10;
1063 } while (final_exp > 0);
1064 if (exp_buf.length - exp_pos == 1)
1065 exp_buf[--exp_pos] = '0';
1066 exp_buf[--exp_pos] = neg ? '-' : '+';
1067 exp_buf[--exp_pos] = is_upper ? 'E' : 'e';
1068
1069 while (right > left + 1 && dec_buf[right - 1] == '0') right--;
1070
1071 if (right == left + 1)
1072 dec_buf[right++] = '.';
1073
1074 static if (g)
1075 writeAligned(w, sgn, dec_buf[left .. left + 1], dec_buf[left + 1 .. right],
1076 exp_buf[exp_pos .. $], f, PrecisionType.allDigits);
1077 else
1078 writeAligned(w, sgn, dec_buf[left .. left + 1], dec_buf[left + 1 .. right],
1079 exp_buf[exp_pos .. $], f, PrecisionType.fractionalDigits);
1080 }
1081
1082 @safe unittest
1083 {
1084 auto f = FormatSpec!dchar("");
1085 f.spec = 'e';
1086 assert(printFloat(float.nan, f) == "nan");
1087 assert(printFloat(-float.nan, f) == "-nan");
1088 assert(printFloat(float.infinity, f) == "inf");
1089 assert(printFloat(-float.infinity, f) == "-inf");
1090 assert(printFloat(0.0f, f) == "0.000000e+00");
1091 assert(printFloat(-0.0f, f) == "-0.000000e+00");
1092 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1093 assert(printFloat(cast(float) 1e-40, f) == "9.999946e-41");
1094 assert(printFloat(cast(float) -1e-40, f) == "-9.999946e-41");
1095 assert(printFloat(1e-30f, f) == "1.000000e-30");
1096 assert(printFloat(-1e-30f, f) == "-1.000000e-30");
1097 assert(printFloat(1e-10f, f) == "1.000000e-10");
1098 assert(printFloat(-1e-10f, f) == "-1.000000e-10");
1099 assert(printFloat(0.1f, f) == "1.000000e-01");
1100 assert(printFloat(-0.1f, f) == "-1.000000e-01");
1101 assert(printFloat(10.0f, f) == "1.000000e+01");
1102 assert(printFloat(-10.0f, f) == "-1.000000e+01");
1103 assert(printFloat(1e30f, f) == "1.000000e+30");
1104 assert(printFloat(-1e30f, f) == "-1.000000e+30");
1105
1106 import std.math.operations : nextUp, nextDown;
1107 assert(printFloat(nextUp(0.0f), f) == "1.401298e-45");
1108 assert(printFloat(nextDown(-0.0f), f) == "-1.401298e-45");
1109 }
1110
1111 @safe unittest
1112 {
1113 auto f = FormatSpec!dchar("");
1114 f.spec = 'e';
1115 f.width = 20;
1116 f.precision = 10;
1117
1118 assert(printFloat(float.nan, f) == " nan");
1119 assert(printFloat(-float.nan, f) == " -nan");
1120 assert(printFloat(float.infinity, f) == " inf");
1121 assert(printFloat(-float.infinity, f) == " -inf");
1122 assert(printFloat(0.0f, f) == " 0.0000000000e+00");
1123 assert(printFloat(-0.0f, f) == " -0.0000000000e+00");
1124 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1125 assert(printFloat(cast(float) 1e-40, f) == " 9.9999461011e-41");
1126 assert(printFloat(cast(float) -1e-40, f) == " -9.9999461011e-41");
1127 assert(printFloat(1e-30f, f) == " 1.0000000032e-30");
1128 assert(printFloat(-1e-30f, f) == " -1.0000000032e-30");
1129 assert(printFloat(1e-10f, f) == " 1.0000000134e-10");
1130 assert(printFloat(-1e-10f, f) == " -1.0000000134e-10");
1131 assert(printFloat(0.1f, f) == " 1.0000000149e-01");
1132 assert(printFloat(-0.1f, f) == " -1.0000000149e-01");
1133 assert(printFloat(10.0f, f) == " 1.0000000000e+01");
1134 assert(printFloat(-10.0f, f) == " -1.0000000000e+01");
1135 assert(printFloat(1e30f, f) == " 1.0000000150e+30");
1136 assert(printFloat(-1e30f, f) == " -1.0000000150e+30");
1137
1138 import std.math.operations : nextUp, nextDown;
1139 assert(printFloat(nextUp(0.0f), f) == " 1.4012984643e-45");
1140 assert(printFloat(nextDown(-0.0f), f) == " -1.4012984643e-45");
1141 }
1142
1143 @safe unittest
1144 {
1145 auto f = FormatSpec!dchar("");
1146 f.spec = 'e';
1147 f.width = 20;
1148 f.precision = 10;
1149 f.flDash = true;
1150
1151 assert(printFloat(float.nan, f) == "nan ");
1152 assert(printFloat(-float.nan, f) == "-nan ");
1153 assert(printFloat(float.infinity, f) == "inf ");
1154 assert(printFloat(-float.infinity, f) == "-inf ");
1155 assert(printFloat(0.0f, f) == "0.0000000000e+00 ");
1156 assert(printFloat(-0.0f, f) == "-0.0000000000e+00 ");
1157 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1158 assert(printFloat(cast(float) 1e-40, f) == "9.9999461011e-41 ");
1159 assert(printFloat(cast(float) -1e-40, f) == "-9.9999461011e-41 ");
1160 assert(printFloat(1e-30f, f) == "1.0000000032e-30 ");
1161 assert(printFloat(-1e-30f, f) == "-1.0000000032e-30 ");
1162 assert(printFloat(1e-10f, f) == "1.0000000134e-10 ");
1163 assert(printFloat(-1e-10f, f) == "-1.0000000134e-10 ");
1164 assert(printFloat(0.1f, f) == "1.0000000149e-01 ");
1165 assert(printFloat(-0.1f, f) == "-1.0000000149e-01 ");
1166 assert(printFloat(10.0f, f) == "1.0000000000e+01 ");
1167 assert(printFloat(-10.0f, f) == "-1.0000000000e+01 ");
1168 assert(printFloat(1e30f, f) == "1.0000000150e+30 ");
1169 assert(printFloat(-1e30f, f) == "-1.0000000150e+30 ");
1170
1171 import std.math.operations : nextUp, nextDown;
1172 assert(printFloat(nextUp(0.0f), f) == "1.4012984643e-45 ");
1173 assert(printFloat(nextDown(-0.0f), f) == "-1.4012984643e-45 ");
1174 }
1175
1176 @safe unittest
1177 {
1178 auto f = FormatSpec!dchar("");
1179 f.spec = 'e';
1180 f.width = 20;
1181 f.precision = 10;
1182 f.flZero = true;
1183
1184 assert(printFloat(float.nan, f) == " nan");
1185 assert(printFloat(-float.nan, f) == " -nan");
1186 assert(printFloat(float.infinity, f) == " inf");
1187 assert(printFloat(-float.infinity, f) == " -inf");
1188 assert(printFloat(0.0f, f) == "00000.0000000000e+00");
1189 assert(printFloat(-0.0f, f) == "-0000.0000000000e+00");
1190 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1191 assert(printFloat(cast(float) 1e-40, f) == "00009.9999461011e-41");
1192 assert(printFloat(cast(float) -1e-40, f) == "-0009.9999461011e-41");
1193 assert(printFloat(1e-30f, f) == "00001.0000000032e-30");
1194 assert(printFloat(-1e-30f, f) == "-0001.0000000032e-30");
1195 assert(printFloat(1e-10f, f) == "00001.0000000134e-10");
1196 assert(printFloat(-1e-10f, f) == "-0001.0000000134e-10");
1197 assert(printFloat(0.1f, f) == "00001.0000000149e-01");
1198 assert(printFloat(-0.1f, f) == "-0001.0000000149e-01");
1199 assert(printFloat(10.0f, f) == "00001.0000000000e+01");
1200 assert(printFloat(-10.0f, f) == "-0001.0000000000e+01");
1201 assert(printFloat(1e30f, f) == "00001.0000000150e+30");
1202 assert(printFloat(-1e30f, f) == "-0001.0000000150e+30");
1203
1204 import std.math.operations : nextUp, nextDown;
1205 assert(printFloat(nextUp(0.0f), f) == "00001.4012984643e-45");
1206 assert(printFloat(nextDown(-0.0f), f) == "-0001.4012984643e-45");
1207 }
1208
1209 @safe unittest
1210 {
1211 import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
1212
1213 // std.math's FloatingPointControl isn't available on all target platforms
1214 static if (is(FloatingPointControl))
1215 {
1216 FloatingPointControl fpctrl;
1217
1218 auto f = FormatSpec!dchar("");
1219 f.spec = 'e';
1220 f.precision = 1;
1221
1222 fpctrl.rounding = FloatingPointControl.roundToNearest;
1223
1224 /*
1225 assert(printFloat(11.5f, f) == "1.2e+01");
1226 assert(printFloat(12.5f, f) == "1.3e+01");
1227 assert(printFloat(11.7f, f) == "1.2e+01");
1228 assert(printFloat(11.3f, f) == "1.1e+01");
1229 assert(printFloat(11.0f, f) == "1.1e+01");
1230 assert(printFloat(-11.5f, f) == "-1.2e+01");
1231 assert(printFloat(-12.5f, f) == "-1.3e+01");
1232 assert(printFloat(-11.7f, f) == "-1.2e+01");
1233 assert(printFloat(-11.3f, f) == "-1.1e+01");
1234 assert(printFloat(-11.0f, f) == "-1.1e+01");
1235 */
1236
1237 assert(printFloat(11.5f, f) == "1.2e+01");
1238 assert(printFloat(12.5f, f) == "1.2e+01");
1239 assert(printFloat(11.7f, f) == "1.2e+01");
1240 assert(printFloat(11.3f, f) == "1.1e+01");
1241 assert(printFloat(11.0f, f) == "1.1e+01");
1242 assert(printFloat(-11.5f, f) == "-1.2e+01");
1243 assert(printFloat(-12.5f, f) == "-1.2e+01");
1244 assert(printFloat(-11.7f, f) == "-1.2e+01");
1245 assert(printFloat(-11.3f, f) == "-1.1e+01");
1246 assert(printFloat(-11.0f, f) == "-1.1e+01");
1247
1248 fpctrl.rounding = FloatingPointControl.roundToZero;
1249
1250 assert(printFloat(11.5f, f) == "1.1e+01");
1251 assert(printFloat(12.5f, f) == "1.2e+01");
1252 assert(printFloat(11.7f, f) == "1.1e+01");
1253 assert(printFloat(11.3f, f) == "1.1e+01");
1254 assert(printFloat(11.0f, f) == "1.1e+01");
1255 assert(printFloat(-11.5f, f) == "-1.1e+01");
1256 assert(printFloat(-12.5f, f) == "-1.2e+01");
1257 assert(printFloat(-11.7f, f) == "-1.1e+01");
1258 assert(printFloat(-11.3f, f) == "-1.1e+01");
1259 assert(printFloat(-11.0f, f) == "-1.1e+01");
1260
1261 fpctrl.rounding = FloatingPointControl.roundUp;
1262
1263 assert(printFloat(11.5f, f) == "1.2e+01");
1264 assert(printFloat(12.5f, f) == "1.3e+01");
1265 assert(printFloat(11.7f, f) == "1.2e+01");
1266 assert(printFloat(11.3f, f) == "1.2e+01");
1267 assert(printFloat(11.0f, f) == "1.1e+01");
1268 assert(printFloat(-11.5f, f) == "-1.1e+01");
1269 assert(printFloat(-12.5f, f) == "-1.2e+01");
1270 assert(printFloat(-11.7f, f) == "-1.1e+01");
1271 assert(printFloat(-11.3f, f) == "-1.1e+01");
1272 assert(printFloat(-11.0f, f) == "-1.1e+01");
1273
1274 fpctrl.rounding = FloatingPointControl.roundDown;
1275
1276 assert(printFloat(11.5f, f) == "1.1e+01");
1277 assert(printFloat(12.5f, f) == "1.2e+01");
1278 assert(printFloat(11.7f, f) == "1.1e+01");
1279 assert(printFloat(11.3f, f) == "1.1e+01");
1280 assert(printFloat(11.0f, f) == "1.1e+01");
1281 assert(printFloat(-11.5f, f) == "-1.2e+01");
1282 assert(printFloat(-12.5f, f) == "-1.3e+01");
1283 assert(printFloat(-11.7f, f) == "-1.2e+01");
1284 assert(printFloat(-11.3f, f) == "-1.2e+01");
1285 assert(printFloat(-11.0f, f) == "-1.1e+01");
1286 }
1287 }
1288
1289 @safe unittest
1290 {
1291 auto f = FormatSpec!dchar("");
1292 f.spec = 'e';
1293 assert(printFloat(double.nan, f) == "nan");
1294 assert(printFloat(-double.nan, f) == "-nan");
1295 assert(printFloat(double.infinity, f) == "inf");
1296 assert(printFloat(-double.infinity, f) == "-inf");
1297 assert(printFloat(0.0, f) == "0.000000e+00");
1298 assert(printFloat(-0.0, f) == "-0.000000e+00");
1299 // / 1000 needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1300 assert(printFloat(1e-307 / 1000, f) == "1.000000e-310");
1301 assert(printFloat(-1e-307 / 1000, f) == "-1.000000e-310");
1302 assert(printFloat(1e-30, f) == "1.000000e-30");
1303 assert(printFloat(-1e-30, f) == "-1.000000e-30");
1304 assert(printFloat(1e-10, f) == "1.000000e-10");
1305 assert(printFloat(-1e-10, f) == "-1.000000e-10");
1306 assert(printFloat(0.1, f) == "1.000000e-01");
1307 assert(printFloat(-0.1, f) == "-1.000000e-01");
1308 assert(printFloat(10.0, f) == "1.000000e+01");
1309 assert(printFloat(-10.0, f) == "-1.000000e+01");
1310 assert(printFloat(1e300, f) == "1.000000e+300");
1311 assert(printFloat(-1e300, f) == "-1.000000e+300");
1312
1313 import std.math.operations : nextUp, nextDown;
1314 assert(printFloat(nextUp(0.0), f) == "4.940656e-324");
1315 assert(printFloat(nextDown(-0.0), f) == "-4.940656e-324");
1316 }
1317
1318 @safe unittest
1319 {
1320 static if (real.mant_dig > 64)
1321 {
1322 pragma(msg, "printFloat tests disabled because of unsupported `real` format");
1323 }
1324 else
1325 {
1326 auto f = FormatSpec!dchar("");
1327 f.spec = 'e';
1328 assert(printFloat(real.nan, f) == "nan");
1329 assert(printFloat(-real.nan, f) == "-nan");
1330 assert(printFloat(real.infinity, f) == "inf");
1331 assert(printFloat(-real.infinity, f) == "-inf");
1332 }
1333 }
1334
1335 @safe unittest
1336 {
1337 auto f = FormatSpec!dchar("");
1338 f.spec = 'e';
1339
1340 import std.math.operations : nextUp;
1341
1342 double eps = nextUp(0.0);
1343 f.precision = 1000;
1344 assert(printFloat(eps, f) ==
1345 "4.9406564584124654417656879286822137236505980261432476442558568250067550727020875186529983636163599"
1346 ~"23797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036"
1347 ~"88718636056998730723050006387409153564984387312473397273169615140031715385398074126238565591171026"
1348 ~"65855668676818703956031062493194527159149245532930545654440112748012970999954193198940908041656332"
1349 ~"45247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431"
1350 ~"93609238289345836806010601150616980975307834227731832924790498252473077637592724787465608477820373"
1351 ~"44696995336470179726777175851256605511991315048911014510378627381672509558373897335989936648099411"
1352 ~"64205702637090279242767544565229087538682506419718265533447265625000000000000000000000000000000000"
1353 ~"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1354 ~"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
1355 ~"000000000000000000000e-324");
1356
1357 f.precision = 50;
1358 assert(printFloat(double.max, f) ==
1359 "1.79769313486231570814527423731704356798070567525845e+308");
1360 assert(printFloat(double.epsilon, f) ==
1361 "2.22044604925031308084726333618164062500000000000000e-16");
1362
1363 f.precision = 10;
1364 assert(printFloat(1.0/3.0, f) == "3.3333333333e-01");
1365 assert(printFloat(1.0/7.0, f) == "1.4285714286e-01");
1366 assert(printFloat(1.0/9.0, f) == "1.1111111111e-01");
1367 }
1368
1369 @safe unittest
1370 {
1371 auto f = FormatSpec!dchar("");
1372 f.spec = 'e';
1373 f.precision = 15;
1374
1375 import std.math.constants : E, PI, PI_2, PI_4, M_1_PI, M_2_PI, M_2_SQRTPI,
1376 LN10, LN2, LOG2, LOG2E, LOG2T, LOG10E, SQRT2, SQRT1_2;
1377
1378 assert(printFloat(cast(double) E, f) == "2.718281828459045e+00");
1379 assert(printFloat(cast(double) PI, f) == "3.141592653589793e+00");
1380 assert(printFloat(cast(double) PI_2, f) == "1.570796326794897e+00");
1381 assert(printFloat(cast(double) PI_4, f) == "7.853981633974483e-01");
1382 assert(printFloat(cast(double) M_1_PI, f) == "3.183098861837907e-01");
1383 assert(printFloat(cast(double) M_2_PI, f) == "6.366197723675814e-01");
1384 assert(printFloat(cast(double) M_2_SQRTPI, f) == "1.128379167095513e+00");
1385 assert(printFloat(cast(double) LN10, f) == "2.302585092994046e+00");
1386 assert(printFloat(cast(double) LN2, f) == "6.931471805599453e-01");
1387 assert(printFloat(cast(double) LOG2, f) == "3.010299956639812e-01");
1388 assert(printFloat(cast(double) LOG2E, f) == "1.442695040888963e+00");
1389 assert(printFloat(cast(double) LOG2T, f) == "3.321928094887362e+00");
1390 assert(printFloat(cast(double) LOG10E, f) == "4.342944819032518e-01");
1391 assert(printFloat(cast(double) SQRT2, f) == "1.414213562373095e+00");
1392 assert(printFloat(cast(double) SQRT1_2, f) == "7.071067811865476e-01");
1393 }
1394
1395 // for 100% coverage
1396 @safe unittest
1397 {
1398 import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
1399
1400 auto f = FormatSpec!dchar("");
1401 f.spec = 'E';
1402 f.precision = 80;
1403 assert(printFloat(5.62776e+12f, f) ==
1404 "5.62775982080000000000000000000000000000000000000000000000000000000000000000000000E+12");
1405
1406 f.precision = 49;
1407 assert(printFloat(2.5997869e-12f, f) ==
1408 "2.5997869221999758693186777236405760049819946289062E-12");
1409
1410 f.precision = 6;
1411 assert(printFloat(-1.1418613e+07f, f) == "-1.141861E+07");
1412 assert(printFloat(-1.368281e+07f, f) == "-1.368281E+07");
1413
1414 f.precision = 1;
1415 assert(printFloat(-245.666f, f) == "-2.5E+02");
1416
1417 static if (is(FloatingPointControl))
1418 {
1419 FloatingPointControl fpctrl;
1420
1421 fpctrl.rounding = FloatingPointControl.roundUp;
1422
1423 f.precision = 0;
1424 assert(printFloat(709422.0f, f) == "8E+05");
1425 }
1426 }
1427
1428 @safe unittest
1429 {
1430 static if (real.mant_dig > 64)
1431 {
1432 pragma(msg, "printFloat tests disabled because of unsupported `real` format");
1433 }
1434 else
1435 {
1436 auto f = FormatSpec!dchar("");
1437 f.spec = 'e';
1438 assert(printFloat(real.nan, f) == "nan");
1439 assert(printFloat(-real.nan, f) == "-nan");
1440 assert(printFloat(real.infinity, f) == "inf");
1441 assert(printFloat(-real.infinity, f) == "-inf");
1442 assert(printFloat(0.0L, f) == "0.000000e+00");
1443 assert(printFloat(-0.0L, f) == "-0.000000e+00");
1444 }
1445
1446 static if (real.mant_dig == 64)
1447 {
1448 assert(printFloat(1e-4940L, f) == "1.000000e-4940");
1449 assert(printFloat(-1e-4940L, f) == "-1.000000e-4940");
1450 assert(printFloat(1e-30L, f) == "1.000000e-30");
1451 assert(printFloat(-1e-30L, f) == "-1.000000e-30");
1452 assert(printFloat(1e-10L, f) == "1.000000e-10");
1453 assert(printFloat(-1e-10L, f) == "-1.000000e-10");
1454 assert(printFloat(0.1L, f) == "1.000000e-01");
1455 assert(printFloat(-0.1L, f) == "-1.000000e-01");
1456 assert(printFloat(10.0L, f) == "1.000000e+01");
1457 assert(printFloat(-10.0L, f) == "-1.000000e+01");
1458 version (Windows) {} // https://issues.dlang.org/show_bug.cgi?id=20972
1459 else
1460 {
1461 assert(printFloat(1e4000L, f) == "1.000000e+4000");
1462 assert(printFloat(-1e4000L, f) == "-1.000000e+4000");
1463 }
1464
1465 import std.math.operations : nextUp, nextDown;
1466 assert(printFloat(nextUp(0.0L), f) == "3.645200e-4951");
1467 assert(printFloat(nextDown(-0.0L), f) == "-3.645200e-4951");
1468 }
1469 }
1470
1471 @safe unittest
1472 {
1473 import std.exception : assertCTFEable;
1474 import std.math.exponential : log2;
1475 import std.math.operations : nextDown;
1476
1477 assertCTFEable!(
1478 {
1479 static if (real.mant_dig == 64) // 80 bit reals
1480 {
1481 // log2 is broken for x87-reals on some computers in CTFE
1482 // the following tests excludes these computers from the tests
1483 // (https://issues.dlang.org/show_bug.cgi?id=21757)
1484 enum test = cast(int) log2(3.05e2312L);
1485 static if (test == 7681)
1486 {
1487 auto f = FormatSpec!dchar("");
1488 f.spec = 'e';
1489 assert(printFloat(real.infinity, f) == "inf");
1490 assert(printFloat(10.0L, f) == "1.000000e+01");
1491 assert(printFloat(2.6080L, f) == "2.608000e+00");
1492 assert(printFloat(3.05e2312L, f) == "3.050000e+2312");
1493
1494 f.precision = 60;
1495 assert(printFloat(2.65e-54L, f) ==
1496 "2.650000000000000000059009987400547013941028940935296547599415e-54");
1497
1498 /*
1499 commented out, because CTFE is currently too slow for 5000 digits with extreme values
1500
1501 f.precision = 5000;
1502 auto result2 = printFloat(1.2119e-4822L, f);
1503 assert(result2.length == 5008);
1504 assert(result2[$ - 20 .. $] == "60729486595339e-4822");
1505 auto result3 = printFloat(real.min_normal, f);
1506 assert(result3.length == 5008);
1507 assert(result3[$ - 20 .. $] == "20781410082267e-4932");
1508 auto result4 = printFloat(real.min_normal.nextDown, f);
1509 assert(result4.length == 5008);
1510 assert(result4[$ - 20 .. $] == "81413263331006e-4932");
1511 */
1512 }
1513 }
1514 });
1515 }
1516
1517 private void printFloatF(bool g, Writer, T, Char)(auto ref Writer w, const(T) val,
1518 FormatSpec!Char f, string sgn, int exp, ulong mnt, bool is_upper)
1519 if (is(T == float) || is(T == double)
1520 || (is(T == real) && (T.mant_dig == double.mant_dig || T.mant_dig == 64)))
1521 {
1522 import std.format.internal.write : writeAligned, PrecisionType, RoundingClass, round;
1523
1524 static if (!g)
1525 {
1526 if (f.precision == f.UNSPECIFIED)
1527 f.precision = 6;
1528 }
1529
1530 // special treatment for 0.0
1531 if (exp == 0 && mnt == 0)
1532 {
1533 writeAligned(w, sgn, "0", ".", "", f, PrecisionType.fractionalDigits);
1534 return;
1535 }
1536
1537 char[T.max_exp + T.mant_dig + 1] dec_buf;
1538
1539 RoundingClass rc;
1540
1541 // Depending on exp, we will use one of three algorithms:
1542 //
1543 // Algorithm A: For large exponents (exp >= T.mant_dig)
1544 // Algorithm B: For small exponents (exp < T.mant_dig - 61)
1545 // Algorithm C: For exponents close to 0.
1546 //
1547 // Algorithm A:
1548 // The number to print looks like this: mantissa followed by several zeros.
1549 //
1550 // We know, that there is no fractional part, so we can just use integer division,
1551 // consecutivly dividing by 10 and writing down the remainder from right to left.
1552 // Unfortunately the integer is too large to fit in an ulong, so we use something
1553 // like BigInt: An array of ulongs. We only use 60 bits of that ulongs, because
1554 // this simplifies (and speeds up) the division to come.
1555 //
1556 // For the division we use integer division with reminder for each ulong and put
1557 // the reminder of each step in the first 4 bits of ulong of the next step (think of
1558 // long division for the rationale behind this). The final reminder is the next
1559 // digit (from right to left).
1560 //
1561 // Algorithm B:
1562 // The number to print looks like this: zero dot several zeros followed by the mantissa
1563 //
1564 // We know, that the number has no integer part. The algorithm consecutivly multiplies
1565 // by 10. The integer part (rounded down) after the multiplication is the next digit
1566 // (from left to right). This integer part is removed after each step.
1567 // Again, the number is represented as an array of ulongs, with only 60 bits used of
1568 // every ulong.
1569 //
1570 // For the multiplication we use normal integer multiplication, which can result in digits
1571 // in the uppermost 4 bits. These 4 digits are the carry which is added to the result
1572 // of the next multiplication and finally the last carry is the next digit.
1573 //
1574 // The calculation will stop, when only zeros remain or when we've got enough digits
1575 // for the requested precision. In the second case, we have to find out, which rounding
1576 // we have. Aside from special cases we do this by calculating one more digit.
1577 //
1578 // Algorithm C:
1579 // This time, we know, that the integral part and the fractional part each fit into a
1580 // ulong. The mantissa might be partially in both parts or completely in the fractional
1581 // part.
1582 //
1583 // We first calculate the integral part by consecutive division by 10. Then we calculate
1584 // the fractional part by consecutive multiplication by 10. Again only until we have enough
1585 // digits. Finally, we decide the rounding type, mainly by looking at the next digit.
1586
1587 static if (is(T == real) && real.mant_dig == 64)
1588 {
1589 enum small_bound = 0;
1590 enum max_buf = 275;
1591 }
1592 else
1593 {
1594 enum small_bound = T.mant_dig - 61;
1595 static if (is(T == float))
1596 enum max_buf = 4;
1597 else
1598 enum max_buf = 18;
1599 }
1600
1601 size_t start = 2;
1602 size_t left = 2;
1603 size_t right = 2;
1604
1605 ulong[max_buf] bigbuf;
1606 if (exp >= T.mant_dig)
1607 {
1608 left = start = dec_buf.length - 1;
1609 right = dec_buf.length;
1610 dec_buf[start] = '.';
1611
1612 // large number without fractional digits
1613 //
1614 // As this number does not fit in a ulong, we use an array of ulongs. We only use 60 of the 64 bits,
1615 // because this makes it much more easy to implement the division by 10.
1616 int count = exp / 60 + 1;
1617
1618 // only the first few ulongs contain the mantiassa. The rest are zeros.
1619 int lower = 60 - (exp - T.mant_dig + 1) % 60;
1620
1621 static if (is(T == real) && real.mant_dig == 64)
1622 {
1623 // for x87 reals, the lowest ulong may contain more than 60 bits,
1624 // because the mantissa is 63 (>60) bits long
1625 // therefore we need one ulong less
1626 if (lower <= 3) count--;
1627 }
1628
1629 // saved in big endian format
1630 ulong[] mybig = bigbuf[0 .. count];
1631
1632 if (lower < T.mant_dig)
1633 {
1634 mybig[0] = mnt >> lower;
1635 mybig[1] = (mnt & ((1L << lower) - 1)) << 60 - lower;
1636 }
1637 else
1638 mybig[0] = (mnt & ((1L << lower) - 1)) << 60 - lower;
1639
1640 // Generation of digits by consecutive division with reminder by 10.
1641 int msu = 0; // Most significant ulong; when it get's zero, we can ignore it furtheron
1642 while (msu < count - 1 || mybig[$ - 1] != 0)
1643 {
1644 ulong mod = 0;
1645 foreach (i;msu .. count)
1646 {
1647 mybig[i] |= mod << 60;
1648 mod = mybig[i] % 10;
1649 mybig[i] /= 10;
1650 }
1651 if (mybig[msu] == 0)
1652 ++msu;
1653
1654 dec_buf[--left] = cast(byte) ('0' + mod);
1655 }
1656
1657 rc = RoundingClass.ZERO;
1658 }
1659 else if (exp < small_bound)
1660 {
1661 // small number without integer digits
1662 //
1663 // Again this number does not fit in a ulong and we use an array of ulongs. And again we
1664 // only use 60 bits, because this simplifies the multiplication by 10.
1665 int count = (T.mant_dig - exp - 2) / 60 + 1;
1666
1667 // saved in little endian format
1668 ulong[] mybig = bigbuf[0 .. count];
1669
1670 // only the last few ulongs contain the mantiassa. Because of little endian
1671 // format these are the ulongs at index 0 and 1 (and 2 in case of x87 reals).
1672 // The rest are zeros.
1673 int upper = 60 - (-exp - 1) % 60;
1674
1675 static if (is(T == real) && real.mant_dig == 64)
1676 {
1677 if (upper < 4)
1678 {
1679 mybig[0] = (mnt & ((1L << (4 - upper)) - 1)) << 56 + upper;
1680 mybig[1] = (mnt >> (4 - upper)) & ((1L << 60) - 1);
1681 mybig[2] = mnt >> 64 - upper;
1682 }
1683 else
1684 {
1685 mybig[0] = (mnt & ((1L << (T.mant_dig - upper)) - 1)) << 60 - (T.mant_dig - upper);
1686 mybig[1] = mnt >> (T.mant_dig - upper);
1687 }
1688 }
1689 else
1690 {
1691 if (upper < T.mant_dig)
1692 {
1693 mybig[0] = (mnt & ((1L << (T.mant_dig - upper)) - 1)) << 60 - (T.mant_dig - upper);
1694 mybig[1] = mnt >> (T.mant_dig - upper);
1695 }
1696 else
1697 mybig[0] = mnt << (upper - T.mant_dig);
1698 }
1699
1700 dec_buf[--left] = '0'; // 0 left of the dot
1701 dec_buf[right++] = '.';
1702
1703 static if (g)
1704 {
1705 // precision starts at first non zero, so we move start
1706 // to the right, until we found first non zero, thus avoiding
1707 // a premature break of the loop
1708 bool found = false;
1709 start = left + 1;
1710 }
1711
1712 // Generation of digits by consecutive multiplication by 10.
1713 int lsu = 0; // Least significant ulong; when it get's zero, we can ignore it furtheron
1714 while ((lsu < count - 1 || mybig[$ - 1] != 0) && right - start - 1 < f.precision)
1715 {
1716 ulong over = 0;
1717 foreach (i;lsu .. count)
1718 {
1719 mybig[i] = mybig[i] * 10 + over;
1720 over = mybig[i] >> 60;
1721 mybig[i] &= (1L << 60) - 1;
1722 }
1723 if (mybig[lsu] == 0)
1724 ++lsu;
1725
1726 dec_buf[right++] = cast(byte) ('0' + over);
1727
1728 static if (g)
1729 {
1730 if (dec_buf[right - 1] != '0')
1731 found = true;
1732 else if (!found)
1733 start++;
1734 }
1735 }
1736
1737 static if (g) start = 2;
1738
1739 if (lsu >= count - 1 && mybig[count - 1] == 0)
1740 rc = RoundingClass.ZERO;
1741 else if (lsu == count - 1 && mybig[lsu] == 1L << 59)
1742 rc = RoundingClass.FIVE;
1743 else
1744 {
1745 ulong over = 0;
1746 foreach (i;lsu .. count)
1747 {
1748 mybig[i] = mybig[i] * 10 + over;
1749 over = mybig[i] >> 60;
1750 mybig[i] &= (1L << 60) - 1;
1751 }
1752 rc = over >= 5 ? RoundingClass.UPPER : RoundingClass.LOWER;
1753 }
1754 }
1755 else
1756 {
1757 // medium sized number, probably with integer and fractional digits
1758 // this is fastest, because both parts fit into a ulong each
1759 ulong int_part = mnt >> (T.mant_dig - 1 - exp);
1760 ulong frac_part = mnt & ((1L << (T.mant_dig - 1 - exp)) - 1);
1761
1762 // for x87 reals the mantiassa might be up to 3 bits too long
1763 // we need to save these bits as a tail and handle this separately
1764 static if (is(T == real) && real.mant_dig == 64)
1765 {
1766 ulong tail = 0;
1767 ulong tail_length = 0;
1768 if (exp < 3)
1769 {
1770 tail = frac_part & ((1L << (3 - exp)) - 1);
1771 tail_length = 3 - exp;
1772 frac_part >>= 3 - exp;
1773 exp = 3;
1774 }
1775 }
1776
1777 static if (g) auto found = int_part > 0; // searching first non zero
1778
1779 // creating int part
1780 if (int_part == 0)
1781 dec_buf[--left] = '0';
1782 else
1783 {
1784 import core.bitop : bsr;
1785 left = right = start = int_part.bsr * 100 / 332 + 4;
1786
1787 while (int_part > 0)
1788 {
1789 dec_buf[--left] = '0' + (int_part % 10);
1790 int_part /= 10;
1791 }
1792 }
1793
1794 static if (g) size_t save_start = right;
1795
1796 dec_buf[right++] = '.';
1797
1798 // creating frac part
1799 static if (g) start = left + (found ? 0 : 1);
1800 while (frac_part != 0 && right - start - 1 < f.precision)
1801 {
1802 frac_part *= 10;
1803 static if (is(T == real) && real.mant_dig == 64)
1804 {
1805 if (tail_length > 0)
1806 {
1807 // together this is *= 10;
1808 tail *= 5;
1809 tail_length--;
1810
1811 frac_part += tail >> tail_length;
1812 if (tail_length > 0)
1813 tail &= (1L << tail_length) - 1;
1814 }
1815 }
1816 dec_buf[right++] = cast(byte)('0' + (frac_part >> (T.mant_dig - 1 - exp)));
1817
1818 static if (g)
1819 {
1820 if (dec_buf[right - 1] != '0')
1821 found = true;
1822 else if (!found)
1823 start++;
1824 }
1825
1826 frac_part &= ((1L << (T.mant_dig - 1 - exp)) - 1);
1827 }
1828
1829 static if (g) start = save_start;
1830
1831 if (frac_part == 0)
1832 rc = RoundingClass.ZERO;
1833 else
1834 {
1835 frac_part *= 10;
1836 auto nextDigit = frac_part >> (T.mant_dig - 1 - exp);
1837 frac_part &= ((1L << (T.mant_dig - 1 - exp)) - 1);
1838
1839 if (nextDigit == 5 && frac_part == 0)
1840 rc = RoundingClass.FIVE;
1841 else if (nextDigit >= 5)
1842 rc = RoundingClass.UPPER;
1843 else
1844 rc = RoundingClass.LOWER;
1845 }
1846 }
1847
1848 if (round(dec_buf, left, right, rc, sgn == "-")) left--;
1849
1850 while (right > start + 1 && dec_buf[right - 1] == '0') right--;
1851
1852 static if (g)
1853 writeAligned(w, sgn, dec_buf[left .. start], dec_buf[start .. right], "", f, PrecisionType.allDigits);
1854 else
1855 writeAligned(w, sgn, dec_buf[left .. start], dec_buf[start .. right], "", f, PrecisionType.fractionalDigits);
1856 }
1857
1858 @safe unittest
1859 {
1860 auto f = FormatSpec!dchar("");
1861 f.spec = 'f';
1862 assert(printFloat(float.nan, f) == "nan");
1863 assert(printFloat(-float.nan, f) == "-nan");
1864 assert(printFloat(float.infinity, f) == "inf");
1865 assert(printFloat(-float.infinity, f) == "-inf");
1866 assert(printFloat(0.0f, f) == "0.000000");
1867 assert(printFloat(-0.0f, f) == "-0.000000");
1868 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1869 assert(printFloat(cast(float) 1e-40, f) == "0.000000");
1870 assert(printFloat(cast(float) -1e-40, f) == "-0.000000");
1871 assert(printFloat(1e-30f, f) == "0.000000");
1872 assert(printFloat(-1e-30f, f) == "-0.000000");
1873 assert(printFloat(1e-10f, f) == "0.000000");
1874 assert(printFloat(-1e-10f, f) == "-0.000000");
1875 assert(printFloat(0.1f, f) == "0.100000");
1876 assert(printFloat(-0.1f, f) == "-0.100000");
1877 assert(printFloat(10.0f, f) == "10.000000");
1878 assert(printFloat(-10.0f, f) == "-10.000000");
1879 assert(printFloat(1e30f, f) == "1000000015047466219876688855040.000000");
1880 assert(printFloat(-1e30f, f) == "-1000000015047466219876688855040.000000");
1881
1882 import std.math.operations : nextUp, nextDown;
1883 assert(printFloat(nextUp(0.0f), f) == "0.000000");
1884 assert(printFloat(nextDown(-0.0f), f) == "-0.000000");
1885 }
1886
1887 @safe unittest
1888 {
1889 auto f = FormatSpec!dchar("");
1890 f.spec = 'f';
1891 f.width = 20;
1892 f.precision = 10;
1893
1894 assert(printFloat(float.nan, f) == " nan");
1895 assert(printFloat(-float.nan, f) == " -nan");
1896 assert(printFloat(float.infinity, f) == " inf");
1897 assert(printFloat(-float.infinity, f) == " -inf");
1898 assert(printFloat(0.0f, f) == " 0.0000000000");
1899 assert(printFloat(-0.0f, f) == " -0.0000000000");
1900 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1901 assert(printFloat(cast(float) 1e-40, f) == " 0.0000000000");
1902 assert(printFloat(cast(float) -1e-40, f) == " -0.0000000000");
1903 assert(printFloat(1e-30f, f) == " 0.0000000000");
1904 assert(printFloat(-1e-30f, f) == " -0.0000000000");
1905 assert(printFloat(1e-10f, f) == " 0.0000000001");
1906 assert(printFloat(-1e-10f, f) == " -0.0000000001");
1907 assert(printFloat(0.1f, f) == " 0.1000000015");
1908 assert(printFloat(-0.1f, f) == " -0.1000000015");
1909 assert(printFloat(10.0f, f) == " 10.0000000000");
1910 assert(printFloat(-10.0f, f) == " -10.0000000000");
1911 assert(printFloat(1e30f, f) == "1000000015047466219876688855040.0000000000");
1912 assert(printFloat(-1e30f, f) == "-1000000015047466219876688855040.0000000000");
1913
1914 import std.math.operations : nextUp, nextDown;
1915 assert(printFloat(nextUp(0.0f), f) == " 0.0000000000");
1916 assert(printFloat(nextDown(-0.0f), f) == " -0.0000000000");
1917 }
1918
1919 @safe unittest
1920 {
1921 auto f = FormatSpec!dchar("");
1922 f.spec = 'f';
1923 f.width = 20;
1924 f.precision = 10;
1925 f.flDash = true;
1926
1927 assert(printFloat(float.nan, f) == "nan ");
1928 assert(printFloat(-float.nan, f) == "-nan ");
1929 assert(printFloat(float.infinity, f) == "inf ");
1930 assert(printFloat(-float.infinity, f) == "-inf ");
1931 assert(printFloat(0.0f, f) == "0.0000000000 ");
1932 assert(printFloat(-0.0f, f) == "-0.0000000000 ");
1933 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1934 assert(printFloat(cast(float) 1e-40, f) == "0.0000000000 ");
1935 assert(printFloat(cast(float) -1e-40, f) == "-0.0000000000 ");
1936 assert(printFloat(1e-30f, f) == "0.0000000000 ");
1937 assert(printFloat(-1e-30f, f) == "-0.0000000000 ");
1938 assert(printFloat(1e-10f, f) == "0.0000000001 ");
1939 assert(printFloat(-1e-10f, f) == "-0.0000000001 ");
1940 assert(printFloat(0.1f, f) == "0.1000000015 ");
1941 assert(printFloat(-0.1f, f) == "-0.1000000015 ");
1942 assert(printFloat(10.0f, f) == "10.0000000000 ");
1943 assert(printFloat(-10.0f, f) == "-10.0000000000 ");
1944 assert(printFloat(1e30f, f) == "1000000015047466219876688855040.0000000000");
1945 assert(printFloat(-1e30f, f) == "-1000000015047466219876688855040.0000000000");
1946
1947 import std.math.operations : nextUp, nextDown;
1948 assert(printFloat(nextUp(0.0f), f) == "0.0000000000 ");
1949 assert(printFloat(nextDown(-0.0f), f) == "-0.0000000000 ");
1950 }
1951
1952 @safe unittest
1953 {
1954 auto f = FormatSpec!dchar("");
1955 f.spec = 'f';
1956 f.width = 20;
1957 f.precision = 10;
1958 f.flZero = true;
1959
1960 assert(printFloat(float.nan, f) == " nan");
1961 assert(printFloat(-float.nan, f) == " -nan");
1962 assert(printFloat(float.infinity, f) == " inf");
1963 assert(printFloat(-float.infinity, f) == " -inf");
1964 assert(printFloat(0.0f, f) == "000000000.0000000000");
1965 assert(printFloat(-0.0f, f) == "-00000000.0000000000");
1966 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
1967 assert(printFloat(cast(float) 1e-40, f) == "000000000.0000000000");
1968 assert(printFloat(cast(float) -1e-40, f) == "-00000000.0000000000");
1969 assert(printFloat(1e-30f, f) == "000000000.0000000000");
1970 assert(printFloat(-1e-30f, f) == "-00000000.0000000000");
1971 assert(printFloat(1e-10f, f) == "000000000.0000000001");
1972 assert(printFloat(-1e-10f, f) == "-00000000.0000000001");
1973 assert(printFloat(0.1f, f) == "000000000.1000000015");
1974 assert(printFloat(-0.1f, f) == "-00000000.1000000015");
1975 assert(printFloat(10.0f, f) == "000000010.0000000000");
1976 assert(printFloat(-10.0f, f) == "-00000010.0000000000");
1977 assert(printFloat(1e30f, f) == "1000000015047466219876688855040.0000000000");
1978 assert(printFloat(-1e30f, f) == "-1000000015047466219876688855040.0000000000");
1979
1980 import std.math.operations : nextUp, nextDown;
1981 assert(printFloat(nextUp(0.0f), f) == "000000000.0000000000");
1982 assert(printFloat(nextDown(-0.0f), f) == "-00000000.0000000000");
1983 }
1984
1985 @safe unittest
1986 {
1987 import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
1988
1989 // std.math's FloatingPointControl isn't available on all target platforms
1990 static if (is(FloatingPointControl))
1991 {
1992 FloatingPointControl fpctrl;
1993
1994 auto f = FormatSpec!dchar("");
1995 f.spec = 'f';
1996 f.precision = 0;
1997
1998 fpctrl.rounding = FloatingPointControl.roundToNearest;
1999
2000 /*
2001 assert(printFloat(11.5f, f) == "12");
2002 assert(printFloat(12.5f, f) == "13");
2003 assert(printFloat(11.7f, f) == "12");
2004 assert(printFloat(11.3f, f) == "11");
2005 assert(printFloat(11.0f, f) == "11");
2006 assert(printFloat(-11.5f, f) == "-12");
2007 assert(printFloat(-12.5f, f) == "-13");
2008 assert(printFloat(-11.7f, f) == "-12");
2009 assert(printFloat(-11.3f, f) == "-11");
2010 assert(printFloat(-11.0f, f) == "-11");
2011 */
2012
2013 assert(printFloat(11.5f, f) == "12");
2014 assert(printFloat(12.5f, f) == "12");
2015 assert(printFloat(11.7f, f) == "12");
2016 assert(printFloat(11.3f, f) == "11");
2017 assert(printFloat(11.0f, f) == "11");
2018 assert(printFloat(-11.5f, f) == "-12");
2019 assert(printFloat(-12.5f, f) == "-12");
2020 assert(printFloat(-11.7f, f) == "-12");
2021 assert(printFloat(-11.3f, f) == "-11");
2022 assert(printFloat(-11.0f, f) == "-11");
2023
2024 fpctrl.rounding = FloatingPointControl.roundToZero;
2025
2026 assert(printFloat(11.5f, f) == "11");
2027 assert(printFloat(12.5f, f) == "12");
2028 assert(printFloat(11.7f, f) == "11");
2029 assert(printFloat(11.3f, f) == "11");
2030 assert(printFloat(11.0f, f) == "11");
2031 assert(printFloat(-11.5f, f) == "-11");
2032 assert(printFloat(-12.5f, f) == "-12");
2033 assert(printFloat(-11.7f, f) == "-11");
2034 assert(printFloat(-11.3f, f) == "-11");
2035 assert(printFloat(-11.0f, f) == "-11");
2036
2037 fpctrl.rounding = FloatingPointControl.roundUp;
2038
2039 assert(printFloat(11.5f, f) == "12");
2040 assert(printFloat(12.5f, f) == "13");
2041 assert(printFloat(11.7f, f) == "12");
2042 assert(printFloat(11.3f, f) == "12");
2043 assert(printFloat(11.0f, f) == "11");
2044 assert(printFloat(-11.5f, f) == "-11");
2045 assert(printFloat(-12.5f, f) == "-12");
2046 assert(printFloat(-11.7f, f) == "-11");
2047 assert(printFloat(-11.3f, f) == "-11");
2048 assert(printFloat(-11.0f, f) == "-11");
2049
2050 fpctrl.rounding = FloatingPointControl.roundDown;
2051
2052 assert(printFloat(11.5f, f) == "11");
2053 assert(printFloat(12.5f, f) == "12");
2054 assert(printFloat(11.7f, f) == "11");
2055 assert(printFloat(11.3f, f) == "11");
2056 assert(printFloat(11.0f, f) == "11");
2057 assert(printFloat(-11.5f, f) == "-12");
2058 assert(printFloat(-12.5f, f) == "-13");
2059 assert(printFloat(-11.7f, f) == "-12");
2060 assert(printFloat(-11.3f, f) == "-12");
2061 assert(printFloat(-11.0f, f) == "-11");
2062 }
2063 }
2064
2065 @safe unittest
2066 {
2067 auto f = FormatSpec!dchar("");
2068 f.spec = 'f';
2069 assert(printFloat(double.nan, f) == "nan");
2070 assert(printFloat(-double.nan, f) == "-nan");
2071 assert(printFloat(double.infinity, f) == "inf");
2072 assert(printFloat(-double.infinity, f) == "-inf");
2073 assert(printFloat(0.0, f) == "0.000000");
2074 assert(printFloat(-0.0, f) == "-0.000000");
2075 // / 1000 needed due to https://issues.dlang.org/show_bug.cgi?id=20361
2076 assert(printFloat(1e-307 / 1000, f) == "0.000000");
2077 assert(printFloat(-1e-307 / 1000, f) == "-0.000000");
2078 assert(printFloat(1e-30, f) == "0.000000");
2079 assert(printFloat(-1e-30, f) == "-0.000000");
2080 assert(printFloat(1e-10, f) == "0.000000");
2081 assert(printFloat(-1e-10, f) == "-0.000000");
2082 assert(printFloat(0.1, f) == "0.100000");
2083 assert(printFloat(-0.1, f) == "-0.100000");
2084 assert(printFloat(10.0, f) == "10.000000");
2085 assert(printFloat(-10.0, f) == "-10.000000");
2086 assert(printFloat(1e300, f) ==
2087 "100000000000000005250476025520442024870446858110815915491585411551180245798890819578637137508044786"
2088 ~"404370444383288387817694252323536043057564479218478670698284838720092657580373783023379478809005936"
2089 ~"895323497079994508111903896764088007465274278014249457925878882005684283811566947219638686545940054"
2090 ~"0160.000000");
2091 assert(printFloat(-1e300, f) ==
2092 "-100000000000000005250476025520442024870446858110815915491585411551180245798890819578637137508044786"
2093 ~"404370444383288387817694252323536043057564479218478670698284838720092657580373783023379478809005936"
2094 ~"895323497079994508111903896764088007465274278014249457925878882005684283811566947219638686545940054"
2095 ~"0160.000000");
2096
2097 import std.math.operations : nextUp, nextDown;
2098 assert(printFloat(nextUp(0.0), f) == "0.000000");
2099 assert(printFloat(nextDown(-0.0), f) == "-0.000000");
2100 }
2101
2102 @safe unittest
2103 {
2104 static if (real.mant_dig > 64)
2105 {
2106 pragma(msg, "printFloat tests disabled because of unsupported `real` format");
2107 }
2108 else
2109 {
2110 auto f = FormatSpec!dchar("");
2111 f.spec = 'f';
2112 assert(printFloat(real.nan, f) == "nan");
2113 assert(printFloat(-real.nan, f) == "-nan");
2114 assert(printFloat(real.infinity, f) == "inf");
2115 assert(printFloat(-real.infinity, f) == "-inf");
2116 assert(printFloat(0.0L, f) == "0.000000");
2117 assert(printFloat(-0.0L, f) == "-0.000000");
2118 }
2119
2120 static if (real.mant_dig == 64)
2121 {
2122 assert(printFloat(1e-4940L, f) == "0.000000");
2123 assert(printFloat(-1e-4940L, f) == "-0.000000");
2124 assert(printFloat(1e-30L, f) == "0.000000");
2125 assert(printFloat(-1e-30L, f) == "-0.000000");
2126 assert(printFloat(1e-10L, f) == "0.000000");
2127 assert(printFloat(-1e-10L, f) == "-0.000000");
2128 assert(printFloat(0.1L, f) == "0.100000");
2129 assert(printFloat(-0.1L, f) == "-0.100000");
2130 assert(printFloat(10.0L, f) == "10.000000");
2131 assert(printFloat(-10.0L, f) == "-10.000000");
2132 version (Windows) {} // https://issues.dlang.org/show_bug.cgi?id=20972
2133 else
2134 {
2135 auto result1 = printFloat(1e4000L, f);
2136 assert(result1.length == 4007 && result1[0 .. 40] == "9999999999999999999965463873099623784932");
2137 auto result2 = printFloat(-1e4000L, f);
2138 assert(result2.length == 4008 && result2[0 .. 40] == "-999999999999999999996546387309962378493");
2139 }
2140
2141 import std.math.operations : nextUp, nextDown;
2142 assert(printFloat(nextUp(0.0L), f) == "0.000000");
2143 assert(printFloat(nextDown(-0.0L), f) == "-0.000000");
2144 }
2145 }
2146
2147 @safe unittest
2148 {
2149 import std.exception : assertCTFEable;
2150 import std.math.exponential : log2;
2151 import std.math.operations : nextDown;
2152
2153 assertCTFEable!(
2154 {
2155 static if (real.mant_dig == 64) // 80 bit reals
2156 {
2157 // log2 is broken for x87-reals on some computers in CTFE
2158 // the following tests excludes these computers from the tests
2159 // (https://issues.dlang.org/show_bug.cgi?id=21757)
2160 enum test = cast(int) log2(3.05e2312L);
2161 static if (test == 7681)
2162 {
2163 auto f = FormatSpec!dchar("");
2164 f.spec = 'f';
2165 assert(printFloat(real.infinity, f) == "inf");
2166 assert(printFloat(10.0L, f) == "10.000000");
2167 assert(printFloat(2.6080L, f) == "2.608000");
2168 auto result1 = printFloat(3.05e2312L, f);
2169 assert(result1.length == 2320);
2170 assert(result1[0 .. 20] == "30499999999999999999");
2171
2172 f.precision = 60;
2173 assert(printFloat(2.65e-54L, f) ==
2174 "0.000000000000000000000000000000000000000000000000000002650000");
2175
2176 /*
2177 commented out, because CTFE is currently too slow for 5000 digits with extreme values
2178
2179 f.precision = 5000;
2180 auto result2 = printFloat(1.2119e-4822L, f);
2181 assert(result2.length == 5002);
2182 assert(result2[$ - 20 .. $] == "60076763752233836613");
2183 auto result3 = printFloat(real.min_normal, f);
2184 assert(result3.length == 5002);
2185 assert(result3[$ - 20 .. $] == "47124010882722980874");
2186 auto result4 = printFloat(real.min_normal.nextDown, f);
2187 assert(result4.length == 5002);
2188 assert(result4[$ - 20 .. $] == "52925846892214823939");
2189 */
2190 }
2191 }
2192 });
2193 }
2194
2195 @safe unittest
2196 {
2197 auto f = FormatSpec!dchar("");
2198 f.spec = 'f';
2199
2200 import std.math.operations : nextUp;
2201
2202 double eps = nextUp(0.0);
2203 f.precision = 1000;
2204 assert(printFloat(eps, f) ==
2205 "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
2206 ~"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
2207 ~"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
2208 ~"00000000000000000000000000000049406564584124654417656879286822137236505980261432476442558568250067"
2209 ~"55072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131"
2210 ~"90311404527845817167848982103688718636056998730723050006387409153564984387312473397273169615140031"
2211 ~"71538539807412623856559117102665855668676818703956031062493194527159149245532930545654440112748012"
2212 ~"97099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107"
2213 ~"49170333222684475333572083243193609238289345836806010601150616980975307834227731832924790498252473"
2214 ~"07763759272478746560847782037344696995336470179726777175851256605511991315048911014510378627381672"
2215 ~"509558373897335989937");
2216
2217 f.precision = 0;
2218 assert(printFloat(double.max, f) ==
2219 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878"
2220 ~"17154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407"
2221 ~"58685084551339423045832369032229481658085593321233482747978262041447231687381771809192998812504040"
2222 ~"26184124858368");
2223
2224 f.precision = 50;
2225 assert(printFloat(double.epsilon, f) ==
2226 "0.00000000000000022204460492503130808472633361816406");
2227
2228 f.precision = 10;
2229 assert(printFloat(1.0/3.0, f) == "0.3333333333");
2230 assert(printFloat(1.0/7.0, f) == "0.1428571429");
2231 assert(printFloat(1.0/9.0, f) == "0.1111111111");
2232 }
2233
2234 @safe unittest
2235 {
2236 auto f = FormatSpec!dchar("");
2237 f.spec = 'f';
2238 f.precision = 15;
2239
2240 import std.math.constants : E, PI, PI_2, PI_4, M_1_PI, M_2_PI, M_2_SQRTPI,
2241 LN10, LN2, LOG2, LOG2E, LOG2T, LOG10E, SQRT2, SQRT1_2;
2242
2243 assert(printFloat(cast(double) E, f) == "2.718281828459045");
2244 assert(printFloat(cast(double) PI, f) == "3.141592653589793");
2245 assert(printFloat(cast(double) PI_2, f) == "1.570796326794897");
2246 assert(printFloat(cast(double) PI_4, f) == "0.785398163397448");
2247 assert(printFloat(cast(double) M_1_PI, f) == "0.318309886183791");
2248 assert(printFloat(cast(double) M_2_PI, f) == "0.636619772367581");
2249 assert(printFloat(cast(double) M_2_SQRTPI, f) == "1.128379167095513");
2250 assert(printFloat(cast(double) LN10, f) == "2.302585092994046");
2251 assert(printFloat(cast(double) LN2, f) == "0.693147180559945");
2252 assert(printFloat(cast(double) LOG2, f) == "0.301029995663981");
2253 assert(printFloat(cast(double) LOG2E, f) == "1.442695040888963");
2254 assert(printFloat(cast(double) LOG2T, f) == "3.321928094887362");
2255 assert(printFloat(cast(double) LOG10E, f) == "0.434294481903252");
2256 assert(printFloat(cast(double) SQRT2, f) == "1.414213562373095");
2257 assert(printFloat(cast(double) SQRT1_2, f) == "0.707106781186548");
2258 }
2259
2260 // for 100% coverage
2261 @safe unittest
2262 {
2263 auto f = FormatSpec!dchar("");
2264 f.spec = 'f';
2265 f.precision = 1;
2266 assert(printFloat(9.99, f) == "10.0");
2267
2268 import std.math.operations : nextUp;
2269
2270 float eps = nextUp(0.0f);
2271
2272 f.precision = 148;
2273 assert(printFloat(eps, f) ==
2274 "0.0000000000000000000000000000000000000000000014012984643248170709237295832899161312802619418765157"
2275 ~"717570682838897910826858606014866381883621215820312");
2276
2277 f.precision = 149;
2278 assert(printFloat(eps, f) ==
2279 "0.0000000000000000000000000000000000000000000014012984643248170709237295832899161312802619418765157"
2280 ~"7175706828388979108268586060148663818836212158203125");
2281 }
2282
2283 private void printFloatG(Writer, T, Char)(auto ref Writer w, const(T) val,
2284 FormatSpec!Char f, string sgn, int exp, ulong mnt, bool is_upper)
2285 if (is(T == float) || is(T == double)
2286 || (is(T == real) && (T.mant_dig == double.mant_dig || T.mant_dig == 64)))
2287 {
2288 import core.math : abs = fabs;
2289
2290 if (f.precision == f.UNSPECIFIED)
2291 f.precision = 6;
2292
2293 if (f.precision == 0)
2294 f.precision = 1;
2295
2296 import std.math.hardware;
2297 import std.format.internal.write : RoundingMode;
2298
2299 auto rm = RoundingMode.toNearestTiesToEven;
2300
2301 if (!__ctfe)
2302 {
2303 // std.math's FloatingPointControl isn't available on all target platforms
2304 static if (is(FloatingPointControl))
2305 {
2306 switch (FloatingPointControl.rounding)
2307 {
2308 case FloatingPointControl.roundUp:
2309 rm = RoundingMode.up;
2310 break;
2311 case FloatingPointControl.roundDown:
2312 rm = RoundingMode.down;
2313 break;
2314 case FloatingPointControl.roundToZero:
2315 rm = RoundingMode.toZero;
2316 break;
2317 case FloatingPointControl.roundToNearest:
2318 rm = RoundingMode.toNearestTiesToEven;
2319 break;
2320 default: assert(false, "Unknown floating point rounding mode");
2321 }
2322 }
2323 }
2324
2325 bool useE = false;
2326
2327 final switch (rm)
2328 {
2329 case RoundingMode.up:
2330 useE = abs(val) >= 10.0 ^^ f.precision - (val > 0 ? 1 : 0)
2331 || abs(val) < 0.0001 - (val > 0 ? (10.0 ^^ (-4 - f.precision)) : 0);
2332 break;
2333 case RoundingMode.down:
2334 useE = abs(val) >= 10.0 ^^ f.precision - (val < 0 ? 1 : 0)
2335 || abs(val) < 0.0001 - (val < 0 ? (10.0 ^^ (-4 - f.precision)) : 0);
2336 break;
2337 case RoundingMode.toZero:
2338 useE = abs(val) >= 10.0 ^^ f.precision
2339 || abs(val) < 0.0001;
2340 break;
2341 case RoundingMode.toNearestTiesToEven:
2342 case RoundingMode.toNearestTiesAwayFromZero:
2343 useE = abs(val) >= 10.0 ^^ f.precision - 0.5
2344 || abs(val) < 0.0001 - 0.5 * (10.0 ^^ (-4 - f.precision));
2345 break;
2346 }
2347
2348 if (useE)
2349 return printFloatE!true(w, val, f, sgn, exp, mnt, is_upper);
2350 else
2351 return printFloatF!true(w, val, f, sgn, exp, mnt, is_upper);
2352 }
2353
2354 @safe unittest
2355 {
2356 // This one tests the switch between e-like and f-like output.
2357 // There is a small gap left between the two, where the used
2358 // variation is not clearly defined. This is intentional and due
2359 // to the way, D handles floating point numbers. On different
2360 // computers with different reals the results may vary in this gap.
2361
2362 import std.math.operations : nextDown, nextUp;
2363 import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
2364
2365 auto f = FormatSpec!dchar("");
2366 f.spec = 'g';
2367
2368 double val = 999999.5;
2369 assert(printFloat(val.nextUp, f) == "1e+06");
2370 val = nextDown(val);
2371 assert(printFloat(val.nextDown, f) == "999999");
2372
2373 val = 0.00009999995;
2374 assert(printFloat(val.nextUp, f) == "0.0001");
2375 val = nextDown(val);
2376 assert(printFloat(val.nextDown, f) == "9.99999e-05");
2377
2378 static if (is(FloatingPointControl))
2379 {
2380 FloatingPointControl fpctrl;
2381
2382 fpctrl.rounding = FloatingPointControl.roundToZero;
2383
2384 val = 1000000;
2385 assert(printFloat(val.nextUp, f) == "1e+06");
2386 val = nextDown(val);
2387 assert(printFloat(val.nextDown, f) == "999999");
2388
2389 val = 0.0001;
2390 assert(printFloat(val.nextUp, f) == "0.0001");
2391 val = nextDown(val);
2392 assert(printFloat(val.nextDown, f) == "9.99999e-05");
2393
2394 fpctrl.rounding = FloatingPointControl.roundUp;
2395
2396 val = 999999;
2397 assert(printFloat(val.nextUp, f) == "1e+06");
2398 val = nextDown(val);
2399 assert(printFloat(val.nextDown, f) == "999999");
2400
2401 // 0.0000999999 is actually represented as 0.0000999998999..., which is
2402 // less than 0.0000999999, so we need to use nextUp to get the corner case here
2403 val = nextUp(0.0000999999);
2404 assert(printFloat(val.nextUp, f) == "0.0001");
2405 val = nextDown(val);
2406 assert(printFloat(val.nextDown, f) == "9.99999e-05");
2407
2408 fpctrl.rounding = FloatingPointControl.roundDown;
2409
2410 val = 1000000;
2411 assert(printFloat(val.nextUp, f) == "1e+06");
2412 val = nextDown(val);
2413 assert(printFloat(val.nextDown, f) == "999999");
2414
2415 val = 0.0001;
2416 assert(printFloat(val.nextUp, f) == "0.0001");
2417 val = nextDown(val);
2418 assert(printFloat(val.nextDown, f) == "9.99999e-05");
2419 }
2420 }
2421
2422 @safe unittest
2423 {
2424 auto f = FormatSpec!dchar("");
2425 f.spec = 'g';
2426 assert(printFloat(float.nan, f) == "nan");
2427 assert(printFloat(-float.nan, f) == "-nan");
2428 assert(printFloat(float.infinity, f) == "inf");
2429 assert(printFloat(-float.infinity, f) == "-inf");
2430 assert(printFloat(0.0f, f) == "0");
2431 assert(printFloat(-0.0f, f) == "-0");
2432
2433 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
2434 assert(printFloat(cast(float) 1e-40, f) == "9.99995e-41");
2435 assert(printFloat(cast(float) -1e-40, f) == "-9.99995e-41");
2436 assert(printFloat(1e-30f, f) == "1e-30");
2437 assert(printFloat(-1e-30f, f) == "-1e-30");
2438 assert(printFloat(1e-10f, f) == "1e-10");
2439 assert(printFloat(-1e-10f, f) == "-1e-10");
2440 assert(printFloat(0.1f, f) == "0.1");
2441 assert(printFloat(-0.1f, f) == "-0.1");
2442 assert(printFloat(10.0f, f) == "10");
2443 assert(printFloat(-10.0f, f) == "-10");
2444 assert(printFloat(1e30f, f) == "1e+30");
2445 assert(printFloat(-1e30f, f) == "-1e+30");
2446
2447 import std.math.operations : nextUp, nextDown;
2448 assert(printFloat(nextUp(0.0f), f) == "1.4013e-45");
2449 assert(printFloat(nextDown(-0.0f), f) == "-1.4013e-45");
2450 }
2451
2452 @safe unittest
2453 {
2454 auto f = FormatSpec!dchar("");
2455 f.spec = 'g';
2456 f.width = 20;
2457 f.precision = 10;
2458
2459 assert(printFloat(float.nan, f) == " nan");
2460 assert(printFloat(-float.nan, f) == " -nan");
2461 assert(printFloat(float.infinity, f) == " inf");
2462 assert(printFloat(-float.infinity, f) == " -inf");
2463 assert(printFloat(0.0f, f) == " 0");
2464 assert(printFloat(-0.0f, f) == " -0");
2465 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
2466 assert(printFloat(cast(float) 1e-40, f) == " 9.999946101e-41");
2467 assert(printFloat(cast(float) -1e-40, f) == " -9.999946101e-41");
2468 assert(printFloat(1e-30f, f) == " 1.000000003e-30");
2469 assert(printFloat(-1e-30f, f) == " -1.000000003e-30");
2470 assert(printFloat(1e-10f, f) == " 1.000000013e-10");
2471 assert(printFloat(-1e-10f, f) == " -1.000000013e-10");
2472 assert(printFloat(0.1f, f) == " 0.1000000015");
2473 assert(printFloat(-0.1f, f) == " -0.1000000015");
2474 assert(printFloat(10.0f, f) == " 10");
2475 assert(printFloat(-10.0f, f) == " -10");
2476 assert(printFloat(1e30f, f) == " 1.000000015e+30");
2477 assert(printFloat(-1e30f, f) == " -1.000000015e+30");
2478
2479 import std.math.operations : nextUp, nextDown;
2480 assert(printFloat(nextUp(0.0f), f) == " 1.401298464e-45");
2481 assert(printFloat(nextDown(-0.0f), f) == " -1.401298464e-45");
2482 }
2483
2484 @safe unittest
2485 {
2486 auto f = FormatSpec!dchar("");
2487 f.spec = 'g';
2488 f.width = 20;
2489 f.precision = 10;
2490 f.flDash = true;
2491
2492 assert(printFloat(float.nan, f) == "nan ");
2493 assert(printFloat(-float.nan, f) == "-nan ");
2494 assert(printFloat(float.infinity, f) == "inf ");
2495 assert(printFloat(-float.infinity, f) == "-inf ");
2496 assert(printFloat(0.0f, f) == "0 ");
2497 assert(printFloat(-0.0f, f) == "-0 ");
2498
2499 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
2500 assert(printFloat(cast(float) 1e-40, f) == "9.999946101e-41 ");
2501 assert(printFloat(cast(float) -1e-40, f) == "-9.999946101e-41 ");
2502 assert(printFloat(1e-30f, f) == "1.000000003e-30 ");
2503 assert(printFloat(-1e-30f, f) == "-1.000000003e-30 ");
2504 assert(printFloat(1e-10f, f) == "1.000000013e-10 ");
2505 assert(printFloat(-1e-10f, f) == "-1.000000013e-10 ");
2506 assert(printFloat(0.1f, f) == "0.1000000015 ");
2507 assert(printFloat(-0.1f, f) == "-0.1000000015 ");
2508 assert(printFloat(10.0f, f) == "10 ");
2509 assert(printFloat(-10.0f, f) == "-10 ");
2510 assert(printFloat(1e30f, f) == "1.000000015e+30 ");
2511 assert(printFloat(-1e30f, f) == "-1.000000015e+30 ");
2512
2513 import std.math.operations : nextUp, nextDown;
2514 assert(printFloat(nextUp(0.0f), f) == "1.401298464e-45 ");
2515 assert(printFloat(nextDown(-0.0f), f) == "-1.401298464e-45 ");
2516 }
2517
2518 @safe unittest
2519 {
2520 auto f = FormatSpec!dchar("");
2521 f.spec = 'g';
2522 f.width = 20;
2523 f.precision = 10;
2524 f.flZero = true;
2525
2526 assert(printFloat(float.nan, f) == " nan");
2527 assert(printFloat(-float.nan, f) == " -nan");
2528 assert(printFloat(float.infinity, f) == " inf");
2529 assert(printFloat(-float.infinity, f) == " -inf");
2530 assert(printFloat(0.0f, f) == "00000000000000000000");
2531 assert(printFloat(-0.0f, f) == "-0000000000000000000");
2532
2533 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
2534 assert(printFloat(cast(float) 1e-40, f) == "000009.999946101e-41");
2535 assert(printFloat(cast(float) -1e-40, f) == "-00009.999946101e-41");
2536 assert(printFloat(1e-30f, f) == "000001.000000003e-30");
2537 assert(printFloat(-1e-30f, f) == "-00001.000000003e-30");
2538 assert(printFloat(1e-10f, f) == "000001.000000013e-10");
2539 assert(printFloat(-1e-10f, f) == "-00001.000000013e-10");
2540 assert(printFloat(0.1f, f) == "000000000.1000000015");
2541 assert(printFloat(-0.1f, f) == "-00000000.1000000015");
2542 assert(printFloat(10.0f, f) == "00000000000000000010");
2543 assert(printFloat(-10.0f, f) == "-0000000000000000010");
2544 assert(printFloat(1e30f, f) == "000001.000000015e+30");
2545 assert(printFloat(-1e30f, f) == "-00001.000000015e+30");
2546
2547 import std.math.operations : nextUp, nextDown;
2548 assert(printFloat(nextUp(0.0f), f) == "000001.401298464e-45");
2549 assert(printFloat(nextDown(-0.0f), f) == "-00001.401298464e-45");
2550 }
2551
2552 @safe unittest
2553 {
2554 auto f = FormatSpec!dchar("");
2555 f.spec = 'g';
2556 f.precision = 10;
2557 f.flHash = true;
2558
2559 assert(printFloat(float.nan, f) == "nan");
2560 assert(printFloat(-float.nan, f) == "-nan");
2561 assert(printFloat(float.infinity, f) == "inf");
2562 assert(printFloat(-float.infinity, f) == "-inf");
2563 assert(printFloat(0.0f, f) == "0.000000000");
2564 assert(printFloat(-0.0f, f) == "-0.000000000");
2565
2566 // cast needed due to https://issues.dlang.org/show_bug.cgi?id=20361
2567 assert(printFloat(cast(float) 1e-40, f) == "9.999946101e-41");
2568 assert(printFloat(cast(float) -1e-40, f) == "-9.999946101e-41");
2569 assert(printFloat(1e-30f, f) == "1.000000003e-30");
2570 assert(printFloat(-1e-30f, f) == "-1.000000003e-30");
2571 assert(printFloat(1e-10f, f) == "1.000000013e-10");
2572 assert(printFloat(-1e-10f, f) == "-1.000000013e-10");
2573 assert(printFloat(0.1f, f) == "0.1000000015");
2574 assert(printFloat(-0.1f, f) == "-0.1000000015");
2575 assert(printFloat(10.0f, f) == "10.00000000");
2576 assert(printFloat(-10.0f, f) == "-10.00000000");
2577 assert(printFloat(1e30f, f) == "1.000000015e+30");
2578 assert(printFloat(-1e30f, f) == "-1.000000015e+30");
2579
2580 import std.math.operations : nextUp, nextDown;
2581 assert(printFloat(nextUp(0.0f), f) == "1.401298464e-45");
2582 assert(printFloat(nextDown(-0.0f), f) == "-1.401298464e-45");
2583 }
2584
2585 @safe unittest
2586 {
2587 import std.math.hardware; // cannot be selective, because FloatingPointControl might not be defined
2588
2589 // std.math's FloatingPointControl isn't available on all target platforms
2590 static if (is(FloatingPointControl))
2591 {
2592 FloatingPointControl fpctrl;
2593
2594 char[256] buf;
2595 auto f = FormatSpec!dchar("");
2596 f.spec = 'g';
2597 f.precision = 2;
2598
2599 fpctrl.rounding = FloatingPointControl.roundToNearest;
2600
2601 /*
2602 assert(printFloat(11.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "12");
2603 assert(printFloat(12.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "13");
2604 assert(printFloat(11.7f, f, RoundingMode.toNearestTiesAwayFromZero) == "12");
2605 assert(printFloat(11.3f, f, RoundingMode.toNearestTiesAwayFromZero) == "11");
2606 assert(printFloat(11.0f, f, RoundingMode.toNearestTiesAwayFromZero) == "11");
2607 assert(printFloat(-11.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "-12");
2608 assert(printFloat(-12.5f, f, RoundingMode.toNearestTiesAwayFromZero) == "-13");
2609 assert(printFloat(-11.7f, f, RoundingMode.toNearestTiesAwayFromZero) == "-12");
2610 assert(printFloat(-11.3f, f, RoundingMode.toNearestTiesAwayFromZero) == "-11");
2611 assert(printFloat(-11.0f, f, RoundingMode.toNearestTiesAwayFromZero) == "-11");
2612 */
2613
2614 // ties to even
2615 assert(printFloat(11.5f, f) == "12");
2616 assert(printFloat(12.5f, f) == "12");
2617 assert(printFloat(11.7f, f) == "12");
2618 assert(printFloat(11.3f, f) == "11");
2619 assert(printFloat(11.0f, f) == "11");
2620 assert(printFloat(-11.5f, f) == "-12");
2621 assert(printFloat(-12.5f, f) == "-12");
2622 assert(printFloat(-11.7f, f) == "-12");
2623 assert(printFloat(-11.3f, f) == "-11");
2624 assert(printFloat(-11.0f, f) == "-11");
2625
2626 fpctrl.rounding = FloatingPointControl.roundToZero;
2627
2628 assert(printFloat(11.5f, f) == "11");
2629 assert(printFloat(12.5f, f) == "12");
2630 assert(printFloat(11.7f, f) == "11");
2631 assert(printFloat(11.3f, f) == "11");
2632 assert(printFloat(11.0f, f) == "11");
2633 assert(printFloat(-11.5f, f) == "-11");
2634 assert(printFloat(-12.5f, f) == "-12");
2635 assert(printFloat(-11.7f, f) == "-11");
2636 assert(printFloat(-11.3f, f) == "-11");
2637 assert(printFloat(-11.0f, f) == "-11");
2638
2639 fpctrl.rounding = FloatingPointControl.roundUp;
2640
2641 assert(printFloat(11.5f, f) == "12");
2642 assert(printFloat(12.5f, f) == "13");
2643 assert(printFloat(11.7f, f) == "12");
2644 assert(printFloat(11.3f, f) == "12");
2645 assert(printFloat(11.0f, f) == "11");
2646 assert(printFloat(-11.5f, f) == "-11");
2647 assert(printFloat(-12.5f, f) == "-12");
2648 assert(printFloat(-11.7f, f) == "-11");
2649 assert(printFloat(-11.3f, f) == "-11");
2650 assert(printFloat(-11.0f, f) == "-11");
2651
2652 fpctrl.rounding = FloatingPointControl.roundDown;
2653
2654 assert(printFloat(11.5f, f) == "11");
2655 assert(printFloat(12.5f, f) == "12");
2656 assert(printFloat(11.7f, f) == "11");
2657 assert(printFloat(11.3f, f) == "11");
2658 assert(printFloat(11.0f, f) == "11");
2659 assert(printFloat(-11.5f, f) == "-12");
2660 assert(printFloat(-12.5f, f) == "-13");
2661 assert(printFloat(-11.7f, f) == "-12");
2662 assert(printFloat(-11.3f, f) == "-12");
2663 assert(printFloat(-11.0f, f) == "-11");
2664 }
2665 }
2666
2667 @safe unittest
2668 {
2669 auto f = FormatSpec!dchar("");
2670 f.spec = 'g';
2671
2672 assert(printFloat(double.nan, f) == "nan");
2673 assert(printFloat(-double.nan, f) == "-nan");
2674 assert(printFloat(double.infinity, f) == "inf");
2675 assert(printFloat(-double.infinity, f) == "-inf");
2676 assert(printFloat(0.0, f) == "0");
2677 assert(printFloat(-0.0, f) == "-0");
2678
2679 // / 1000 needed due to https://issues.dlang.org/show_bug.cgi?id=20361
2680 assert(printFloat(1e-307 / 1000, f) == "1e-310");
2681 assert(printFloat(-1e-307 / 1000, f) == "-1e-310");
2682 assert(printFloat(1e-30, f) == "1e-30");
2683 assert(printFloat(-1e-30, f) == "-1e-30");
2684 assert(printFloat(1e-10, f) == "1e-10");
2685 assert(printFloat(-1e-10, f) == "-1e-10");
2686 assert(printFloat(0.1, f) == "0.1");
2687 assert(printFloat(-0.1, f) == "-0.1");
2688 assert(printFloat(10.0, f) == "10");
2689 assert(printFloat(-10.0, f) == "-10");
2690 assert(printFloat(1e300, f) == "1e+300");
2691 assert(printFloat(-1e300, f) == "-1e+300");
2692
2693 import std.math.operations : nextUp, nextDown;
2694 assert(printFloat(nextUp(0.0), f) == "4.94066e-324");
2695 assert(printFloat(nextDown(-0.0), f) == "-4.94066e-324");
2696 }
2697
2698 @safe unittest
2699 {
2700 static if (real.mant_dig > 64)
2701 {
2702 pragma(msg, "printFloat tests disabled because of unsupported `real` format");
2703 }
2704 else
2705 {
2706 char[256] buf;
2707 auto f = FormatSpec!dchar("");
2708 f.spec = 'g';
2709
2710 assert(printFloat(real.nan, f) == "nan");
2711 assert(printFloat(-real.nan, f) == "-nan");
2712 assert(printFloat(real.infinity, f) == "inf");
2713 assert(printFloat(-real.infinity, f) == "-inf");
2714 }
2715 }
2716
2717 @safe unittest
2718 {
2719 auto f = FormatSpec!dchar("");
2720 f.spec = 'g';
2721
2722 import std.math.operations : nextUp;
2723
2724 double eps = nextUp(0.0);
2725 f.precision = 1000;
2726 assert(printFloat(eps, f) ==
2727 "4.940656458412465441765687928682213723650598026143247644255856825006"
2728 ~ "755072702087518652998363616359923797965646954457177309266567103559"
2729 ~ "397963987747960107818781263007131903114045278458171678489821036887"
2730 ~ "186360569987307230500063874091535649843873124733972731696151400317"
2731 ~ "153853980741262385655911710266585566867681870395603106249319452715"
2732 ~ "914924553293054565444011274801297099995419319894090804165633245247"
2733 ~ "571478690147267801593552386115501348035264934720193790268107107491"
2734 ~ "703332226844753335720832431936092382893458368060106011506169809753"
2735 ~ "078342277318329247904982524730776375927247874656084778203734469699"
2736 ~ "533647017972677717585125660551199131504891101451037862738167250955"
2737 ~ "837389733598993664809941164205702637090279242767544565229087538682"
2738 ~ "506419718265533447265625e-324");
2739
2740 f.precision = 50;
2741 assert(printFloat(double.max, f) ==
2742 "1.7976931348623157081452742373170435679807056752584e+308");
2743 assert(printFloat(double.epsilon, f) ==
2744 "2.220446049250313080847263336181640625e-16");
2745
2746 f.precision = 10;
2747 assert(printFloat(1.0/3.0, f) == "0.3333333333");
2748 assert(printFloat(1.0/7.0, f) == "0.1428571429");
2749 assert(printFloat(1.0/9.0, f) == "0.1111111111");
2750 }
2751
2752 @safe unittest
2753 {
2754 auto f = FormatSpec!dchar("");
2755 f.spec = 'g';
2756 f.precision = 15;
2757
2758 import std.math.constants : E, PI, PI_2, PI_4, M_1_PI, M_2_PI, M_2_SQRTPI,
2759 LN10, LN2, LOG2, LOG2E, LOG2T, LOG10E, SQRT2, SQRT1_2;
2760
2761 assert(printFloat(cast(double) E, f) == "2.71828182845905");
2762 assert(printFloat(cast(double) PI, f) == "3.14159265358979");
2763 assert(printFloat(cast(double) PI_2, f) == "1.5707963267949");
2764 assert(printFloat(cast(double) PI_4, f) == "0.785398163397448");
2765 assert(printFloat(cast(double) M_1_PI, f) == "0.318309886183791");
2766 assert(printFloat(cast(double) M_2_PI, f) == "0.636619772367581");
2767 assert(printFloat(cast(double) M_2_SQRTPI, f) == "1.12837916709551");
2768 assert(printFloat(cast(double) LN10, f) == "2.30258509299405");
2769 assert(printFloat(cast(double) LN2, f) == "0.693147180559945");
2770 assert(printFloat(cast(double) LOG2, f) == "0.301029995663981");
2771 assert(printFloat(cast(double) LOG2E, f) == "1.44269504088896");
2772 assert(printFloat(cast(double) LOG2T, f) == "3.32192809488736");
2773 assert(printFloat(cast(double) LOG10E, f) == "0.434294481903252");
2774 assert(printFloat(cast(double) SQRT2, f) == "1.4142135623731");
2775 assert(printFloat(cast(double) SQRT1_2, f) == "0.707106781186548");
2776 }
2777
2778 // for 100% coverage
2779 @safe unittest
2780 {
2781 auto f = FormatSpec!dchar("");
2782 f.spec = 'g';
2783 f.precision = 0;
2784
2785 assert(printFloat(0.009999, f) == "0.01");
2786 }
2787
2788 @safe unittest
2789 {
2790 static if (real.mant_dig > 64)
2791 {
2792 pragma(msg, "printFloat tests disabled because of unsupported `real` format");
2793 }
2794 else
2795 {
2796 auto f = FormatSpec!dchar("");
2797 f.spec = 'g';
2798 assert(printFloat(real.nan, f) == "nan");
2799 assert(printFloat(-real.nan, f) == "-nan");
2800 assert(printFloat(real.infinity, f) == "inf");
2801 assert(printFloat(-real.infinity, f) == "-inf");
2802 assert(printFloat(0.0L, f) == "0");
2803 assert(printFloat(-0.0L, f) == "-0");
2804 }
2805
2806 static if (real.mant_dig == 64)
2807 {
2808 assert(printFloat(1e-4940L, f) == "1e-4940");
2809 assert(printFloat(-1e-4940L, f) == "-1e-4940");
2810 assert(printFloat(1e-30L, f) == "1e-30");
2811 assert(printFloat(-1e-30L, f) == "-1e-30");
2812 assert(printFloat(1e-10L, f) == "1e-10");
2813 assert(printFloat(-1e-10L, f) == "-1e-10");
2814 assert(printFloat(0.1L, f) == "0.1");
2815 assert(printFloat(-0.1L, f) == "-0.1");
2816 assert(printFloat(10.0L, f) == "10");
2817 assert(printFloat(-10.0L, f) == "-10");
2818 version (Windows) {} // https://issues.dlang.org/show_bug.cgi?id=20972
2819 else
2820 {
2821 assert(printFloat(1e4000L, f) == "1e+4000");
2822 assert(printFloat(-1e4000L, f) == "-1e+4000");
2823 }
2824
2825 import std.math.operations : nextUp, nextDown;
2826 assert(printFloat(nextUp(0.0L), f) == "3.6452e-4951");
2827 assert(printFloat(nextDown(-0.0L), f) == "-3.6452e-4951");
2828 }
2829 }
2830
2831 @safe unittest
2832 {
2833 import std.exception : assertCTFEable;
2834 import std.math.exponential : log2;
2835 import std.math.operations : nextDown;
2836
2837 assertCTFEable!(
2838 {
2839 static if (real.mant_dig == 64) // 80 bit reals
2840 {
2841 // log2 is broken for x87-reals on some computers in CTFE
2842 // the following tests excludes these computers from the tests
2843 // (https://issues.dlang.org/show_bug.cgi?id=21757)
2844 enum test = cast(int) log2(3.05e2312L);
2845 static if (test == 7681)
2846 {
2847 auto f = FormatSpec!dchar("");
2848 f.spec = 'g';
2849 assert(printFloat(real.infinity, f) == "inf");
2850 assert(printFloat(10.0L, f) == "10");
2851 assert(printFloat(2.6080L, f) == "2.608");
2852 assert(printFloat(3.05e2312L, f) == "3.05e+2312");
2853
2854 f.precision = 60;
2855 assert(printFloat(2.65e-54L, f) ==
2856 "2.65000000000000000005900998740054701394102894093529654759941e-54");
2857
2858 /*
2859 commented out, because CTFE is currently too slow for 5000 digits with extreme values
2860
2861 f.precision = 5000;
2862 auto result2 = printFloat(1.2119e-4822L, f);
2863 assert(result2.length == 5007);
2864 assert(result2[$ - 20 .. $] == "26072948659534e-4822");
2865 auto result3 = printFloat(real.min_normal, f);
2866 assert(result3.length == 5007);
2867 assert(result3[$ - 20 .. $] == "72078141008227e-4932");
2868 auto result4 = printFloat(real.min_normal.nextDown, f);
2869 assert(result4.length == 5007);
2870 assert(result4[$ - 20 .. $] == "48141326333101e-4932");
2871 */
2872 }
2873 }
2874 });
2875 }
2876
2877 // check no allocations
2878 @safe unittest
2879 {
2880 import std.format : NoOpSink;
2881 auto w = NoOpSink();
2882
2883 import core.memory;
2884 auto stats = () @trusted { return GC.stats; } ();
2885
2886 auto f = FormatSpec!dchar("");
2887 f.spec = 'a';
2888 printFloat(w, float.nan, f);
2889 printFloat(w, -float.infinity, f);
2890 printFloat(w, 0.0f, f);
2891
2892 printFloat(w, -double.nan, f);
2893 printFloat(w, double.infinity, f);
2894 printFloat(w, -0.0, f);
2895
2896 import std.math.operations : nextUp;
2897 import std.math.constants : E;
2898
2899 printFloat(w, nextUp(0.0f), f);
2900 printFloat(w, cast(float) E, f);
2901
2902 f.precision = 1000;
2903 printFloat(w, float.nan, f);
2904 printFloat(w, 0.0, f);
2905 printFloat(w, 1.23456789e+100, f);
2906
2907 f.spec = 'E';
2908 f.precision = 80;
2909 printFloat(w, 5.62776e+12f, f);
2910
2911 f.precision = 6;
2912 printFloat(w, -1.1418613e+07f, f);
2913
2914 f.precision = 20;
2915 printFloat(w, double.max, f);
2916 printFloat(w, nextUp(0.0), f);
2917
2918 f.precision = 1000;
2919 printFloat(w, 1.0, f);
2920
2921 f.spec = 'f';
2922 f.precision = 15;
2923 printFloat(w, cast(double) E, f);
2924
2925 f.precision = 20;
2926 printFloat(w, double.max, f);
2927 printFloat(w, nextUp(0.0), f);
2928
2929 f.precision = 1000;
2930 printFloat(w, 1.0, f);
2931
2932 f.spec = 'g';
2933 f.precision = 15;
2934 printFloat(w, cast(double) E, f);
2935
2936 f.precision = 20;
2937 printFloat(w, double.max, f);
2938 printFloat(w, nextUp(0.0), f);
2939
2940 f.flHash = true;
2941 f.precision = 1000;
2942 printFloat(w, 1.0, f);
2943
2944 assert(() @trusted { return GC.stats.usedSize; } () == stats.usedSize);
2945 }