Debian Codegraph @

libphobos/src/std/math/operations.d

raw
1 // Written in the D programming language.
2
3 /**
4 This is a submodule of $(MREF std, math).
5
6 It contains several functions for work with floating point numbers.
7
8 Copyright: Copyright The D Language Foundation 2000 - 2011.
9 License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
10 Authors: $(HTTP digitalmars.com, Walter Bright), Don Clugston,
11 Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger
12 Source: $(PHOBOSSRC std/math/operations.d)
13
14 Macros:
15 TABLE_SV = <table border="1" cellpadding="4" cellspacing="0">
16 <caption>Special Values</caption>
17 $0</table>
18 SVH = $(TR $(TH $1) $(TH $2))
19 SV = $(TR $(TD $1) $(TD $2))
20 NAN = $(RED NAN)
21 PLUSMN = &plusmn;
22 INFIN = &infin;
23 LT = &lt;
24 GT = &gt;
25 */
26
27 module std.math.operations;
28
29 import std.traits : CommonType, isFloatingPoint, isIntegral, Unqual;
30
31 // Functions for NaN payloads
32 /*
33 * A 'payload' can be stored in the significand of a $(NAN). One bit is required
34 * to distinguish between a quiet and a signalling $(NAN). This leaves 22 bits
35 * of payload for a float; 51 bits for a double; 62 bits for an 80-bit real;
36 * and 111 bits for a 128-bit quad.
37 */
38 /**
39 * Create a quiet $(NAN), storing an integer inside the payload.
40 *
41 * For floats, the largest possible payload is 0x3F_FFFF.
42 * For doubles, it is 0x3_FFFF_FFFF_FFFF.
43 * For 80-bit or 128-bit reals, it is 0x3FFF_FFFF_FFFF_FFFF.
44 */
45 real NaN(ulong payload) @trusted pure nothrow @nogc
46 {
47 import std.math.traits : floatTraits, RealFormat;
48
49 alias F = floatTraits!(real);
50 static if (F.realFormat == RealFormat.ieeeExtended ||
51 F.realFormat == RealFormat.ieeeExtended53)
52 {
53 // real80 (in x86 real format, the implied bit is actually
54 // not implied but a real bit which is stored in the real)
55 ulong v = 3; // implied bit = 1, quiet bit = 1
56 }
57 else
58 {
59 ulong v = 1; // no implied bit. quiet bit = 1
60 }
61 if (__ctfe)
62 {
63 v = 1; // We use a double in CTFE.
64 assert(payload >>> 51 == 0,
65 "Cannot set more than 51 bits of NaN payload in CTFE.");
66 }
67
68
69 ulong a = payload;
70
71 // 22 Float bits
72 ulong w = a & 0x3F_FFFF;
73 a -= w;
74
75 v <<=22;
76 v |= w;
77 a >>=22;
78
79 // 29 Double bits
80 v <<=29;
81 w = a & 0xFFF_FFFF;
82 v |= w;
83 a -= w;
84 a >>=29;
85
86 if (__ctfe)
87 {
88 v |= 0x7FF0_0000_0000_0000;
89 return *cast(double*) &v;
90 }
91 else static if (F.realFormat == RealFormat.ieeeDouble)
92 {
93 v |= 0x7FF0_0000_0000_0000;
94 real x;
95 * cast(ulong *)(&x) = v;
96 return x;
97 }
98 else
99 {
100 v <<=11;
101 a &= 0x7FF;
102 v |= a;
103 real x = real.nan;
104
105 // Extended real bits
106 static if (F.realFormat == RealFormat.ieeeQuadruple)
107 {
108 v <<= 1; // there's no implicit bit
109
110 version (LittleEndian)
111 {
112 *cast(ulong*)(6+cast(ubyte*)(&x)) = v;
113 }
114 else
115 {
116 *cast(ulong*)(2+cast(ubyte*)(&x)) = v;
117 }
118 }
119 else
120 {
121 *cast(ulong *)(&x) = v;
122 }
123 return x;
124 }
125 }
126
127 ///
128 @safe @nogc pure nothrow unittest
129 {
130 import std.math.traits : isNaN;
131
132 real a = NaN(1_000_000);
133 assert(isNaN(a));
134 assert(getNaNPayload(a) == 1_000_000);
135 }
136
137 @system pure nothrow @nogc unittest // not @safe because taking address of local.
138 {
139 import std.math.traits : floatTraits, RealFormat;
140
141 static if (floatTraits!(real).realFormat == RealFormat.ieeeDouble)
142 {
143 auto x = NaN(1);
144 auto xl = *cast(ulong*)&x;
145 assert(xl & 0x8_0000_0000_0000UL); //non-signaling bit, bit 52
146 assert((xl & 0x7FF0_0000_0000_0000UL) == 0x7FF0_0000_0000_0000UL); //all exp bits set
147 }
148 }
149
150 /**
151 * Extract an integral payload from a $(NAN).
152 *
153 * Returns:
154 * the integer payload as a ulong.
155 *
156 * For floats, the largest possible payload is 0x3F_FFFF.
157 * For doubles, it is 0x3_FFFF_FFFF_FFFF.
158 * For 80-bit or 128-bit reals, it is 0x3FFF_FFFF_FFFF_FFFF.
159 */
160 ulong getNaNPayload(real x) @trusted pure nothrow @nogc
161 {
162 import std.math.traits : floatTraits, RealFormat;
163
164 // assert(isNaN(x));
165 alias F = floatTraits!(real);
166 ulong m = void;
167 if (__ctfe)
168 {
169 double y = x;
170 m = *cast(ulong*) &y;
171 // Make it look like an 80-bit significand.
172 // Skip exponent, and quiet bit
173 m &= 0x0007_FFFF_FFFF_FFFF;
174 m <<= 11;
175 }
176 else static if (F.realFormat == RealFormat.ieeeDouble)
177 {
178 m = *cast(ulong*)(&x);
179 // Make it look like an 80-bit significand.
180 // Skip exponent, and quiet bit
181 m &= 0x0007_FFFF_FFFF_FFFF;
182 m <<= 11;
183 }
184 else static if (F.realFormat == RealFormat.ieeeQuadruple)
185 {
186 version (LittleEndian)
187 {
188 m = *cast(ulong*)(6+cast(ubyte*)(&x));
189 }
190 else
191 {
192 m = *cast(ulong*)(2+cast(ubyte*)(&x));
193 }
194
195 m >>= 1; // there's no implicit bit
196 }
197 else
198 {
199 m = *cast(ulong*)(&x);
200 }
201
202 // ignore implicit bit and quiet bit
203
204 const ulong f = m & 0x3FFF_FF00_0000_0000L;
205
206 ulong w = f >>> 40;
207 w |= (m & 0x00FF_FFFF_F800L) << (22 - 11);
208 w |= (m & 0x7FF) << 51;
209 return w;
210 }
211
212 ///
213 @safe @nogc pure nothrow unittest
214 {
215 import std.math.traits : isNaN;
216
217 real a = NaN(1_000_000);
218 assert(isNaN(a));
219 assert(getNaNPayload(a) == 1_000_000);
220 }
221
222 @safe @nogc pure nothrow unittest
223 {
224 import std.math.traits : isIdentical, isNaN;
225
226 enum real a = NaN(1_000_000);
227 static assert(isNaN(a));
228 static assert(getNaNPayload(a) == 1_000_000);
229 real b = NaN(1_000_000);
230 assert(isIdentical(b, a));
231 // The CTFE version of getNaNPayload relies on it being impossible
232 // for a CTFE-constructed NaN to have more than 51 bits of payload.
233 enum nanNaN = NaN(getNaNPayload(real.nan));
234 assert(isIdentical(real.nan, nanNaN));
235 static if (real.init != real.init)
236 {
237 enum initNaN = NaN(getNaNPayload(real.init));
238 assert(isIdentical(real.init, initNaN));
239 }
240 }
241
242 debug(UnitTest)
243 {
244 @safe pure nothrow @nogc unittest
245 {
246 real nan4 = NaN(0x789_ABCD_EF12_3456);
247 static if (floatTraits!(real).realFormat == RealFormat.ieeeExtended
248 || floatTraits!(real).realFormat == RealFormat.ieeeQuadruple)
249 {
250 assert(getNaNPayload(nan4) == 0x789_ABCD_EF12_3456);
251 }
252 else
253 {
254 assert(getNaNPayload(nan4) == 0x1_ABCD_EF12_3456);
255 }
256 double nan5 = nan4;
257 assert(getNaNPayload(nan5) == 0x1_ABCD_EF12_3456);
258 float nan6 = nan4;
259 assert(getNaNPayload(nan6) == 0x12_3456);
260 nan4 = NaN(0xFABCD);
261 assert(getNaNPayload(nan4) == 0xFABCD);
262 nan6 = nan4;
263 assert(getNaNPayload(nan6) == 0xFABCD);
264 nan5 = NaN(0x100_0000_0000_3456);
265 assert(getNaNPayload(nan5) == 0x0000_0000_3456);
266 }
267 }
268
269 /**
270 * Calculate the next largest floating point value after x.
271 *
272 * Return the least number greater than x that is representable as a real;
273 * thus, it gives the next point on the IEEE number line.
274 *
275 * $(TABLE_SV
276 * $(SVH x, nextUp(x) )
277 * $(SV -$(INFIN), -real.max )
278 * $(SV $(PLUSMN)0.0, real.min_normal*real.epsilon )
279 * $(SV real.max, $(INFIN) )
280 * $(SV $(INFIN), $(INFIN) )
281 * $(SV $(NAN), $(NAN) )
282 * )
283 */
284 real nextUp(real x) @trusted pure nothrow @nogc
285 {
286 import std.math.traits : floatTraits, RealFormat, MANTISSA_MSB, MANTISSA_LSB;
287
288 alias F = floatTraits!(real);
289 static if (F.realFormat != RealFormat.ieeeDouble)
290 {
291 if (__ctfe)
292 {
293 if (x == -real.infinity)
294 return -real.max;
295 if (!(x < real.infinity)) // Infinity or NaN.
296 return x;
297 real delta;
298 // Start with a decent estimate of delta.
299 if (x <= 0x1.ffffffffffffep+1023 && x >= -double.max)
300 {
301 const double d = cast(double) x;
302 delta = (cast(real) nextUp(d) - cast(real) d) * 0x1p-11L;
303 while (x + (delta * 0x1p-100L) > x)
304 delta *= 0x1p-100L;
305 }
306 else
307 {
308 delta = 0x1p960L;
309 while (!(x + delta > x) && delta < real.max * 0x1p-100L)
310 delta *= 0x1p100L;
311 }
312 if (x + delta > x)
313 {
314 while (x + (delta / 2) > x)
315 delta /= 2;
316 }
317 else
318 {
319 do { delta += delta; } while (!(x + delta > x));
320 }
321 if (x < 0 && x + delta == 0)
322 return -0.0L;
323 return x + delta;
324 }
325 }
326 static if (F.realFormat == RealFormat.ieeeDouble)
327 {
328 return nextUp(cast(double) x);
329 }
330 else static if (F.realFormat == RealFormat.ieeeQuadruple)
331 {
332 ushort e = F.EXPMASK & (cast(ushort *)&x)[F.EXPPOS_SHORT];
333 if (e == F.EXPMASK)
334 {
335 // NaN or Infinity
336 if (x == -real.infinity) return -real.max;
337 return x; // +Inf and NaN are unchanged.
338 }
339
340 auto ps = cast(ulong *)&x;
341 if (ps[MANTISSA_MSB] & 0x8000_0000_0000_0000)
342 {
343 // Negative number
344 if (ps[MANTISSA_LSB] == 0 && ps[MANTISSA_MSB] == 0x8000_0000_0000_0000)
345 {
346 // it was negative zero, change to smallest subnormal
347 ps[MANTISSA_LSB] = 1;
348 ps[MANTISSA_MSB] = 0;
349 return x;
350 }
351 if (ps[MANTISSA_LSB] == 0) --ps[MANTISSA_MSB];
352 --ps[MANTISSA_LSB];
353 }
354 else
355 {
356 // Positive number
357 ++ps[MANTISSA_LSB];
358 if (ps[MANTISSA_LSB] == 0) ++ps[MANTISSA_MSB];
359 }
360 return x;
361 }
362 else static if (F.realFormat == RealFormat.ieeeExtended ||
363 F.realFormat == RealFormat.ieeeExtended53)
364 {
365 // For 80-bit reals, the "implied bit" is a nuisance...
366 ushort *pe = cast(ushort *)&x;
367 ulong *ps = cast(ulong *)&x;
368 // EPSILON is 1 for 64-bit, and 2048 for 53-bit precision reals.
369 enum ulong EPSILON = 2UL ^^ (64 - real.mant_dig);
370
371 if ((pe[F.EXPPOS_SHORT] & F.EXPMASK) == F.EXPMASK)
372 {
373 // First, deal with NANs and infinity
374 if (x == -real.infinity) return -real.max;
375 return x; // +Inf and NaN are unchanged.
376 }
377 if (pe[F.EXPPOS_SHORT] & 0x8000)
378 {
379 // Negative number -- need to decrease the significand
380 *ps -= EPSILON;
381 // Need to mask with 0x7FFF... so subnormals are treated correctly.
382 if ((*ps & 0x7FFF_FFFF_FFFF_FFFF) == 0x7FFF_FFFF_FFFF_FFFF)
383 {
384 if (pe[F.EXPPOS_SHORT] == 0x8000) // it was negative zero
385 {
386 *ps = 1;
387 pe[F.EXPPOS_SHORT] = 0; // smallest subnormal.
388 return x;
389 }
390
391 --pe[F.EXPPOS_SHORT];
392
393 if (pe[F.EXPPOS_SHORT] == 0x8000)
394 return x; // it's become a subnormal, implied bit stays low.
395
396 *ps = 0xFFFF_FFFF_FFFF_FFFF; // set the implied bit
397 return x;
398 }
399 return x;
400 }
401 else
402 {
403 // Positive number -- need to increase the significand.
404 // Works automatically for positive zero.
405 *ps += EPSILON;
406 if ((*ps & 0x7FFF_FFFF_FFFF_FFFF) == 0)
407 {
408 // change in exponent
409 ++pe[F.EXPPOS_SHORT];
410 *ps = 0x8000_0000_0000_0000; // set the high bit
411 }
412 }
413 return x;
414 }
415 else // static if (F.realFormat == RealFormat.ibmExtended)
416 {
417 assert(0, "nextUp not implemented");
418 }
419 }
420
421 /** ditto */
422 double nextUp(double x) @trusted pure nothrow @nogc
423 {
424 ulong s = *cast(ulong *)&x;
425
426 if ((s & 0x7FF0_0000_0000_0000) == 0x7FF0_0000_0000_0000)
427 {
428 // First, deal with NANs and infinity
429 if (x == -x.infinity) return -x.max;
430 return x; // +INF and NAN are unchanged.
431 }
432 if (s & 0x8000_0000_0000_0000) // Negative number
433 {
434 if (s == 0x8000_0000_0000_0000) // it was negative zero
435 {
436 s = 0x0000_0000_0000_0001; // change to smallest subnormal
437 return *cast(double*) &s;
438 }
439 --s;
440 }
441 else
442 { // Positive number
443 ++s;
444 }
445 return *cast(double*) &s;
446 }
447
448 /** ditto */
449 float nextUp(float x) @trusted pure nothrow @nogc
450 {
451 uint s = *cast(uint *)&x;
452
453 if ((s & 0x7F80_0000) == 0x7F80_0000)
454 {
455 // First, deal with NANs and infinity
456 if (x == -x.infinity) return -x.max;
457
458 return x; // +INF and NAN are unchanged.
459 }
460 if (s & 0x8000_0000) // Negative number
461 {
462 if (s == 0x8000_0000) // it was negative zero
463 {
464 s = 0x0000_0001; // change to smallest subnormal
465 return *cast(float*) &s;
466 }
467
468 --s;
469 }
470 else
471 {
472 // Positive number
473 ++s;
474 }
475 return *cast(float*) &s;
476 }
477
478 ///
479 @safe @nogc pure nothrow unittest
480 {
481 assert(nextUp(1.0 - 1.0e-6).feqrel(0.999999) > 16);
482 assert(nextUp(1.0 - real.epsilon).feqrel(1.0) > 16);
483 }
484
485 /**
486 * Calculate the next smallest floating point value before x.
487 *
488 * Return the greatest number less than x that is representable as a real;
489 * thus, it gives the previous point on the IEEE number line.
490 *
491 * $(TABLE_SV
492 * $(SVH x, nextDown(x) )
493 * $(SV $(INFIN), real.max )
494 * $(SV $(PLUSMN)0.0, -real.min_normal*real.epsilon )
495 * $(SV -real.max, -$(INFIN) )
496 * $(SV -$(INFIN), -$(INFIN) )
497 * $(SV $(NAN), $(NAN) )
498 * )
499 */
500 real nextDown(real x) @safe pure nothrow @nogc
501 {
502 return -nextUp(-x);
503 }
504
505 /** ditto */
506 double nextDown(double x) @safe pure nothrow @nogc
507 {
508 return -nextUp(-x);
509 }
510
511 /** ditto */
512 float nextDown(float x) @safe pure nothrow @nogc
513 {
514 return -nextUp(-x);
515 }
516
517 ///
518 @safe pure nothrow @nogc unittest
519 {
520 assert( nextDown(1.0 + real.epsilon) == 1.0);
521 }
522
523 @safe pure nothrow @nogc unittest
524 {
525 import std.math.traits : floatTraits, RealFormat, isIdentical;
526
527 static if (floatTraits!(real).realFormat == RealFormat.ieeeExtended ||
528 floatTraits!(real).realFormat == RealFormat.ieeeDouble ||
529 floatTraits!(real).realFormat == RealFormat.ieeeExtended53 ||
530 floatTraits!(real).realFormat == RealFormat.ieeeQuadruple)
531 {
532 // Tests for reals
533 assert(isIdentical(nextUp(NaN(0xABC)), NaN(0xABC)));
534 //static assert(isIdentical(nextUp(NaN(0xABC)), NaN(0xABC)));
535 // negative numbers
536 assert( nextUp(-real.infinity) == -real.max );
537 assert( nextUp(-1.0L-real.epsilon) == -1.0 );
538 assert( nextUp(-2.0L) == -2.0 + real.epsilon);
539 static assert( nextUp(-real.infinity) == -real.max );
540 static assert( nextUp(-1.0L-real.epsilon) == -1.0 );
541 static assert( nextUp(-2.0L) == -2.0 + real.epsilon);
542 // subnormals and zero
543 assert( nextUp(-real.min_normal) == -real.min_normal*(1-real.epsilon) );
544 assert( nextUp(-real.min_normal*(1-real.epsilon)) == -real.min_normal*(1-2*real.epsilon) );
545 assert( isIdentical(-0.0L, nextUp(-real.min_normal*real.epsilon)) );
546 assert( nextUp(-0.0L) == real.min_normal*real.epsilon );
547 assert( nextUp(0.0L) == real.min_normal*real.epsilon );
548 assert( nextUp(real.min_normal*(1-real.epsilon)) == real.min_normal );
549 assert( nextUp(real.min_normal) == real.min_normal*(1+real.epsilon) );
550 static assert( nextUp(-real.min_normal) == -real.min_normal*(1-real.epsilon) );
551 static assert( nextUp(-real.min_normal*(1-real.epsilon)) == -real.min_normal*(1-2*real.epsilon) );
552 static assert( -0.0L is nextUp(-real.min_normal*real.epsilon) );
553 static assert( nextUp(-0.0L) == real.min_normal*real.epsilon );
554 static assert( nextUp(0.0L) == real.min_normal*real.epsilon );
555 static assert( nextUp(real.min_normal*(1-real.epsilon)) == real.min_normal );
556 static assert( nextUp(real.min_normal) == real.min_normal*(1+real.epsilon) );
557 // positive numbers
558 assert( nextUp(1.0L) == 1.0 + real.epsilon );
559 assert( nextUp(2.0L-real.epsilon) == 2.0 );
560 assert( nextUp(real.max) == real.infinity );
561 assert( nextUp(real.infinity)==real.infinity );
562 static assert( nextUp(1.0L) == 1.0 + real.epsilon );
563 static assert( nextUp(2.0L-real.epsilon) == 2.0 );
564 static assert( nextUp(real.max) == real.infinity );
565 static assert( nextUp(real.infinity)==real.infinity );
566 // ctfe near double.max boundary
567 static assert(nextUp(nextDown(cast(real) double.max)) == cast(real) double.max);
568 }
569
570 double n = NaN(0xABC);
571 assert(isIdentical(nextUp(n), n));
572 // negative numbers
573 assert( nextUp(-double.infinity) == -double.max );
574 assert( nextUp(-1-double.epsilon) == -1.0 );
575 assert( nextUp(-2.0) == -2.0 + double.epsilon);
576 // subnormals and zero
577
578 assert( nextUp(-double.min_normal) == -double.min_normal*(1-double.epsilon) );
579 assert( nextUp(-double.min_normal*(1-double.epsilon)) == -double.min_normal*(1-2*double.epsilon) );
580 assert( isIdentical(-0.0, nextUp(-double.min_normal*double.epsilon)) );
581 assert( nextUp(0.0) == double.min_normal*double.epsilon );
582 assert( nextUp(-0.0) == double.min_normal*double.epsilon );
583 assert( nextUp(double.min_normal*(1-double.epsilon)) == double.min_normal );
584 assert( nextUp(double.min_normal) == double.min_normal*(1+double.epsilon) );
585 // positive numbers
586 assert( nextUp(1.0) == 1.0 + double.epsilon );
587 assert( nextUp(2.0-double.epsilon) == 2.0 );
588 assert( nextUp(double.max) == double.infinity );
589
590 float fn = NaN(0xABC);
591 assert(isIdentical(nextUp(fn), fn));
592 float f = -float.min_normal*(1-float.epsilon);
593 float f1 = -float.min_normal;
594 assert( nextUp(f1) == f);
595 f = 1.0f+float.epsilon;
596 f1 = 1.0f;
597 assert( nextUp(f1) == f );
598 f1 = -0.0f;
599 assert( nextUp(f1) == float.min_normal*float.epsilon);
600 assert( nextUp(float.infinity)==float.infinity );
601
602 assert(nextDown(1.0L+real.epsilon)==1.0);
603 assert(nextDown(1.0+double.epsilon)==1.0);
604 f = 1.0f+float.epsilon;
605 assert(nextDown(f)==1.0);
606 assert(nextafter(1.0+real.epsilon, -real.infinity)==1.0);
607
608 // CTFE
609
610 enum double ctfe_n = NaN(0xABC);
611 //static assert(isIdentical(nextUp(ctfe_n), ctfe_n)); // FIXME: https://issues.dlang.org/show_bug.cgi?id=20197
612 static assert(nextUp(double.nan) is double.nan);
613 // negative numbers
614 static assert( nextUp(-double.infinity) == -double.max );
615 static assert( nextUp(-1-double.epsilon) == -1.0 );
616 static assert( nextUp(-2.0) == -2.0 + double.epsilon);
617 // subnormals and zero
618
619 static assert( nextUp(-double.min_normal) == -double.min_normal*(1-double.epsilon) );
620 static assert( nextUp(-double.min_normal*(1-double.epsilon)) == -double.min_normal*(1-2*double.epsilon) );
621 static assert( -0.0 is nextUp(-double.min_normal*double.epsilon) );
622 static assert( nextUp(0.0) == double.min_normal*double.epsilon );
623 static assert( nextUp(-0.0) == double.min_normal*double.epsilon );
624 static assert( nextUp(double.min_normal*(1-double.epsilon)) == double.min_normal );
625 static assert( nextUp(double.min_normal) == double.min_normal*(1+double.epsilon) );
626 // positive numbers
627 static assert( nextUp(1.0) == 1.0 + double.epsilon );
628 static assert( nextUp(2.0-double.epsilon) == 2.0 );
629 static assert( nextUp(double.max) == double.infinity );
630
631 enum float ctfe_fn = NaN(0xABC);
632 //static assert(isIdentical(nextUp(ctfe_fn), ctfe_fn)); // FIXME: https://issues.dlang.org/show_bug.cgi?id=20197
633 static assert(nextUp(float.nan) is float.nan);
634 static assert(nextUp(-float.min_normal) == -float.min_normal*(1-float.epsilon));
635 static assert(nextUp(1.0f) == 1.0f+float.epsilon);
636 static assert(nextUp(-0.0f) == float.min_normal*float.epsilon);
637 static assert(nextUp(float.infinity)==float.infinity);
638 static assert(nextDown(1.0L+real.epsilon)==1.0);
639 static assert(nextDown(1.0+double.epsilon)==1.0);
640 static assert(nextDown(1.0f+float.epsilon)==1.0);
641 static assert(nextafter(1.0+real.epsilon, -real.infinity)==1.0);
642 }
643
644
645
646 /******************************************
647 * Calculates the next representable value after x in the direction of y.
648 *
649 * If y > x, the result will be the next largest floating-point value;
650 * if y < x, the result will be the next smallest value.
651 * If x == y, the result is y.
652 * If x or y is a NaN, the result is a NaN.
653 *
654 * Remarks:
655 * This function is not generally very useful; it's almost always better to use
656 * the faster functions nextUp() or nextDown() instead.
657 *
658 * The FE_INEXACT and FE_OVERFLOW exceptions will be raised if x is finite and
659 * the function result is infinite. The FE_INEXACT and FE_UNDERFLOW
660 * exceptions will be raised if the function value is subnormal, and x is
661 * not equal to y.
662 */
663 T nextafter(T)(const T x, const T y) @safe pure nothrow @nogc
664 {
665 import std.math.traits : isNaN;
666
667 if (x == y || isNaN(y))
668 {
669 return y;
670 }
671
672 if (isNaN(x))
673 {
674 return x;
675 }
676
677 return ((y>x) ? nextUp(x) : nextDown(x));
678 }
679
680 ///
681 @safe pure nothrow @nogc unittest
682 {
683 import std.math.traits : isNaN;
684
685 float a = 1;
686 assert(is(typeof(nextafter(a, a)) == float));
687 assert(nextafter(a, a.infinity) > a);
688 assert(isNaN(nextafter(a, a.nan)));
689 assert(isNaN(nextafter(a.nan, a)));
690
691 double b = 2;
692 assert(is(typeof(nextafter(b, b)) == double));
693 assert(nextafter(b, b.infinity) > b);
694 assert(isNaN(nextafter(b, b.nan)));
695 assert(isNaN(nextafter(b.nan, b)));
696
697 real c = 3;
698 assert(is(typeof(nextafter(c, c)) == real));
699 assert(nextafter(c, c.infinity) > c);
700 assert(isNaN(nextafter(c, c.nan)));
701 assert(isNaN(nextafter(c.nan, c)));
702 }
703
704 @safe pure nothrow @nogc unittest
705 {
706 import std.math.traits : isNaN, signbit;
707
708 // CTFE
709 enum float a = 1;
710 static assert(is(typeof(nextafter(a, a)) == float));
711 static assert(nextafter(a, a.infinity) > a);
712 static assert(isNaN(nextafter(a, a.nan)));
713 static assert(isNaN(nextafter(a.nan, a)));
714
715 enum double b = 2;
716 static assert(is(typeof(nextafter(b, b)) == double));
717 static assert(nextafter(b, b.infinity) > b);
718 static assert(isNaN(nextafter(b, b.nan)));
719 static assert(isNaN(nextafter(b.nan, b)));
720
721 enum real c = 3;
722 static assert(is(typeof(nextafter(c, c)) == real));
723 static assert(nextafter(c, c.infinity) > c);
724 static assert(isNaN(nextafter(c, c.nan)));
725 static assert(isNaN(nextafter(c.nan, c)));
726
727 enum real negZero = nextafter(+0.0L, -0.0L);
728 static assert(negZero == -0.0L);
729 static assert(signbit(negZero));
730
731 static assert(nextafter(c, c) == c);
732 }
733
734 //real nexttoward(real x, real y) { return core.stdc.math.nexttowardl(x, y); }
735
736 /**
737 * Returns the positive difference between x and y.
738 *
739 * Equivalent to `fmax(x-y, 0)`.
740 *
741 * Returns:
742 * $(TABLE_SV
743 * $(TR $(TH x, y) $(TH fdim(x, y)))
744 * $(TR $(TD x $(GT) y) $(TD x - y))
745 * $(TR $(TD x $(LT)= y) $(TD +0.0))
746 * )
747 */
748 real fdim(real x, real y) @safe pure nothrow @nogc
749 {
750 return (x < y) ? +0.0 : x - y;
751 }
752
753 ///
754 @safe pure nothrow @nogc unittest
755 {
756 import std.math.traits : isNaN;
757
758 assert(fdim(2.0, 0.0) == 2.0);
759 assert(fdim(-2.0, 0.0) == 0.0);
760 assert(fdim(real.infinity, 2.0) == real.infinity);
761 assert(isNaN(fdim(real.nan, 2.0)));
762 assert(isNaN(fdim(2.0, real.nan)));
763 assert(isNaN(fdim(real.nan, real.nan)));
764 }
765
766 /**
767 * Returns the larger of `x` and `y`.
768 *
769 * If one of the arguments is a `NaN`, the other is returned.
770 *
771 * See_Also: $(REF max, std,algorithm,comparison) is faster because it does not perform the `isNaN` test.
772 */
773 F fmax(F)(const F x, const F y) @safe pure nothrow @nogc
774 if (__traits(isFloating, F))
775 {
776 import std.math.traits : isNaN;
777
778 // Do the more predictable test first. Generates 0 branches with ldc and 1 branch with gdc.
779 // See https://godbolt.org/z/erxrW9
780 if (isNaN(x)) return y;
781 return y > x ? y : x;
782 }
783
784 ///
785 @safe pure nothrow @nogc unittest
786 {
787 import std.meta : AliasSeq;
788 static foreach (F; AliasSeq!(float, double, real))
789 {
790 assert(fmax(F(0.0), F(2.0)) == 2.0);
791 assert(fmax(F(-2.0), 0.0) == F(0.0));
792 assert(fmax(F.infinity, F(2.0)) == F.infinity);
793 assert(fmax(F.nan, F(2.0)) == F(2.0));
794 assert(fmax(F(2.0), F.nan) == F(2.0));
795 }
796 }
797
798 /**
799 * Returns the smaller of `x` and `y`.
800 *
801 * If one of the arguments is a `NaN`, the other is returned.
802 *
803 * See_Also: $(REF min, std,algorithm,comparison) is faster because it does not perform the `isNaN` test.
804 */
805 F fmin(F)(const F x, const F y) @safe pure nothrow @nogc
806 if (__traits(isFloating, F))
807 {
808 import std.math.traits : isNaN;
809
810 // Do the more predictable test first. Generates 0 branches with ldc and 1 branch with gdc.
811 // See https://godbolt.org/z/erxrW9
812 if (isNaN(x)) return y;
813 return y < x ? y : x;
814 }
815
816 ///
817 @safe pure nothrow @nogc unittest
818 {
819 import std.meta : AliasSeq;
820 static foreach (F; AliasSeq!(float, double, real))
821 {
822 assert(fmin(F(0.0), F(2.0)) == 0.0);
823 assert(fmin(F(-2.0), F(0.0)) == -2.0);
824 assert(fmin(F.infinity, F(2.0)) == 2.0);
825 assert(fmin(F.nan, F(2.0)) == 2.0);
826 assert(fmin(F(2.0), F.nan) == 2.0);
827 }
828 }
829
830 /**************************************
831 * Returns (x * y) + z, rounding only once according to the
832 * current rounding mode.
833 *
834 * BUGS: Not currently implemented - rounds twice.
835 */
836 pragma(inline, true)
837 real fma(real x, real y, real z) @safe pure nothrow @nogc { return (x * y) + z; }
838
839 ///
840 @safe pure nothrow @nogc unittest
841 {
842 assert(fma(0.0, 2.0, 2.0) == 2.0);
843 assert(fma(2.0, 2.0, 2.0) == 6.0);
844 assert(fma(real.infinity, 2.0, 2.0) == real.infinity);
845 assert(fma(real.nan, 2.0, 2.0) is real.nan);
846 assert(fma(2.0, 2.0, real.nan) is real.nan);
847 }
848
849 /**************************************
850 * To what precision is x equal to y?
851 *
852 * Returns: the number of mantissa bits which are equal in x and y.
853 * eg, 0x1.F8p+60 and 0x1.F1p+60 are equal to 5 bits of precision.
854 *
855 * $(TABLE_SV
856 * $(TR $(TH x) $(TH y) $(TH feqrel(x, y)))
857 * $(TR $(TD x) $(TD x) $(TD real.mant_dig))
858 * $(TR $(TD x) $(TD $(GT)= 2*x) $(TD 0))
859 * $(TR $(TD x) $(TD $(LT)= x/2) $(TD 0))
860 * $(TR $(TD $(NAN)) $(TD any) $(TD 0))
861 * $(TR $(TD any) $(TD $(NAN)) $(TD 0))
862 * )
863 */
864 int feqrel(X)(const X x, const X y) @trusted pure nothrow @nogc
865 if (isFloatingPoint!(X))
866 {
867 import std.math.traits : floatTraits, RealFormat;
868 import core.math : fabs;
869
870 /* Public Domain. Author: Don Clugston, 18 Aug 2005.
871 */
872 alias F = floatTraits!(X);
873 static if (F.realFormat == RealFormat.ieeeSingle
874 || F.realFormat == RealFormat.ieeeDouble
875 || F.realFormat == RealFormat.ieeeExtended
876 || F.realFormat == RealFormat.ieeeExtended53
877 || F.realFormat == RealFormat.ieeeQuadruple)
878 {
879 if (x == y)
880 return X.mant_dig; // ensure diff != 0, cope with INF.
881
882 Unqual!X diff = fabs(x - y);
883
884 ushort *pa = cast(ushort *)(&x);
885 ushort *pb = cast(ushort *)(&y);
886 ushort *pd = cast(ushort *)(&diff);
887
888
889 // The difference in abs(exponent) between x or y and abs(x-y)
890 // is equal to the number of significand bits of x which are
891 // equal to y. If negative, x and y have different exponents.
892 // If positive, x and y are equal to 'bitsdiff' bits.
893 // AND with 0x7FFF to form the absolute value.
894 // To avoid out-by-1 errors, we subtract 1 so it rounds down
895 // if the exponents were different. This means 'bitsdiff' is
896 // always 1 lower than we want, except that if bitsdiff == 0,
897 // they could have 0 or 1 bits in common.
898
899 int bitsdiff = ((( (pa[F.EXPPOS_SHORT] & F.EXPMASK)
900 + (pb[F.EXPPOS_SHORT] & F.EXPMASK)
901 - (1 << F.EXPSHIFT)) >> 1)
902 - (pd[F.EXPPOS_SHORT] & F.EXPMASK)) >> F.EXPSHIFT;
903 if ( (pd[F.EXPPOS_SHORT] & F.EXPMASK) == 0)
904 { // Difference is subnormal
905 // For subnormals, we need to add the number of zeros that
906 // lie at the start of diff's significand.
907 // We do this by multiplying by 2^^real.mant_dig
908 diff *= F.RECIP_EPSILON;
909 return bitsdiff + X.mant_dig - ((pd[F.EXPPOS_SHORT] & F.EXPMASK) >> F.EXPSHIFT);
910 }
911
912 if (bitsdiff > 0)
913 return bitsdiff + 1; // add the 1 we subtracted before
914
915 // Avoid out-by-1 errors when factor is almost 2.
916 if (bitsdiff == 0
917 && ((pa[F.EXPPOS_SHORT] ^ pb[F.EXPPOS_SHORT]) & F.EXPMASK) == 0)
918 {
919 return 1;
920 } else return 0;
921 }
922 else
923 {
924 static assert(false, "Not implemented for this architecture");
925 }
926 }
927
928 ///
929 @safe pure unittest
930 {
931 assert(feqrel(2.0, 2.0) == 53);
932 assert(feqrel(2.0f, 2.0f) == 24);
933 assert(feqrel(2.0, double.nan) == 0);
934
935 // Test that numbers are within n digits of each
936 // other by testing if feqrel > n * log2(10)
937
938 // five digits
939 assert(feqrel(2.0, 2.00001) > 16);
940 // ten digits
941 assert(feqrel(2.0, 2.00000000001) > 33);
942 }
943
944 @safe pure nothrow @nogc unittest
945 {
946 void testFeqrel(F)()
947 {
948 // Exact equality
949 assert(feqrel(F.max, F.max) == F.mant_dig);
950 assert(feqrel!(F)(0.0, 0.0) == F.mant_dig);
951 assert(feqrel(F.infinity, F.infinity) == F.mant_dig);
952
953 // a few bits away from exact equality
954 F w=1;
955 for (int i = 1; i < F.mant_dig - 1; ++i)
956 {
957 assert(feqrel!(F)(1.0 + w * F.epsilon, 1.0) == F.mant_dig-i);
958 assert(feqrel!(F)(1.0 - w * F.epsilon, 1.0) == F.mant_dig-i);
959 assert(feqrel!(F)(1.0, 1 + (w-1) * F.epsilon) == F.mant_dig - i + 1);
960 w*=2;
961 }
962
963 assert(feqrel!(F)(1.5+F.epsilon, 1.5) == F.mant_dig-1);
964 assert(feqrel!(F)(1.5-F.epsilon, 1.5) == F.mant_dig-1);
965 assert(feqrel!(F)(1.5-F.epsilon, 1.5+F.epsilon) == F.mant_dig-2);
966
967
968 // Numbers that are close
969 assert(feqrel!(F)(0x1.Bp+84, 0x1.B8p+84) == 5);
970 assert(feqrel!(F)(0x1.8p+10, 0x1.Cp+10) == 2);
971 assert(feqrel!(F)(1.5 * (1 - F.epsilon), 1.0L) == 2);
972 assert(feqrel!(F)(1.5, 1.0) == 1);
973 assert(feqrel!(F)(2 * (1 - F.epsilon), 1.0L) == 1);
974
975 // Factors of 2
976 assert(feqrel(F.max, F.infinity) == 0);
977 assert(feqrel!(F)(2 * (1 - F.epsilon), 1.0L) == 1);
978 assert(feqrel!(F)(1.0, 2.0) == 0);
979 assert(feqrel!(F)(4.0, 1.0) == 0);
980
981 // Extreme inequality
982 assert(feqrel(F.nan, F.nan) == 0);
983 assert(feqrel!(F)(0.0L, -F.nan) == 0);
984 assert(feqrel(F.nan, F.infinity) == 0);
985 assert(feqrel(F.infinity, -F.infinity) == 0);
986 assert(feqrel(F.max, -F.max) == 0);
987
988 assert(feqrel(F.min_normal / 8, F.min_normal / 17) == 3);
989
990 const F Const = 2;
991 immutable F Immutable = 2;
992 auto Compiles = feqrel(Const, Immutable);
993 }
994
995 assert(feqrel(7.1824L, 7.1824L) == real.mant_dig);
996
997 testFeqrel!(real)();
998 testFeqrel!(double)();
999 testFeqrel!(float)();
1000 }
1001
1002 /**
1003 Computes whether a values is approximately equal to a reference value,
1004 admitting a maximum relative difference, and a maximum absolute difference.
1005
1006 Warning:
1007 This template is considered out-dated. It will be removed from
1008 Phobos in 2.106.0. Please use $(LREF isClose) instead. To achieve
1009 a similar behaviour to `approxEqual(a, b)` use
1010 `isClose(a, b, 1e-2, 1e-5)`. In case of comparing to 0.0,
1011 `isClose(a, b, 0.0, eps)` should be used, where `eps`
1012 represents the accepted deviation from 0.0."
1013
1014 Params:
1015 value = Value to compare.
1016 reference = Reference value.
1017 maxRelDiff = Maximum allowable difference relative to `reference`.
1018 Setting to 0.0 disables this check. Defaults to `1e-2`.
1019 maxAbsDiff = Maximum absolute difference. This is mainly usefull
1020 for comparing values to zero. Setting to 0.0 disables this check.
1021 Defaults to `1e-5`.
1022
1023 Returns:
1024 `true` if `value` is approximately equal to `reference` under
1025 either criterium. It is sufficient, when `value ` satisfies
1026 one of the two criteria.
1027
1028 If one item is a range, and the other is a single value, then
1029 the result is the logical and-ing of calling `approxEqual` on
1030 each element of the ranged item against the single item. If
1031 both items are ranges, then `approxEqual` returns `true` if
1032 and only if the ranges have the same number of elements and if
1033 `approxEqual` evaluates to `true` for each pair of elements.
1034
1035 See_Also:
1036 Use $(LREF feqrel) to get the number of equal bits in the mantissa.
1037 */
1038 deprecated("approxEqual will be removed in 2.106.0. Please use isClose instead.")
1039 bool approxEqual(T, U, V)(T value, U reference, V maxRelDiff = 1e-2, V maxAbsDiff = 1e-5)
1040 {
1041 import core.math : fabs;
1042 import std.range.primitives : empty, front, isInputRange, popFront;
1043 static if (isInputRange!T)
1044 {
1045 static if (isInputRange!U)
1046 {
1047 // Two ranges
1048 for (;; value.popFront(), reference.popFront())
1049 {
1050 if (value.empty) return reference.empty;
1051 if (reference.empty) return value.empty;
1052 if (!approxEqual(value.front, reference.front, maxRelDiff, maxAbsDiff))
1053 return false;
1054 }
1055 }
1056 else static if (isIntegral!U)
1057 {
1058 // convert reference to real
1059 return approxEqual(value, real(reference), maxRelDiff, maxAbsDiff);
1060 }
1061 else
1062 {
1063 // value is range, reference is number
1064 for (; !value.empty; value.popFront())
1065 {
1066 if (!approxEqual(value.front, reference, maxRelDiff, maxAbsDiff))
1067 return false;
1068 }
1069 return true;
1070 }
1071 }
1072 else
1073 {
1074 static if (isInputRange!U)
1075 {
1076 // value is number, reference is range
1077 for (; !reference.empty; reference.popFront())
1078 {
1079 if (!approxEqual(value, reference.front, maxRelDiff, maxAbsDiff))
1080 return false;
1081 }
1082 return true;
1083 }
1084 else static if (isIntegral!T || isIntegral!U)
1085 {
1086 // convert both value and reference to real
1087 return approxEqual(real(value), real(reference), maxRelDiff, maxAbsDiff);
1088 }
1089 else
1090 {
1091 // two numbers
1092 //static assert(is(T : real) && is(U : real));
1093 if (reference == 0)
1094 {
1095 return fabs(value) <= maxAbsDiff;
1096 }
1097 static if (is(typeof(value.infinity)) && is(typeof(reference.infinity)))
1098 {
1099 if (value == value.infinity && reference == reference.infinity ||
1100 value == -value.infinity && reference == -reference.infinity) return true;
1101 }
1102 return fabs((value - reference) / reference) <= maxRelDiff
1103 || maxAbsDiff != 0 && fabs(value - reference) <= maxAbsDiff;
1104 }
1105 }
1106 }
1107
1108 deprecated @safe pure nothrow unittest
1109 {
1110 assert(approxEqual(1.0, 1.0099));
1111 assert(!approxEqual(1.0, 1.011));
1112 assert(approxEqual(0.00001, 0.0));
1113 assert(!approxEqual(0.00002, 0.0));
1114
1115 assert(approxEqual(3.0, [3, 3.01, 2.99])); // several reference values is strange
1116 assert(approxEqual([3, 3.01, 2.99], 3.0)); // better
1117
1118 float[] arr1 = [ 1.0, 2.0, 3.0 ];
1119 double[] arr2 = [ 1.001, 1.999, 3 ];
1120 assert(approxEqual(arr1, arr2));
1121 }
1122
1123 deprecated @safe pure nothrow unittest
1124 {
1125 // relative comparison depends on reference, make sure proper
1126 // side is used when comparing range to single value. Based on
1127 // https://issues.dlang.org/show_bug.cgi?id=15763
1128 auto a = [2e-3 - 1e-5];
1129 auto b = 2e-3 + 1e-5;
1130 assert(a[0].approxEqual(b));
1131 assert(!b.approxEqual(a[0]));
1132 assert(a.approxEqual(b));
1133 assert(!b.approxEqual(a));
1134 }
1135
1136 deprecated @safe pure nothrow @nogc unittest
1137 {
1138 assert(!approxEqual(0.0,1e-15,1e-9,0.0));
1139 assert(approxEqual(0.0,1e-15,1e-9,1e-9));
1140 assert(!approxEqual(1.0,3.0,0.0,1.0));
1141
1142 assert(approxEqual(1.00000000099,1.0,1e-9,0.0));
1143 assert(!approxEqual(1.0000000011,1.0,1e-9,0.0));
1144 }
1145
1146 deprecated @safe pure nothrow @nogc unittest
1147 {
1148 // maybe unintuitive behavior
1149 assert(approxEqual(1000.0,1010.0));
1150 assert(approxEqual(9_090_000_000.0,9_000_000_000.0));
1151 assert(approxEqual(0.0,1e30,1.0));
1152 assert(approxEqual(0.00001,1e-30));
1153 assert(!approxEqual(-1e-30,1e-30,1e-2,0.0));
1154 }
1155
1156 deprecated @safe pure nothrow @nogc unittest
1157 {
1158 int a = 10;
1159 assert(approxEqual(10, a));
1160
1161 assert(!approxEqual(3, 0));
1162 assert(approxEqual(3, 3));
1163 assert(approxEqual(3.0, 3));
1164 assert(approxEqual(3, 3.0));
1165
1166 assert(approxEqual(0.0,0.0));
1167 assert(approxEqual(-0.0,0.0));
1168 assert(approxEqual(0.0f,0.0));
1169 }
1170
1171 deprecated @safe pure nothrow @nogc unittest
1172 {
1173 real num = real.infinity;
1174 assert(num == real.infinity);
1175 assert(approxEqual(num, real.infinity));
1176 num = -real.infinity;
1177 assert(num == -real.infinity);
1178 assert(approxEqual(num, -real.infinity));
1179
1180 assert(!approxEqual(1,real.nan));
1181 assert(!approxEqual(real.nan,real.max));
1182 assert(!approxEqual(real.nan,real.nan));
1183 }
1184
1185 deprecated @safe pure nothrow unittest
1186 {
1187 assert(!approxEqual([1.0,2.0,3.0],[1.0,2.0]));
1188 assert(!approxEqual([1.0,2.0],[1.0,2.0,3.0]));
1189
1190 assert(approxEqual!(real[],real[])([],[]));
1191 assert(approxEqual(cast(real[])[],cast(real[])[]));
1192 }
1193
1194
1195 /**
1196 Computes whether two values are approximately equal, admitting a maximum
1197 relative difference, and a maximum absolute difference.
1198
1199 Params:
1200 lhs = First item to compare.
1201 rhs = Second item to compare.
1202 maxRelDiff = Maximum allowable relative difference.
1203 Setting to 0.0 disables this check. Default depends on the type of
1204 `lhs` and `rhs`: It is approximately half the number of decimal digits of
1205 precision of the smaller type.
1206 maxAbsDiff = Maximum absolute difference. This is mainly usefull
1207 for comparing values to zero. Setting to 0.0 disables this check.
1208 Defaults to `0.0`.
1209
1210 Returns:
1211 `true` if the two items are approximately equal under either criterium.
1212 It is sufficient, when `value ` satisfies one of the two criteria.
1213
1214 If one item is a range, and the other is a single value, then
1215 the result is the logical and-ing of calling `isClose` on
1216 each element of the ranged item against the single item. If
1217 both items are ranges, then `isClose` returns `true` if
1218 and only if the ranges have the same number of elements and if
1219 `isClose` evaluates to `true` for each pair of elements.
1220
1221 See_Also:
1222 Use $(LREF feqrel) to get the number of equal bits in the mantissa.
1223 */
1224 bool isClose(T, U, V = CommonType!(FloatingPointBaseType!T,FloatingPointBaseType!U))
1225 (T lhs, U rhs, V maxRelDiff = CommonDefaultFor!(T,U), V maxAbsDiff = 0.0)
1226 {
1227 import std.range.primitives : empty, front, isInputRange, popFront;
1228 import std.complex : Complex;
1229 static if (isInputRange!T)
1230 {
1231 static if (isInputRange!U)
1232 {
1233 // Two ranges
1234 for (;; lhs.popFront(), rhs.popFront())
1235 {
1236 if (lhs.empty) return rhs.empty;
1237 if (rhs.empty) return lhs.empty;
1238 if (!isClose(lhs.front, rhs.front, maxRelDiff, maxAbsDiff))
1239 return false;
1240 }
1241 }
1242 else
1243 {
1244 // lhs is range, rhs is number
1245 for (; !lhs.empty; lhs.popFront())
1246 {
1247 if (!isClose(lhs.front, rhs, maxRelDiff, maxAbsDiff))
1248 return false;
1249 }
1250 return true;
1251 }
1252 }
1253 else static if (isInputRange!U)
1254 {
1255 // lhs is number, rhs is range
1256 for (; !rhs.empty; rhs.popFront())
1257 {
1258 if (!isClose(lhs, rhs.front, maxRelDiff, maxAbsDiff))
1259 return false;
1260 }
1261 return true;
1262 }
1263 else static if (is(T TE == Complex!TE))
1264 {
1265 static if (is(U UE == Complex!UE))
1266 {
1267 // Two complex numbers
1268 return isClose(lhs.re, rhs.re, maxRelDiff, maxAbsDiff)
1269 && isClose(lhs.im, rhs.im, maxRelDiff, maxAbsDiff);
1270 }
1271 else
1272 {
1273 // lhs is complex, rhs is number
1274 return isClose(lhs.re, rhs, maxRelDiff, maxAbsDiff)
1275 && isClose(lhs.im, 0.0, maxRelDiff, maxAbsDiff);
1276 }
1277 }
1278 else static if (is(U UE == Complex!UE))
1279 {
1280 // lhs is number, rhs is complex
1281 return isClose(lhs, rhs.re, maxRelDiff, maxAbsDiff)
1282 && isClose(0.0, rhs.im, maxRelDiff, maxAbsDiff);
1283 }
1284 else
1285 {
1286 // two numbers
1287 if (lhs == rhs) return true;
1288
1289 static if (is(typeof(lhs.infinity)))
1290 if (lhs == lhs.infinity || lhs == -lhs.infinity)
1291 return false;
1292 static if (is(typeof(rhs.infinity)))
1293 if (rhs == rhs.infinity || rhs == -rhs.infinity)
1294 return false;
1295
1296 import std.math.algebraic : abs;
1297
1298 auto diff = abs(lhs - rhs);
1299
1300 return diff <= maxRelDiff*abs(lhs)
1301 || diff <= maxRelDiff*abs(rhs)
1302 || diff <= maxAbsDiff;
1303 }
1304 }
1305
1306 ///
1307 @safe pure nothrow @nogc unittest
1308 {
1309 assert(isClose(1.0,0.999_999_999));
1310 assert(isClose(0.001, 0.000_999_999_999));
1311 assert(isClose(1_000_000_000.0,999_999_999.0));
1312
1313 assert(isClose(17.123_456_789, 17.123_456_78));
1314 assert(!isClose(17.123_456_789, 17.123_45));
1315
1316 // use explicit 3rd parameter for less (or more) accuracy
1317 assert(isClose(17.123_456_789, 17.123_45, 1e-6));
1318 assert(!isClose(17.123_456_789, 17.123_45, 1e-7));
1319
1320 // use 4th parameter when comparing close to zero
1321 assert(!isClose(1e-100, 0.0));
1322 assert(isClose(1e-100, 0.0, 0.0, 1e-90));
1323 assert(!isClose(1e-10, -1e-10));
1324 assert(isClose(1e-10, -1e-10, 0.0, 1e-9));
1325 assert(!isClose(1e-300, 1e-298));
1326 assert(isClose(1e-300, 1e-298, 0.0, 1e-200));
1327
1328 // different default limits for different floating point types
1329 assert(isClose(1.0f, 0.999_99f));
1330 assert(!isClose(1.0, 0.999_99));
1331 static if (real.sizeof > double.sizeof)
1332 assert(!isClose(1.0L, 0.999_999_999L));
1333 }
1334
1335 ///
1336 @safe pure nothrow unittest
1337 {
1338 assert(isClose([1.0, 2.0, 3.0], [0.999_999_999, 2.000_000_001, 3.0]));
1339 assert(!isClose([1.0, 2.0], [0.999_999_999, 2.000_000_001, 3.0]));
1340 assert(!isClose([1.0, 2.0, 3.0], [0.999_999_999, 2.000_000_001]));
1341
1342 assert(isClose([2.0, 1.999_999_999, 2.000_000_001], 2.0));
1343 assert(isClose(2.0, [2.0, 1.999_999_999, 2.000_000_001]));
1344 }
1345
1346 @safe pure nothrow unittest
1347 {
1348 assert(!isClose([1.0, 2.0, 3.0], [0.999_999_999, 3.0, 3.0]));
1349 assert(!isClose([2.0, 1.999_999, 2.000_000_001], 2.0));
1350 assert(!isClose(2.0, [2.0, 1.999_999_999, 2.000_000_999]));
1351 }
1352
1353 @safe pure nothrow @nogc unittest
1354 {
1355 immutable a = 1.00001f;
1356 const b = 1.000019;
1357 assert(isClose(a,b));
1358
1359 assert(isClose(1.00001f,1.000019f));
1360 assert(isClose(1.00001f,1.000019));
1361 assert(isClose(1.00001,1.000019f));
1362 assert(!isClose(1.00001,1.000019));
1363
1364 real a1 = 1e-300L;
1365 real a2 = a1.nextUp;
1366 assert(isClose(a1,a2));
1367 }
1368
1369 @safe pure nothrow unittest
1370 {
1371 float[] arr1 = [ 1.0, 2.0, 3.0 ];
1372 double[] arr2 = [ 1.00001, 1.99999, 3 ];
1373 assert(isClose(arr1, arr2));
1374 }
1375
1376 @safe pure nothrow @nogc unittest
1377 {
1378 assert(!isClose(1000.0,1010.0));
1379 assert(!isClose(9_090_000_000.0,9_000_000_000.0));
1380 assert(isClose(0.0,1e30,1.0));
1381 assert(!isClose(0.00001,1e-30));
1382 assert(!isClose(-1e-30,1e-30,1e-2,0.0));
1383 }
1384
1385 @safe pure nothrow @nogc unittest
1386 {
1387 assert(!isClose(3, 0));
1388 assert(isClose(3, 3));
1389 assert(isClose(3.0, 3));
1390 assert(isClose(3, 3.0));
1391
1392 assert(isClose(0.0,0.0));
1393 assert(isClose(-0.0,0.0));
1394 assert(isClose(0.0f,0.0));
1395 }
1396
1397 @safe pure nothrow @nogc unittest
1398 {
1399 real num = real.infinity;
1400 assert(num == real.infinity);
1401 assert(isClose(num, real.infinity));
1402 num = -real.infinity;
1403 assert(num == -real.infinity);
1404 assert(isClose(num, -real.infinity));
1405
1406 assert(!isClose(1,real.nan));
1407 assert(!isClose(real.nan,real.max));
1408 assert(!isClose(real.nan,real.nan));
1409
1410 assert(!isClose(-double.infinity, 1));
1411 }
1412
1413 @safe pure nothrow @nogc unittest
1414 {
1415 assert(isClose!(real[],real[],real)([],[]));
1416 assert(isClose(cast(real[])[],cast(real[])[]));
1417 }
1418
1419 @safe pure nothrow @nogc unittest
1420 {
1421 import std.conv : to;
1422
1423 float f = 31.79f;
1424 double d = 31.79;
1425 double f2d = f.to!double;
1426
1427 assert(isClose(f,f2d));
1428 assert(!isClose(d,f2d));
1429 }
1430
1431 @safe pure nothrow @nogc unittest
1432 {
1433 import std.conv : to;
1434
1435 double d = 31.79;
1436 float f = d.to!float;
1437 double f2d = f.to!double;
1438
1439 assert(isClose(f,f2d));
1440 assert(!isClose(d,f2d));
1441 assert(isClose(d,f2d,1e-4));
1442 }
1443
1444 package(std.math) template CommonDefaultFor(T,U)
1445 {
1446 import std.algorithm.comparison : min;
1447
1448 alias baseT = FloatingPointBaseType!T;
1449 alias baseU = FloatingPointBaseType!U;
1450
1451 enum CommonType!(baseT, baseU) CommonDefaultFor = 10.0L ^^ -((min(baseT.dig, baseU.dig) + 1) / 2 + 1);
1452 }
1453
1454 private template FloatingPointBaseType(T)
1455 {
1456 import std.range.primitives : ElementType;
1457 static if (isFloatingPoint!T)
1458 {
1459 alias FloatingPointBaseType = Unqual!T;
1460 }
1461 else static if (isFloatingPoint!(ElementType!(Unqual!T)))
1462 {
1463 alias FloatingPointBaseType = Unqual!(ElementType!(Unqual!T));
1464 }
1465 else
1466 {
1467 alias FloatingPointBaseType = real;
1468 }
1469 }
1470
1471 /***********************************
1472 * Defines a total order on all floating-point numbers.
1473 *
1474 * The order is defined as follows:
1475 * $(UL
1476 * $(LI All numbers in [-$(INFIN), +$(INFIN)] are ordered
1477 * the same way as by built-in comparison, with the exception of
1478 * -0.0, which is less than +0.0;)
1479 * $(LI If the sign bit is set (that is, it's 'negative'), $(NAN) is less
1480 * than any number; if the sign bit is not set (it is 'positive'),
1481 * $(NAN) is greater than any number;)
1482 * $(LI $(NAN)s of the same sign are ordered by the payload ('negative'
1483 * ones - in reverse order).)
1484 * )
1485 *
1486 * Returns:
1487 * negative value if `x` precedes `y` in the order specified above;
1488 * 0 if `x` and `y` are identical, and positive value otherwise.
1489 *
1490 * See_Also:
1491 * $(MYREF isIdentical)
1492 * Standards: Conforms to IEEE 754-2008
1493 */
1494 int cmp(T)(const(T) x, const(T) y) @nogc @trusted pure nothrow
1495 if (isFloatingPoint!T)
1496 {
1497 import std.math.traits : floatTraits, RealFormat;
1498
1499 alias F = floatTraits!T;
1500
1501 static if (F.realFormat == RealFormat.ieeeSingle
1502 || F.realFormat == RealFormat.ieeeDouble)
1503 {
1504 static if (T.sizeof == 4)
1505 alias UInt = uint;
1506 else
1507 alias UInt = ulong;
1508
1509 union Repainter
1510 {
1511 T number;
1512 UInt bits;
1513 }
1514
1515 enum msb = ~(UInt.max >>> 1);
1516
1517 import std.typecons : Tuple;
1518 Tuple!(Repainter, Repainter) vars = void;
1519 vars[0].number = x;
1520 vars[1].number = y;
1521
1522 foreach (ref var; vars)
1523 if (var.bits & msb)
1524 var.bits = ~var.bits;
1525 else
1526 var.bits |= msb;
1527
1528 if (vars[0].bits < vars[1].bits)
1529 return -1;
1530 else if (vars[0].bits > vars[1].bits)
1531 return 1;
1532 else
1533 return 0;
1534 }
1535 else static if (F.realFormat == RealFormat.ieeeExtended53
1536 || F.realFormat == RealFormat.ieeeExtended
1537 || F.realFormat == RealFormat.ieeeQuadruple)
1538 {
1539 static if (F.realFormat == RealFormat.ieeeQuadruple)
1540 alias RemT = ulong;
1541 else
1542 alias RemT = ushort;
1543
1544 struct Bits
1545 {
1546 ulong bulk;
1547 RemT rem;
1548 }
1549
1550 union Repainter
1551 {
1552 T number;
1553 Bits bits;
1554 ubyte[T.sizeof] bytes;
1555 }
1556
1557 import std.typecons : Tuple;
1558 Tuple!(Repainter, Repainter) vars = void;
1559 vars[0].number = x;
1560 vars[1].number = y;
1561
1562 foreach (ref var; vars)
1563 if (var.bytes[F.SIGNPOS_BYTE] & 0x80)
1564 {
1565 var.bits.bulk = ~var.bits.bulk;
1566 var.bits.rem = cast(typeof(var.bits.rem))(-1 - var.bits.rem); // ~var.bits.rem
1567 }
1568 else
1569 {
1570 var.bytes[F.SIGNPOS_BYTE] |= 0x80;
1571 }
1572
1573 version (LittleEndian)
1574 {
1575 if (vars[0].bits.rem < vars[1].bits.rem)
1576 return -1;
1577 else if (vars[0].bits.rem > vars[1].bits.rem)
1578 return 1;
1579 else if (vars[0].bits.bulk < vars[1].bits.bulk)
1580 return -1;
1581 else if (vars[0].bits.bulk > vars[1].bits.bulk)
1582 return 1;
1583 else
1584 return 0;
1585 }
1586 else
1587 {
1588 if (vars[0].bits.bulk < vars[1].bits.bulk)
1589 return -1;
1590 else if (vars[0].bits.bulk > vars[1].bits.bulk)
1591 return 1;
1592 else if (vars[0].bits.rem < vars[1].bits.rem)
1593 return -1;
1594 else if (vars[0].bits.rem > vars[1].bits.rem)
1595 return 1;
1596 else
1597 return 0;
1598 }
1599 }
1600 else
1601 {
1602 // IBM Extended doubledouble does not follow the general
1603 // sign-exponent-significand layout, so has to be handled generically
1604
1605 import std.math.traits : signbit, isNaN;
1606
1607 const int xSign = signbit(x),
1608 ySign = signbit(y);
1609
1610 if (xSign == 1 && ySign == 1)
1611 return cmp(-y, -x);
1612 else if (xSign == 1)
1613 return -1;
1614 else if (ySign == 1)
1615 return 1;
1616 else if (x < y)
1617 return -1;
1618 else if (x == y)
1619 return 0;
1620 else if (x > y)
1621 return 1;
1622 else if (isNaN(x) && !isNaN(y))
1623 return 1;
1624 else if (isNaN(y) && !isNaN(x))
1625 return -1;
1626 else if (getNaNPayload(x) < getNaNPayload(y))
1627 return -1;
1628 else if (getNaNPayload(x) > getNaNPayload(y))
1629 return 1;
1630 else
1631 return 0;
1632 }
1633 }
1634
1635 /// Most numbers are ordered naturally.
1636 @safe unittest
1637 {
1638 assert(cmp(-double.infinity, -double.max) < 0);
1639 assert(cmp(-double.max, -100.0) < 0);
1640 assert(cmp(-100.0, -0.5) < 0);
1641 assert(cmp(-0.5, 0.0) < 0);
1642 assert(cmp(0.0, 0.5) < 0);
1643 assert(cmp(0.5, 100.0) < 0);
1644 assert(cmp(100.0, double.max) < 0);
1645 assert(cmp(double.max, double.infinity) < 0);
1646
1647 assert(cmp(1.0, 1.0) == 0);
1648 }
1649
1650 /// Positive and negative zeroes are distinct.
1651 @safe unittest
1652 {
1653 assert(cmp(-0.0, +0.0) < 0);
1654 assert(cmp(+0.0, -0.0) > 0);
1655 }
1656
1657 /// Depending on the sign, $(NAN)s go to either end of the spectrum.
1658 @safe unittest
1659 {
1660 assert(cmp(-double.nan, -double.infinity) < 0);
1661 assert(cmp(double.infinity, double.nan) < 0);
1662 assert(cmp(-double.nan, double.nan) < 0);
1663 }
1664
1665 /// $(NAN)s of the same sign are ordered by the payload.
1666 @safe unittest
1667 {
1668 assert(cmp(NaN(10), NaN(20)) < 0);
1669 assert(cmp(-NaN(20), -NaN(10)) < 0);
1670 }
1671
1672 @safe unittest
1673 {
1674 import std.meta : AliasSeq;
1675 static foreach (T; AliasSeq!(float, double, real))
1676 {{
1677 T[] values = [-cast(T) NaN(20), -cast(T) NaN(10), -T.nan, -T.infinity,
1678 -T.max, -T.max / 2, T(-16.0), T(-1.0).nextDown,
1679 T(-1.0), T(-1.0).nextUp,
1680 T(-0.5), -T.min_normal, (-T.min_normal).nextUp,
1681 -2 * T.min_normal * T.epsilon,
1682 -T.min_normal * T.epsilon,
1683 T(-0.0), T(0.0),
1684 T.min_normal * T.epsilon,
1685 2 * T.min_normal * T.epsilon,
1686 T.min_normal.nextDown, T.min_normal, T(0.5),
1687 T(1.0).nextDown, T(1.0),
1688 T(1.0).nextUp, T(16.0), T.max / 2, T.max,
1689 T.infinity, T.nan, cast(T) NaN(10), cast(T) NaN(20)];
1690
1691 foreach (i, x; values)
1692 {
1693 foreach (y; values[i + 1 .. $])
1694 {
1695 assert(cmp(x, y) < 0);
1696 assert(cmp(y, x) > 0);
1697 }
1698 assert(cmp(x, x) == 0);
1699 }
1700 }}
1701 }
1702
1703 package(std): // not yet public
1704
1705 struct FloatingPointBitpattern(T)
1706 if (isFloatingPoint!T)
1707 {
1708 static if (T.mant_dig <= 64)
1709 {
1710 ulong mantissa;
1711 }
1712 else
1713 {
1714 ulong mantissa_lsb;
1715 ulong mantissa_msb;
1716 }
1717
1718 int exponent;
1719 bool negative;
1720 }
1721
1722 FloatingPointBitpattern!T extractBitpattern(T)(const(T) value) @trusted
1723 if (isFloatingPoint!T)
1724 {
1725 import std.math.traits : floatTraits, RealFormat;
1726
1727 T val = value;
1728 FloatingPointBitpattern!T ret;
1729
1730 alias F = floatTraits!T;
1731 static if (F.realFormat == RealFormat.ieeeExtended)
1732 {
1733 if (__ctfe)
1734 {
1735 import core.math : fabs, ldexp;
1736 import std.math.rounding : floor;
1737 import std.math.traits : isInfinity, isNaN, signbit;
1738 import std.math.exponential : log2;
1739
1740 if (isNaN(val) || isInfinity(val))
1741 ret.exponent = 32767;
1742 else if (fabs(val) < real.min_normal)
1743 ret.exponent = 0;
1744 else if (fabs(val) >= nextUp(real.max / 2))
1745 ret.exponent = 32766;
1746 else
1747 ret.exponent = cast(int) (val.fabs.log2.floor() + 16383);
1748
1749 if (ret.exponent == 32767)
1750 {
1751 // NaN or infinity
1752 ret.mantissa = isNaN(val) ? ((1L << 63) - 1) : 0;
1753 }
1754 else
1755 {
1756 auto delta = 16382 + 64 // bias + bits of ulong
1757 - (ret.exponent == 0 ? 1 : ret.exponent); // -1 in case of subnormals
1758 val = ldexp(val, delta); // val *= 2^^delta
1759
1760 ulong tmp = cast(ulong) fabs(val);
1761 if (ret.exponent != 32767 && ret.exponent > 0 && tmp <= ulong.max / 2)
1762 {
1763 // correction, due to log2(val) being rounded up:
1764 ret.exponent--;
1765 val *= 2;
1766 tmp = cast(ulong) fabs(val);
1767 }
1768
1769 ret.mantissa = tmp & long.max;
1770 }
1771
1772 ret.negative = (signbit(val) == 1);
1773 }
1774 else
1775 {
1776 ushort* vs = cast(ushort*) &val;
1777 ret.mantissa = (cast(ulong*) vs)[0] & long.max;
1778 ret.exponent = vs[4] & short.max;
1779 ret.negative = (vs[4] >> 15) & 1;
1780 }
1781 }
1782 else
1783 {
1784 static if (F.realFormat == RealFormat.ieeeSingle)
1785 {
1786 ulong ival = *cast(uint*) &val;
1787 }
1788 else static if (F.realFormat == RealFormat.ieeeDouble)
1789 {
1790 ulong ival = *cast(ulong*) &val;
1791 }
1792 else
1793 {
1794 static assert(false, "Floating point type `" ~ F.realFormat ~ "` not supported.");
1795 }
1796
1797 import std.math.exponential : log2;
1798 enum log2_max_exp = cast(int) log2(T(T.max_exp));
1799
1800 ret.mantissa = ival & ((1L << (T.mant_dig - 1)) - 1);
1801 ret.exponent = (ival >> (T.mant_dig - 1)) & ((1L << (log2_max_exp + 1)) - 1);
1802 ret.negative = (ival >> (T.mant_dig + log2_max_exp)) & 1;
1803 }
1804
1805 // add leading 1 for normalized values and correct exponent for denormalied values
1806 if (ret.exponent != 0 && ret.exponent != 2 * T.max_exp - 1)
1807 ret.mantissa |= 1L << (T.mant_dig - 1);
1808 else if (ret.exponent == 0)
1809 ret.exponent = 1;
1810
1811 ret.exponent -= T.max_exp - 1;
1812
1813 return ret;
1814 }
1815
1816 @safe pure unittest
1817 {
1818 float f = 1.0f;
1819 auto bp = extractBitpattern(f);
1820 assert(bp.mantissa == 0x80_0000);
1821 assert(bp.exponent == 0);
1822 assert(bp.negative == false);
1823
1824 f = float.max;
1825 bp = extractBitpattern(f);
1826 assert(bp.mantissa == 0xff_ffff);
1827 assert(bp.exponent == 127);
1828 assert(bp.negative == false);
1829
1830 f = -1.5432e-17f;
1831 bp = extractBitpattern(f);
1832 assert(bp.mantissa == 0x8e_55c8);
1833 assert(bp.exponent == -56);
1834 assert(bp.negative == true);
1835
1836 // using double literal due to https://issues.dlang.org/show_bug.cgi?id=20361
1837 f = 2.3822073893521890206e-44;
1838 bp = extractBitpattern(f);
1839 assert(bp.mantissa == 0x00_0011);
1840 assert(bp.exponent == -126);
1841 assert(bp.negative == false);
1842
1843 f = -float.infinity;
1844 bp = extractBitpattern(f);
1845 assert(bp.mantissa == 0);
1846 assert(bp.exponent == 128);
1847 assert(bp.negative == true);
1848
1849 f = float.nan;
1850 bp = extractBitpattern(f);
1851 assert(bp.mantissa != 0); // we don't guarantee payloads
1852 assert(bp.exponent == 128);
1853 assert(bp.negative == false);
1854 }
1855
1856 @safe pure unittest
1857 {
1858 double d = 1.0;
1859 auto bp = extractBitpattern(d);
1860 assert(bp.mantissa == 0x10_0000_0000_0000L);
1861 assert(bp.exponent == 0);
1862 assert(bp.negative == false);
1863
1864 d = double.max;
1865 bp = extractBitpattern(d);
1866 assert(bp.mantissa == 0x1f_ffff_ffff_ffffL);
1867 assert(bp.exponent == 1023);
1868 assert(bp.negative == false);
1869
1870 d = -1.5432e-222;
1871 bp = extractBitpattern(d);
1872 assert(bp.mantissa == 0x11_d9b6_a401_3b04L);
1873 assert(bp.exponent == -737);
1874 assert(bp.negative == true);
1875
1876 d = 0.0.nextUp;
1877 bp = extractBitpattern(d);
1878 assert(bp.mantissa == 0x00_0000_0000_0001L);
1879 assert(bp.exponent == -1022);
1880 assert(bp.negative == false);
1881
1882 d = -double.infinity;
1883 bp = extractBitpattern(d);
1884 assert(bp.mantissa == 0);
1885 assert(bp.exponent == 1024);
1886 assert(bp.negative == true);
1887
1888 d = double.nan;
1889 bp = extractBitpattern(d);
1890 assert(bp.mantissa != 0); // we don't guarantee payloads
1891 assert(bp.exponent == 1024);
1892 assert(bp.negative == false);
1893 }
1894
1895 @safe pure unittest
1896 {
1897 import std.math.traits : floatTraits, RealFormat;
1898
1899 alias F = floatTraits!real;
1900 static if (F.realFormat == RealFormat.ieeeExtended)
1901 {
1902 real r = 1.0L;
1903 auto bp = extractBitpattern(r);
1904 assert(bp.mantissa == 0x8000_0000_0000_0000L);
1905 assert(bp.exponent == 0);
1906 assert(bp.negative == false);
1907
1908 r = real.max;
1909 bp = extractBitpattern(r);
1910 assert(bp.mantissa == 0xffff_ffff_ffff_ffffL);
1911 assert(bp.exponent == 16383);
1912 assert(bp.negative == false);
1913
1914 r = -1.5432e-3333L;
1915 bp = extractBitpattern(r);
1916 assert(bp.mantissa == 0xc768_a2c7_a616_cc22L);
1917 assert(bp.exponent == -11072);
1918 assert(bp.negative == true);
1919
1920 r = 0.0L.nextUp;
1921 bp = extractBitpattern(r);
1922 assert(bp.mantissa == 0x0000_0000_0000_0001L);
1923 assert(bp.exponent == -16382);
1924 assert(bp.negative == false);
1925
1926 r = -float.infinity;
1927 bp = extractBitpattern(r);
1928 assert(bp.mantissa == 0);
1929 assert(bp.exponent == 16384);
1930 assert(bp.negative == true);
1931
1932 r = float.nan;
1933 bp = extractBitpattern(r);
1934 assert(bp.mantissa != 0); // we don't guarantee payloads
1935 assert(bp.exponent == 16384);
1936 assert(bp.negative == false);
1937
1938 r = nextDown(0x1p+16383L);
1939 bp = extractBitpattern(r);
1940 assert(bp.mantissa == 0xffff_ffff_ffff_ffffL);
1941 assert(bp.exponent == 16382);
1942 assert(bp.negative == false);
1943 }
1944 }
1945
1946 @safe pure unittest
1947 {
1948 import std.math.traits : floatTraits, RealFormat;
1949 import std.math.exponential : log2;
1950
1951 alias F = floatTraits!real;
1952
1953 static if (F.realFormat == RealFormat.ieeeExtended)
1954 {
1955 // log2 is broken for x87-reals on some computers in CTFE
1956 // the following test excludes these computers from the test
1957 // (https://issues.dlang.org/show_bug.cgi?id=21757)
1958 enum test = cast(int) log2(3.05e2312L);
1959 static if (test == 7681)
1960 {
1961 enum r1 = 1.0L;
1962 enum bp1 = extractBitpattern(r1);
1963 static assert(bp1.mantissa == 0x8000_0000_0000_0000L);
1964 static assert(bp1.exponent == 0);
1965 static assert(bp1.negative == false);
1966
1967 enum r2 = real.max;
1968 enum bp2 = extractBitpattern(r2);
1969 static assert(bp2.mantissa == 0xffff_ffff_ffff_ffffL);
1970 static assert(bp2.exponent == 16383);
1971 static assert(bp2.negative == false);
1972
1973 enum r3 = -1.5432e-3333L;
1974 enum bp3 = extractBitpattern(r3);
1975 static assert(bp3.mantissa == 0xc768_a2c7_a616_cc22L);
1976 static assert(bp3.exponent == -11072);
1977 static assert(bp3.negative == true);
1978
1979 enum r4 = 0.0L.nextUp;
1980 enum bp4 = extractBitpattern(r4);
1981 static assert(bp4.mantissa == 0x0000_0000_0000_0001L);
1982 static assert(bp4.exponent == -16382);
1983 static assert(bp4.negative == false);
1984
1985 enum r5 = -real.infinity;
1986 enum bp5 = extractBitpattern(r5);
1987 static assert(bp5.mantissa == 0);
1988 static assert(bp5.exponent == 16384);
1989 static assert(bp5.negative == true);
1990
1991 enum r6 = real.nan;
1992 enum bp6 = extractBitpattern(r6);
1993 static assert(bp6.mantissa != 0); // we don't guarantee payloads
1994 static assert(bp6.exponent == 16384);
1995 static assert(bp6.negative == false);
1996
1997 enum r7 = nextDown(0x1p+16383L);
1998 enum bp7 = extractBitpattern(r7);
1999 static assert(bp7.mantissa == 0xffff_ffff_ffff_ffffL);
2000 static assert(bp7.exponent == 16382);
2001 static assert(bp7.negative == false);
2002 }
2003 }
2004 }