.
[glibc/history.git] / math / test-misc.c
blobd2393cc8406ed08824d47689039aeedd014b494d
1 /* Miscellaneous tests which don't fit anywhere else.
2 Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <fenv.h>
21 #include <float.h>
22 #include <ieee754.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include <string.h>
28 int
29 main (void)
31 int result = 0;
33 #ifndef NO_LONG_DOUBLE
35 long double x = 0x100000001ll + (long double) 0.5;
36 long double q;
37 long double r;
39 r = modfl (x, &q);
40 if (q != (long double) 0x100000001ll || r != 0.5)
42 printf ("modfl (%Lg, ...) failed\n", x);
43 result = 1;
47 # if __GNUC__ >= 3 || __GNUC_MINOR__ >= 96
49 long double x;
50 long double m;
51 long double r;
52 int e;
53 int i;
55 # if LDBL_MANT_DIG == 64
56 m = 0xf.fffffffffffffffp-4L;
57 # elif LDBL_MANT_DIG == 106
58 /* This has to match the mantissa of LDBL_MAX which actually does have a
59 missing bit in the middle. */
60 m = 0x1.fffffffffffff7ffffffffffff8p-1L;
61 # elif LDBL_MANT_DIG == 113
62 m = 0x1.ffffffffffffffffffffffffffffp-1L;
63 # else
64 # error "Please adjust"
65 # endif
67 for (i = LDBL_MAX_EXP, x = LDBL_MAX; i >= LDBL_MIN_EXP; --i, x /= 2.0L)
69 printf ("2^%d: ", i);
71 r = frexpl (x, &e);
72 if (r != m)
74 printf ("mantissa incorrect: %.20La\n", r);
75 result = 1;
76 continue;
78 if (e != i)
80 printf ("exponent wrong %d (%.20Lg)\n", e, x);
81 result = 1;
82 continue;
84 puts ("ok");
87 for (i = LDBL_MIN_EXP, x = LDBL_MIN; i >= LDBL_MIN_EXP - LDBL_MANT_DIG + 1;
88 --i, x /= 2.0L)
90 printf ("2^%d: ", i);
92 r = frexpl (x, &e);
93 if (r != 0.5L)
95 printf ("mantissa incorrect: %.20La\n", r);
96 result = 1;
97 continue;
99 if (e != i)
101 printf ("exponent wrong %d (%.20Lg)\n", e, x);
102 result = 1;
103 continue;
105 puts ("ok");
109 # endif
111 #if 0
113 int e;
114 long double r = frexpl (LDBL_MIN * LDBL_EPSILON, &e);
116 if (r != 0.5)
118 printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): mantissa wrong: %Lg\n",
120 result = 1;
122 else if (e != -16444)
124 printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): exponent wrong: %d\n",
126 result = 1;
129 #endif
130 #endif
133 double x = 0x100000001ll + (double) 0.5;
134 double q;
135 double r;
137 r = modf (x, &q);
138 if (q != (double) 0x100000001ll || r != 0.5)
140 printf ("modf (%g, ...) failed\n", x);
141 result = 1;
146 union ieee754_float v1;
147 union ieee754_float v2;
148 float f;
150 v1.f = f = FLT_MIN;
151 if (fpclassify (f) != FP_NORMAL)
153 printf ("fpclassify (FLT_MIN) failed: %d\n", fpclassify (f));
154 result = 1;
156 f = nextafterf (f, FLT_MIN / 2.0f);
157 if (fpclassify (f) != FP_SUBNORMAL)
159 printf ("fpclassify (FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
160 result = 1;
162 v2.f = f = nextafterf (f, FLT_MIN);
163 if (fpclassify (f) != FP_NORMAL)
165 printf ("fpclassify (FLT_MIN-epsilon+epsilon) failed: %d\n",
166 fpclassify (f));
167 result = 1;
170 if (v1.ieee.mantissa != v2.ieee.mantissa)
172 printf ("FLT_MIN: mantissa differs: %8x vs %8x\n",
173 v1.ieee.mantissa, v2.ieee.mantissa);
174 result = 1;
176 if (v1.ieee.exponent != v2.ieee.exponent)
178 printf ("FLT_MIN: exponent differs: %4x vs %4x\n",
179 v1.ieee.exponent, v2.ieee.exponent);
180 result = 1;
182 if (v1.ieee.negative != v2.ieee.negative)
184 printf ("FLT_MIN: negative differs: %d vs %d\n",
185 v1.ieee.negative, v2.ieee.negative);
186 result = 1;
189 v1.f = f = -FLT_MIN;
190 if (fpclassify (f) != FP_NORMAL)
192 printf ("fpclassify (-FLT_MIN) failed: %d\n", fpclassify (f));
193 result = 1;
195 f = nextafterf (f, -FLT_MIN / 2.0f);
196 if (fpclassify (f) != FP_SUBNORMAL)
198 printf ("fpclassify (-FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
199 result = 1;
201 v2.f = f = nextafterf (f, -FLT_MIN);
202 if (fpclassify (f) != FP_NORMAL)
204 printf ("fpclassify (-FLT_MIN-epsilon+epsilon) failed: %d\n",
205 fpclassify (f));
206 result = 1;
209 if (v1.ieee.mantissa != v2.ieee.mantissa)
211 printf ("-FLT_MIN: mantissa differs: %8x vs %8x\n",
212 v1.ieee.mantissa, v2.ieee.mantissa);
213 result = 1;
215 if (v1.ieee.exponent != v2.ieee.exponent)
217 printf ("-FLT_MIN: exponent differs: %4x vs %4x\n",
218 v1.ieee.exponent, v2.ieee.exponent);
219 result = 1;
221 if (v1.ieee.negative != v2.ieee.negative)
223 printf ("-FLT_MIN: negative differs: %d vs %d\n",
224 v1.ieee.negative, v2.ieee.negative);
225 result = 1;
228 f = FLT_MAX;
229 if (fpclassify (f) != FP_NORMAL)
231 printf ("fpclassify (FLT_MAX) failed: %d\n", fpclassify (f));
232 result = 1;
234 f = nextafterf (f, INFINITY);
235 if (fpclassify (f) != FP_INFINITE)
237 printf ("fpclassify (FLT_MAX+epsilon) failed: %d\n", fpclassify (f));
238 result = 1;
241 f = -FLT_MAX;
242 if (fpclassify (f) != FP_NORMAL)
244 printf ("fpclassify (-FLT_MAX) failed: %d\n", fpclassify (f));
245 result = 1;
247 f = nextafterf (f, -INFINITY);
248 if (fpclassify (f) != FP_INFINITE)
250 printf ("fpclassify (-FLT_MAX-epsilon) failed: %d\n", fpclassify (f));
251 result = 1;
254 v1.f = f = 0.0625;
255 f = nextafterf (f, 0.0);
256 v2.f = f = nextafterf (f, 1.0);
258 if (v1.ieee.mantissa != v2.ieee.mantissa)
260 printf ("0.0625f down: mantissa differs: %8x vs %8x\n",
261 v1.ieee.mantissa, v2.ieee.mantissa);
262 result = 1;
264 if (v1.ieee.exponent != v2.ieee.exponent)
266 printf ("0.0625f down: exponent differs: %4x vs %4x\n",
267 v1.ieee.exponent, v2.ieee.exponent);
268 result = 1;
270 if (v1.ieee.negative != v2.ieee.negative)
272 printf ("0.0625f down: negative differs: %d vs %d\n",
273 v1.ieee.negative, v2.ieee.negative);
274 result = 1;
277 v1.f = f = 0.0625;
278 f = nextafterf (f, 1.0);
279 v2.f = f = nextafterf (f, 0.0);
281 if (v1.ieee.mantissa != v2.ieee.mantissa)
283 printf ("0.0625f up: mantissa differs: %8x vs %8x\n",
284 v1.ieee.mantissa, v2.ieee.mantissa);
285 result = 1;
287 if (v1.ieee.exponent != v2.ieee.exponent)
289 printf ("0.0625f up: exponent differs: %4x vs %4x\n",
290 v1.ieee.exponent, v2.ieee.exponent);
291 result = 1;
293 if (v1.ieee.negative != v2.ieee.negative)
295 printf ("0.0625f up: negative differs: %d vs %d\n",
296 v1.ieee.negative, v2.ieee.negative);
297 result = 1;
300 v1.f = f = -0.0625;
301 f = nextafterf (f, 0.0);
302 v2.f = f = nextafterf (f, -1.0);
304 if (v1.ieee.mantissa != v2.ieee.mantissa)
306 printf ("-0.0625f up: mantissa differs: %8x vs %8x\n",
307 v1.ieee.mantissa, v2.ieee.mantissa);
308 result = 1;
310 if (v1.ieee.exponent != v2.ieee.exponent)
312 printf ("-0.0625f up: exponent differs: %4x vs %4x\n",
313 v1.ieee.exponent, v2.ieee.exponent);
314 result = 1;
316 if (v1.ieee.negative != v2.ieee.negative)
318 printf ("-0.0625f up: negative differs: %d vs %d\n",
319 v1.ieee.negative, v2.ieee.negative);
320 result = 1;
323 v1.f = f = -0.0625;
324 f = nextafterf (f, -1.0);
325 v2.f = f = nextafterf (f, 0.0);
327 if (v1.ieee.mantissa != v2.ieee.mantissa)
329 printf ("-0.0625f down: mantissa differs: %8x vs %8x\n",
330 v1.ieee.mantissa, v2.ieee.mantissa);
331 result = 1;
333 if (v1.ieee.exponent != v2.ieee.exponent)
335 printf ("-0.0625f down: exponent differs: %4x vs %4x\n",
336 v1.ieee.exponent, v2.ieee.exponent);
337 result = 1;
339 if (v1.ieee.negative != v2.ieee.negative)
341 printf ("-0.0625f down: negative differs: %d vs %d\n",
342 v1.ieee.negative, v2.ieee.negative);
343 result = 1;
346 v1.f = f = 0.0f;
347 f = nextafterf (f, 1.0);
348 v2.f = nextafterf (f, -1.0);
350 if (v1.ieee.mantissa != v2.ieee.mantissa)
352 printf ("0.0f up: mantissa differs: %8x vs %8x\n",
353 v1.ieee.mantissa, v2.ieee.mantissa);
354 result = 1;
356 if (v1.ieee.exponent != v2.ieee.exponent)
358 printf ("0.0f up: exponent differs: %4x vs %4x\n",
359 v1.ieee.exponent, v2.ieee.exponent);
360 result = 1;
362 if (0 != v2.ieee.negative)
364 printf ("0.0f up: negative differs: 0 vs %d\n",
365 v2.ieee.negative);
366 result = 1;
369 v1.f = f = 0.0f;
370 f = nextafterf (f, -1.0);
371 v2.f = nextafterf (f, 1.0);
373 if (v1.ieee.mantissa != v2.ieee.mantissa)
375 printf ("0.0f down: mantissa differs: %8x vs %8x\n",
376 v1.ieee.mantissa, v2.ieee.mantissa);
377 result = 1;
379 if (v1.ieee.exponent != v2.ieee.exponent)
381 printf ("0.0f down: exponent differs: %4x vs %4x\n",
382 v1.ieee.exponent, v2.ieee.exponent);
383 result = 1;
385 if (1 != v2.ieee.negative)
387 printf ("0.0f down: negative differs: 1 vs %d\n",
388 v2.ieee.negative);
389 result = 1;
392 if (nextafterf (0.0f, INFINITY) != nextafterf (0.0f, 1.0f)
393 || nextafterf (-0.0f, INFINITY) != nextafterf (-0.0f, 1.0f)
394 || nextafterf (0.0f, -INFINITY) != nextafterf (0.0f, -1.0f)
395 || nextafterf (-0.0f, -INFINITY) != nextafterf (-0.0f, -1.0f))
397 printf ("nextafterf (+-0, +-Inf) != nextafterf (+-0, +-1)\n");
398 result = 1;
401 if (nexttowardf (0.0f, INFINITY) != nexttowardf (0.0f, 1.0f)
402 || nexttowardf (-0.0f, INFINITY) != nexttowardf (-0.0f, 1.0f)
403 || nexttowardf (0.0f, -INFINITY) != nexttowardf (0.0f, -1.0f)
404 || nexttowardf (-0.0f, -INFINITY) != nexttowardf (-0.0f, -1.0f))
406 printf ("nexttowardf (+-0, +-Inf) != nexttowardf (+-0, +-1)\n");
407 result = 1;
412 union ieee754_double v1;
413 union ieee754_double v2;
414 double d;
416 v1.d = d = DBL_MIN;
417 if (fpclassify (d) != FP_NORMAL)
419 printf ("fpclassify (DBL_MIN) failed: %d\n", fpclassify (d));
420 result = 1;
422 d = nextafter (d, DBL_MIN / 2.0);
423 if (fpclassify (d) != FP_SUBNORMAL)
425 printf ("fpclassify (DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
426 result = 1;
428 v2.d = d = nextafter (d, DBL_MIN);
429 if (fpclassify (d) != FP_NORMAL)
431 printf ("fpclassify (DBL_MIN-epsilon+epsilon) failed: %d\n",
432 fpclassify (d));
433 result = 1;
436 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
438 printf ("DBL_MIN: mantissa0 differs: %8x vs %8x\n",
439 v1.ieee.mantissa0, v2.ieee.mantissa0);
440 result = 1;
442 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
444 printf ("DBL_MIN: mantissa1 differs: %8x vs %8x\n",
445 v1.ieee.mantissa1, v2.ieee.mantissa1);
446 result = 1;
448 if (v1.ieee.exponent != v2.ieee.exponent)
450 printf ("DBL_MIN: exponent differs: %4x vs %4x\n",
451 v1.ieee.exponent, v2.ieee.exponent);
452 result = 1;
454 if (v1.ieee.negative != v2.ieee.negative)
456 printf ("DBL_MIN: negative differs: %d vs %d\n",
457 v1.ieee.negative, v2.ieee.negative);
458 result = 1;
461 v1.d = d = -DBL_MIN;
462 if (fpclassify (d) != FP_NORMAL)
464 printf ("fpclassify (-DBL_MIN) failed: %d\n", fpclassify (d));
465 result = 1;
467 d = nextafter (d, -DBL_MIN / 2.0);
468 if (fpclassify (d) != FP_SUBNORMAL)
470 printf ("fpclassify (-DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
471 result = 1;
473 v2.d = d = nextafter (d, -DBL_MIN);
474 if (fpclassify (d) != FP_NORMAL)
476 printf ("fpclassify (-DBL_MIN-epsilon+epsilon) failed: %d\n",
477 fpclassify (d));
478 result = 1;
481 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
483 printf ("-DBL_MIN: mantissa0 differs: %8x vs %8x\n",
484 v1.ieee.mantissa0, v2.ieee.mantissa0);
485 result = 1;
487 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
489 printf ("-DBL_MIN: mantissa1 differs: %8x vs %8x\n",
490 v1.ieee.mantissa1, v2.ieee.mantissa1);
491 result = 1;
493 if (v1.ieee.exponent != v2.ieee.exponent)
495 printf ("-DBL_MIN: exponent differs: %4x vs %4x\n",
496 v1.ieee.exponent, v2.ieee.exponent);
497 result = 1;
499 if (v1.ieee.negative != v2.ieee.negative)
501 printf ("-DBL_MIN: negative differs: %d vs %d\n",
502 v1.ieee.negative, v2.ieee.negative);
503 result = 1;
506 d = DBL_MAX;
507 if (fpclassify (d) != FP_NORMAL)
509 printf ("fpclassify (DBL_MAX) failed: %d\n", fpclassify (d));
510 result = 1;
512 d = nextafter (d, INFINITY);
513 if (fpclassify (d) != FP_INFINITE)
515 printf ("fpclassify (DBL_MAX+epsilon) failed: %d\n", fpclassify (d));
516 result = 1;
519 d = -DBL_MAX;
520 if (fpclassify (d) != FP_NORMAL)
522 printf ("fpclassify (-DBL_MAX) failed: %d\n", fpclassify (d));
523 result = 1;
525 d = nextafter (d, -INFINITY);
526 if (fpclassify (d) != FP_INFINITE)
528 printf ("fpclassify (-DBL_MAX-epsilon) failed: %d\n", fpclassify (d));
529 result = 1;
532 v1.d = d = 0.0625;
533 d = nextafter (d, 0.0);
534 v2.d = d = nextafter (d, 1.0);
536 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
538 printf ("0.0625 down: mantissa0 differs: %8x vs %8x\n",
539 v1.ieee.mantissa0, v2.ieee.mantissa0);
540 result = 1;
542 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
544 printf ("0.0625 down: mantissa1 differs: %8x vs %8x\n",
545 v1.ieee.mantissa1, v2.ieee.mantissa1);
546 result = 1;
548 if (v1.ieee.exponent != v2.ieee.exponent)
550 printf ("0.0625 down: exponent differs: %4x vs %4x\n",
551 v1.ieee.exponent, v2.ieee.exponent);
552 result = 1;
554 if (v1.ieee.negative != v2.ieee.negative)
556 printf ("0.0625 down: negative differs: %d vs %d\n",
557 v1.ieee.negative, v2.ieee.negative);
558 result = 1;
561 v1.d = d = 0.0625;
562 d = nextafter (d, 1.0);
563 v2.d = d = nextafter (d, 0.0);
565 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
567 printf ("0.0625 up: mantissa0 differs: %8x vs %8x\n",
568 v1.ieee.mantissa0, v2.ieee.mantissa0);
569 result = 1;
571 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
573 printf ("0.0625 up: mantissa1 differs: %8x vs %8x\n",
574 v1.ieee.mantissa1, v2.ieee.mantissa1);
575 result = 1;
577 if (v1.ieee.exponent != v2.ieee.exponent)
579 printf ("0.0625 up: exponent differs: %4x vs %4x\n",
580 v1.ieee.exponent, v2.ieee.exponent);
581 result = 1;
583 if (v1.ieee.negative != v2.ieee.negative)
585 printf ("0.0625 up: negative differs: %d vs %d\n",
586 v1.ieee.negative, v2.ieee.negative);
587 result = 1;
590 v1.d = d = -0.0625;
591 d = nextafter (d, 0.0);
592 v2.d = d = nextafter (d, -1.0);
594 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
596 printf ("-0.0625 up: mantissa0 differs: %8x vs %8x\n",
597 v1.ieee.mantissa0, v2.ieee.mantissa0);
598 result = 1;
600 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
602 printf ("-0.0625 up: mantissa1 differs: %8x vs %8x\n",
603 v1.ieee.mantissa1, v2.ieee.mantissa1);
604 result = 1;
606 if (v1.ieee.exponent != v2.ieee.exponent)
608 printf ("-0.0625 up: exponent differs: %4x vs %4x\n",
609 v1.ieee.exponent, v2.ieee.exponent);
610 result = 1;
612 if (v1.ieee.negative != v2.ieee.negative)
614 printf ("-0.0625 up: negative differs: %d vs %d\n",
615 v1.ieee.negative, v2.ieee.negative);
616 result = 1;
619 v1.d = d = -0.0625;
620 d = nextafter (d, -1.0);
621 v2.d = d = nextafter (d, 0.0);
623 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
625 printf ("-0.0625 down: mantissa0 differs: %8x vs %8x\n",
626 v1.ieee.mantissa0, v2.ieee.mantissa0);
627 result = 1;
629 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
631 printf ("-0.0625 down: mantissa1 differs: %8x vs %8x\n",
632 v1.ieee.mantissa1, v2.ieee.mantissa1);
633 result = 1;
635 if (v1.ieee.exponent != v2.ieee.exponent)
637 printf ("-0.0625 down: exponent differs: %4x vs %4x\n",
638 v1.ieee.exponent, v2.ieee.exponent);
639 result = 1;
641 if (v1.ieee.negative != v2.ieee.negative)
643 printf ("-0.0625 down: negative differs: %d vs %d\n",
644 v1.ieee.negative, v2.ieee.negative);
645 result = 1;
648 v1.d = d = 0.0;
649 d = nextafter (d, 1.0);
650 v2.d = nextafter (d, -1.0);
652 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
654 printf ("0.0 up: mantissa0 differs: %8x vs %8x\n",
655 v1.ieee.mantissa0, v2.ieee.mantissa0);
656 result = 1;
658 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
660 printf ("0.0 up: mantissa1 differs: %8x vs %8x\n",
661 v1.ieee.mantissa1, v2.ieee.mantissa1);
662 result = 1;
664 if (v1.ieee.exponent != v2.ieee.exponent)
666 printf ("0.0 up: exponent differs: %4x vs %4x\n",
667 v1.ieee.exponent, v2.ieee.exponent);
668 result = 1;
670 if (0 != v2.ieee.negative)
672 printf ("0.0 up: negative differs: 0 vs %d\n",
673 v2.ieee.negative);
674 result = 1;
677 v1.d = d = 0.0;
678 d = nextafter (d, -1.0);
679 v2.d = nextafter (d, 1.0);
681 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
683 printf ("0.0 down: mantissa0 differs: %8x vs %8x\n",
684 v1.ieee.mantissa0, v2.ieee.mantissa0);
685 result = 1;
687 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
689 printf ("0.0 down: mantissa1 differs: %8x vs %8x\n",
690 v1.ieee.mantissa1, v2.ieee.mantissa1);
691 result = 1;
693 if (v1.ieee.exponent != v2.ieee.exponent)
695 printf ("0.0 down: exponent differs: %4x vs %4x\n",
696 v1.ieee.exponent, v2.ieee.exponent);
697 result = 1;
699 if (1 != v2.ieee.negative)
701 printf ("0.0 down: negative differs: 1 vs %d\n",
702 v2.ieee.negative);
703 result = 1;
706 if (nextafter (0.0, INFINITY) != nextafter (0.0, 1.0)
707 || nextafter (-0.0, INFINITY) != nextafter (-0.0, 1.0)
708 || nextafter (0.0, -INFINITY) != nextafter (0.0, -1.0)
709 || nextafter (-0.0, -INFINITY) != nextafter (-0.0, -1.0))
711 printf ("nextafter (+-0, +-Inf) != nextafter (+-0, +-1)\n");
712 result = 1;
715 if (nexttoward (0.0, INFINITY) != nexttoward (0.0, 1.0)
716 || nexttoward (-0.0, INFINITY) != nexttoward (-0.0, 1.0)
717 || nexttoward (0.0, -INFINITY) != nexttoward (0.0, -1.0)
718 || nexttoward (-0.0, -INFINITY) != nexttoward (-0.0, -1.0))
720 printf ("nexttoward (+-0, +-Inf) != nexttoward (+-0, +-1)\n");
721 result = 1;
725 #ifndef NO_LONG_DOUBLE
727 union ieee854_long_double v1;
728 union ieee854_long_double v2;
729 long double ld;
731 v1.d = ld = LDBL_MIN;
732 if (fpclassify (ld) != FP_NORMAL)
734 printf ("fpclassify (LDBL_MIN) failed: %d\n", fpclassify (ld));
735 result = 1;
737 ld = nextafterl (ld, LDBL_MIN / 2.0);
738 if (fpclassify (ld) != FP_SUBNORMAL)
740 printf ("fpclassify (LDBL_MIN-epsilon) failed: %d (%La)\n",
741 fpclassify (ld), ld);
742 result = 1;
744 v2.d = ld = nextafterl (ld, LDBL_MIN);
745 if (fpclassify (ld) != FP_NORMAL)
747 printf ("fpclassify (LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
748 fpclassify (ld), ld);
749 result = 1;
752 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
754 printf ("LDBL_MIN: mantissa0 differs: %8x vs %8x\n",
755 v1.ieee.mantissa0, v2.ieee.mantissa0);
756 result = 1;
758 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
760 printf ("LDBL_MIN: mantissa1 differs: %8x vs %8x\n",
761 v1.ieee.mantissa1, v2.ieee.mantissa1);
762 result = 1;
764 if (v1.ieee.exponent != v2.ieee.exponent)
766 printf ("LDBL_MIN: exponent differs: %4x vs %4x\n",
767 v1.ieee.exponent, v2.ieee.exponent);
768 result = 1;
770 if (v1.ieee.negative != v2.ieee.negative)
772 printf ("LDBL_MIN: negative differs: %d vs %d\n",
773 v1.ieee.negative, v2.ieee.negative);
774 result = 1;
777 v1.d = ld = -LDBL_MIN;
778 if (fpclassify (ld) != FP_NORMAL)
780 printf ("fpclassify (-LDBL_MIN) failed: %d\n", fpclassify (ld));
781 result = 1;
783 ld = nextafterl (ld, -LDBL_MIN / 2.0);
784 if (fpclassify (ld) != FP_SUBNORMAL)
786 printf ("fpclassify (-LDBL_MIN-epsilon) failed: %d (%La)\n",
787 fpclassify (ld), ld);
788 result = 1;
790 v2.d = ld = nextafterl (ld, -LDBL_MIN);
791 if (fpclassify (ld) != FP_NORMAL)
793 printf ("fpclassify (-LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
794 fpclassify (ld), ld);
795 result = 1;
798 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
800 printf ("-LDBL_MIN: mantissa0 differs: %8x vs %8x\n",
801 v1.ieee.mantissa0, v2.ieee.mantissa0);
802 result = 1;
804 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
806 printf ("-LDBL_MIN: mantissa1 differs: %8x vs %8x\n",
807 v1.ieee.mantissa1, v2.ieee.mantissa1);
808 result = 1;
810 if (v1.ieee.exponent != v2.ieee.exponent)
812 printf ("-LDBL_MIN: exponent differs: %4x vs %4x\n",
813 v1.ieee.exponent, v2.ieee.exponent);
814 result = 1;
816 if (v1.ieee.negative != v2.ieee.negative)
818 printf ("-LDBL_MIN: negative differs: %d vs %d\n",
819 v1.ieee.negative, v2.ieee.negative);
820 result = 1;
823 ld = LDBL_MAX;
824 if (fpclassify (ld) != FP_NORMAL)
826 printf ("fpclassify (LDBL_MAX) failed: %d\n", fpclassify (ld));
827 result = 1;
829 ld = nextafterl (ld, INFINITY);
830 if (fpclassify (ld) != FP_INFINITE)
832 printf ("fpclassify (LDBL_MAX+epsilon) failed: %d\n", fpclassify (ld));
833 result = 1;
836 ld = -LDBL_MAX;
837 if (fpclassify (ld) != FP_NORMAL)
839 printf ("fpclassify (-LDBL_MAX) failed: %d\n", fpclassify (ld));
840 result = 1;
842 ld = nextafterl (ld, -INFINITY);
843 if (fpclassify (ld) != FP_INFINITE)
845 printf ("fpclassify (-LDBL_MAX-epsilon) failed: %d\n",
846 fpclassify (ld));
847 result = 1;
850 v1.d = ld = 0.0625;
851 ld = nextafterl (ld, 0.0);
852 v2.d = ld = nextafterl (ld, 1.0);
854 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
856 printf ("0.0625L down: mantissa0 differs: %8x vs %8x\n",
857 v1.ieee.mantissa0, v2.ieee.mantissa0);
858 result = 1;
860 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
862 printf ("0.0625L down: mantissa1 differs: %8x vs %8x\n",
863 v1.ieee.mantissa1, v2.ieee.mantissa1);
864 result = 1;
866 if (v1.ieee.exponent != v2.ieee.exponent)
868 printf ("0.0625L down: exponent differs: %4x vs %4x\n",
869 v1.ieee.exponent, v2.ieee.exponent);
870 result = 1;
872 if (v1.ieee.negative != v2.ieee.negative)
874 printf ("0.0625L down: negative differs: %d vs %d\n",
875 v1.ieee.negative, v2.ieee.negative);
876 result = 1;
879 v1.d = ld = 0.0625;
880 ld = nextafterl (ld, 1.0);
881 v2.d = ld = nextafterl (ld, 0.0);
883 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
885 printf ("0.0625L up: mantissa0 differs: %8x vs %8x\n",
886 v1.ieee.mantissa0, v2.ieee.mantissa0);
887 result = 1;
889 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
891 printf ("0.0625L up: mantissa1 differs: %8x vs %8x\n",
892 v1.ieee.mantissa1, v2.ieee.mantissa1);
893 result = 1;
895 if (v1.ieee.exponent != v2.ieee.exponent)
897 printf ("0.0625L up: exponent differs: %4x vs %4x\n",
898 v1.ieee.exponent, v2.ieee.exponent);
899 result = 1;
901 if (v1.ieee.negative != v2.ieee.negative)
903 printf ("0.0625L up: negative differs: %d vs %d\n",
904 v1.ieee.negative, v2.ieee.negative);
905 result = 1;
908 v1.d = ld = -0.0625;
909 ld = nextafterl (ld, 0.0);
910 v2.d = ld = nextafterl (ld, -1.0);
912 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
914 printf ("-0.0625L up: mantissa0 differs: %8x vs %8x\n",
915 v1.ieee.mantissa0, v2.ieee.mantissa0);
916 result = 1;
918 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
920 printf ("-0.0625L up: mantissa1 differs: %8x vs %8x\n",
921 v1.ieee.mantissa1, v2.ieee.mantissa1);
922 result = 1;
924 if (v1.ieee.exponent != v2.ieee.exponent)
926 printf ("-0.0625L up: exponent differs: %4x vs %4x\n",
927 v1.ieee.exponent, v2.ieee.exponent);
928 result = 1;
930 if (v1.ieee.negative != v2.ieee.negative)
932 printf ("-0.0625L up: negative differs: %d vs %d\n",
933 v1.ieee.negative, v2.ieee.negative);
934 result = 1;
937 v1.d = ld = -0.0625;
938 ld = nextafterl (ld, -1.0);
939 v2.d = ld = nextafterl (ld, 0.0);
941 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
943 printf ("-0.0625L down: mantissa0 differs: %8x vs %8x\n",
944 v1.ieee.mantissa0, v2.ieee.mantissa0);
945 result = 1;
947 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
949 printf ("-0.0625L down: mantissa1 differs: %8x vs %8x\n",
950 v1.ieee.mantissa1, v2.ieee.mantissa1);
951 result = 1;
953 if (v1.ieee.exponent != v2.ieee.exponent)
955 printf ("-0.0625L down: exponent differs: %4x vs %4x\n",
956 v1.ieee.exponent, v2.ieee.exponent);
957 result = 1;
959 if (v1.ieee.negative != v2.ieee.negative)
961 printf ("-0.0625L down: negative differs: %d vs %d\n",
962 v1.ieee.negative, v2.ieee.negative);
963 result = 1;
966 v1.d = ld = 0.0;
967 ld = nextafterl (ld, 1.0);
968 v2.d = nextafterl (ld, -1.0);
970 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
972 printf ("0.0L up: mantissa0 differs: %8x vs %8x\n",
973 v1.ieee.mantissa0, v2.ieee.mantissa0);
974 result = 1;
976 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
978 printf ("0.0L up: mantissa1 differs: %8x vs %8x\n",
979 v1.ieee.mantissa1, v2.ieee.mantissa1);
980 result = 1;
982 if (v1.ieee.exponent != v2.ieee.exponent)
984 printf ("0.0L up: exponent differs: %4x vs %4x\n",
985 v1.ieee.exponent, v2.ieee.exponent);
986 result = 1;
988 if (0 != v2.ieee.negative)
990 printf ("0.0L up: negative differs: 0 vs %d\n",
991 v2.ieee.negative);
992 result = 1;
995 v1.d = ld = 0.0;
996 ld = nextafterl (ld, -1.0);
997 v2.d = nextafterl (ld, 1.0);
999 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
1001 printf ("0.0L down: mantissa0 differs: %8x vs %8x\n",
1002 v1.ieee.mantissa0, v2.ieee.mantissa0);
1003 result = 1;
1005 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
1007 printf ("0.0L down: mantissa1 differs: %8x vs %8x\n",
1008 v1.ieee.mantissa1, v2.ieee.mantissa1);
1009 result = 1;
1011 if (v1.ieee.exponent != v2.ieee.exponent)
1013 printf ("0.0L down: exponent differs: %4x vs %4x\n",
1014 v1.ieee.exponent, v2.ieee.exponent);
1015 result = 1;
1017 if (1 != v2.ieee.negative)
1019 printf ("0.0L down: negative differs: 1 vs %d\n",
1020 v2.ieee.negative);
1021 result = 1;
1024 if (nextafterl (0.0, INFINITY) != nextafterl (0.0, 1.0)
1025 || nextafterl (-0.0, INFINITY) != nextafterl (-0.0, 1.0)
1026 || nextafterl (0.0, -INFINITY) != nextafterl (0.0, -1.0)
1027 || nextafterl (-0.0, -INFINITY) != nextafterl (-0.0, -1.0))
1029 printf ("nextafterl (+-0, +-Inf) != nextafterl (+-0, +-1)\n");
1030 result = 1;
1033 if (nexttowardl (0.0L, INFINITY) != nexttowardl (0.0L, 1.0L)
1034 || nexttowardl (-0.0L, INFINITY) != nexttowardl (-0.0L, 1.0L)
1035 || nexttowardl (0.0L, -INFINITY) != nexttowardl (0.0L, -1.0L)
1036 || nexttowardl (-0.0L, -INFINITY) != nexttowardl (-0.0L, -1.0L))
1038 printf ("nexttowardl (+-0, +-Inf) != nexttowardl (+-0, +-1)\n");
1039 result = 1;
1042 #endif
1044 if (! isnormal (FLT_MIN))
1046 puts ("isnormal (FLT_MIN) failed");
1047 result = 1;
1049 if (! isnormal (DBL_MIN))
1051 puts ("isnormal (DBL_MIN) failed");
1052 result = 1;
1054 #ifndef NO_LONG_DOUBLE
1055 if (! isnormal (LDBL_MIN))
1057 puts ("isnormal (LDBL_MIN) failed");
1058 result = 1;
1060 #endif
1062 #ifdef __i386__
1063 /* This is a test for the strange long doubles in x86 FPUs. */
1065 union
1067 char b[10];
1068 long double d;
1069 } u =
1070 { .b = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0 } };
1072 if (fpclassify (u.d) != FP_NORMAL)
1074 printf ("fpclassify (0x00008000000000000000) failed: %d (%Lg)\n",
1075 fpclassify (u.d), u.d);
1076 result = 1;
1080 /* Special NaNs in x86 long double. Test for scalbl. */
1082 union
1084 char b[10];
1085 long double d;
1086 } u =
1087 { .b = { 0, 1, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f } };
1088 long double r;
1090 r = scalbl (u.d, 0.0);
1091 if (!isnan (r))
1093 puts ("scalbl(NaN, 0) does not return NaN");
1094 result = 1;
1096 else if (memcmp (&r, &u.d, sizeof (double)) != 0)
1098 puts ("scalbl(NaN, 0) does not return the same NaN");
1099 result = 1;
1102 #endif
1104 #ifndef NO_LONG_DOUBLE
1106 long double r;
1108 feclearexcept (FE_ALL_EXCEPT);
1109 r = scalbl (LDBL_MIN, 2147483647);
1110 if (! isinf (r))
1112 puts ("scalbl (LDBL_MIN, 2147483647) does not return Inf");
1113 result = 1;
1115 else if (signbit (r) != 0)
1117 puts ("scalbl (LDBL_MIN, 2147483647) returns -Inf");
1118 result = 1;
1120 else if (fetestexcept (FE_UNDERFLOW))
1122 puts ("scalbl(NaN, 0) raises underflow exception");
1123 result = 1;
1126 feclearexcept (FE_ALL_EXCEPT);
1127 r = scalbl (LDBL_MAX, -2147483647);
1128 if (r != 0.0)
1130 puts ("scalbl (LDBL_MAX, -2147483647) does not return 0");
1131 result = 1;
1133 else if (signbit (r) != 0)
1135 puts ("scalbl (LDBL_MAX, -2147483647) returns -Inf");
1136 result = 1;
1138 else if (fetestexcept (FE_OVERFLOW))
1140 puts ("scalbl(NaN, 0) raises overflow exception");
1141 result = 1;
1144 #endif
1146 /* The tests here are very similar to tests earlier in this file,
1147 the important difference is just that there are no intervening
1148 union variables that cause some GCC versions to hide possible
1149 bugs in nextafter* implementation. */
1150 if (nextafterf (nextafterf (FLT_MIN, FLT_MIN / 2.0), FLT_MIN) != FLT_MIN)
1152 puts ("nextafterf FLT_MIN test failed");
1153 result = 1;
1155 if (nextafterf (nextafterf (-FLT_MIN, -FLT_MIN / 2.0), -FLT_MIN)
1156 != -FLT_MIN)
1158 puts ("nextafterf -FLT_MIN test failed");
1159 result = 1;
1161 if (nextafter (nextafter (DBL_MIN, DBL_MIN / 2.0), DBL_MIN) != DBL_MIN)
1163 puts ("nextafter DBL_MIN test failed");
1164 result = 1;
1166 if (nextafter (nextafter (-DBL_MIN, -DBL_MIN / 2.0), -DBL_MIN) != -DBL_MIN)
1168 puts ("nextafter -DBL_MIN test failed");
1169 result = 1;
1171 #ifndef NO_LONG_DOUBLE
1172 if (nextafterl (nextafterl (LDBL_MIN, LDBL_MIN / 2.0), LDBL_MIN)
1173 != LDBL_MIN)
1175 puts ("nextafterl LDBL_MIN test failed");
1176 result = 1;
1178 if (nextafterl (nextafterl (-LDBL_MIN, -LDBL_MIN / 2.0), -LDBL_MIN)
1179 != -LDBL_MIN)
1181 puts ("nextafterl -LDBL_MIN test failed");
1182 result = 1;
1184 #endif
1186 return result;