Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / test / math2.c
blobb7108de4ad2c404f4f6e0cc0ad78b05e8b2c9dac
2 #include "test.h"
3 #include <errno.h>
6 int
7 randi (void)
9 static int next;
10 next = (next * 1103515245) + 12345;
11 return ((next >> 16) & 0xffff);
14 double randx (void)
16 double res;
18 do
20 union {
21 short parts[4];
22 double res;
23 } u;
25 u.parts[0] = randi();
26 u.parts[1] = randi();
27 u.parts[2] = randi();
28 u.parts[3] = randi();
29 res = u.res;
31 } while (!finite(res));
33 return res ;
36 /* Return a random double, but bias for numbers closer to 0 */
37 double randy (void)
39 int pow;
40 double r= randx();
41 r = frexp(r, &pow);
42 return ldexp(r, randi() & 0x1f);
45 void
46 test_frexp (void)
48 int i;
49 double r;
50 int t;
52 float xf;
53 double gives;
55 int pow;
58 /* Frexp of x return a and n, where a * 2**n == x, so test this with a
59 set of random numbers */
60 for (t = 0; t < 2; t++)
62 for (i = 0; i < 1000; i++)
65 double x = randx();
66 line(i);
67 switch (t)
69 case 0:
70 newfunc("frexp/ldexp");
71 r = frexp(x, &pow);
72 if (r > 1.0 || r < -1.0)
74 /* Answer can never be > 1 or < 1 */
75 test_iok(0,1);
78 gives = ldexp(r ,pow);
79 test_mok(gives,x,62);
80 break;
81 case 1:
82 newfunc("frexpf/ldexpf");
83 if (x > FLT_MIN && x < FLT_MAX)
85 /* test floats too, but they have a smaller range so make sure x
86 isn't too big. Also x can get smaller than a float can
87 represent to make sure that doesn't happen too */
88 xf = x;
89 r = frexpf(xf, &pow);
90 if (r > 1.0 || r < -1.0)
92 /* Answer can never be > 1 or < -1 */
93 test_iok(0,1);
96 gives = ldexpf(r ,pow);
97 test_mok(gives,x, 32);
106 /* test a few numbers manually to make sure frexp/ldexp are not
107 testing as ok because both are broken */
109 r = frexp(64.0, &i);
111 test_mok(r, 0.5,64);
112 test_iok(i, 7);
114 r = frexp(96.0, &i);
116 test_mok(r, 0.75, 64);
117 test_iok(i, 7);
121 /* Test mod - this is given a real hammering by the strtod type
122 routines, here are some more tests.
124 By definition
126 modf = func(value, &iptr)
128 (*iptr + modf) == value
130 we test this
133 void
134 test_mod (void)
136 int i;
138 newfunc("modf");
141 for (i = 0; i < 1000; i++)
143 double intpart;
144 double n;
145 line(i);
146 n = randx();
147 if (finite(n) && n != 0.0 )
149 double r = modf(n, &intpart);
150 line(i);
151 test_mok(intpart + r, n, 63);
155 newfunc("modff");
157 for (i = 0; i < 1000; i++)
159 float intpart;
160 double nd;
161 line(i);
162 nd = randx() ;
163 if (nd < FLT_MAX && finitef(nd) && nd != 0.0)
165 float n = nd;
166 double r = modff(n, &intpart);
167 line(i);
168 test_mok(intpart + r, n, 32);
176 Test pow by multiplying logs
178 void
179 test_pow (void)
181 unsigned int i;
182 newfunc("pow");
184 for (i = 0; i < 1000; i++)
186 double n1;
187 double n2;
188 double res;
189 double shouldbe;
191 line(i);
192 n1 = fabs(randy());
193 n2 = fabs(randy()/100.0);
194 res = pow(n1, n2);
195 shouldbe = exp(log(n1) * n2);
196 test_mok(shouldbe, res,64);
199 newfunc("powf");
201 for (i = 0; i < 1000; i++)
203 double n1;
204 double n2;
205 double res;
206 double shouldbe;
208 errno = 0;
210 line(i);
211 n1 = fabs(randy());
212 n2 = fabs(randy()/100.0);
213 res = powf(n1, n2);
214 shouldbe = expf(logf(n1) * n2);
215 if (!errno)
216 test_mok(shouldbe, res,28);
226 void
227 test_math2 (void)
229 test_mod();
230 test_frexp();
231 test_pow();