Debian Codegraph @

gnulib-tests/test-math-h.c

raw
1 /* Test of <math.h> substitute.
2 Copyright (C) 2007-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2007. */
18
19 #include <config.h>
20
21 #include <math.h>
22
23 #ifndef INFINITY
24 # error INFINITY should be defined, added in ISO C 99
25 choke me
26 #endif
27
28 #ifndef NAN
29 # error NAN should be defined, added in ISO C 99
30 choke me
31 #endif
32
33 #ifndef HUGE_VALF
34 # error HUGE_VALF should be defined
35 choke me
36 #endif
37
38 #ifndef HUGE_VAL
39 # error HUGE_VAL should be defined
40 choke me
41 #endif
42
43 #ifndef HUGE_VALL
44 # error HUGE_VALL should be defined
45 choke me
46 #endif
47
48 #ifndef FP_ILOGB0
49 # error FP_ILOGB0 should be defined
50 choke me
51 #endif
52
53 #ifndef FP_ILOGBNAN
54 # error FP_ILOGBNAN should be defined
55 choke me
56 #endif
57
58 /* Check that INFINITY expands into a constant expression. */
59 float in = INFINITY;
60
61 /* Check that NAN expands into a constant expression. */
62 float na = NAN;
63
64 /* Check that HUGE_VALF expands into a constant expression. */
65 float hf = HUGE_VALF;
66
67 /* Check that HUGE_VAL expands into a constant expression. */
68 double hd = HUGE_VAL;
69
70 /* Check that HUGE_VALL expands into a constant expression. */
71 long double hl = HUGE_VALL;
72
73 #include <limits.h>
74
75 #include "macros.h"
76
77 /* Compare two numbers with ==.
78 This is a separate function because IRIX 6.5 "cc -O" miscompiles an
79 'x == x' test. */
80 static int
81 numeric_equalf (float x, float y)
82 {
83 return x == y;
84 }
85 static int
86 numeric_equald (double x, double y)
87 {
88 return x == y;
89 }
90 static int
91 numeric_equall (long double x, long double y)
92 {
93 return x == y;
94 }
95
96 int
97 main (void)
98 {
99 double d;
100 double zero = 0.0;
101
102 /* Check that INFINITY is a float. */
103 ASSERT (sizeof (INFINITY) == sizeof (float));
104
105 /* Check that NAN is a float. */
106 ASSERT (sizeof (NAN) == sizeof (float));
107
108 /* Check the value of NAN. */
109 d = NAN;
110 ASSERT (!numeric_equald (d, d));
111
112 /* Check the value of HUGE_VALF. */
113 ASSERT (numeric_equalf (HUGE_VALF, HUGE_VALF + HUGE_VALF));
114
115 /* Check the value of HUGE_VAL. */
116 d = HUGE_VAL;
117 ASSERT (numeric_equald (d, 1.0 / zero));
118 ASSERT (numeric_equald (HUGE_VAL, HUGE_VAL + HUGE_VAL));
119
120 /* Check the value of HUGE_VALL. */
121 ASSERT (numeric_equall (HUGE_VALL, HUGE_VALL + HUGE_VALL));
122
123 /* Check the value of FP_ILOGB0. */
124 ASSERT (FP_ILOGB0 == INT_MIN || FP_ILOGB0 == - INT_MAX);
125
126 /* Check the value of FP_ILOGBNAN. */
127 ASSERT (FP_ILOGBNAN == INT_MIN || FP_ILOGBNAN == INT_MAX);
128
129 return test_exit_status;
130 }