Welcome to mirror list, hosted at ThFree Co, Russian Federation.

math2.c « test « libm « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50b5376467652a49c6699ab77bce6a0a585fda43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

#include "test.h"
#include <errno.h>


int
_DEFUN_VOID(randi)
{
  static int next;
  next = (next * 1103515245) + 12345;
  return ((next >> 16) & 0xffff);
}

double _DEFUN_VOID(randx)
{
  double res;
  
  do 
  {
    union {
	short parts[4];
	double res;
      } u;
    
    u.parts[0] = randi();
    u.parts[1] = randi();
    u.parts[2] = randi();
    u.parts[3] = randi();
    res = u.res;
    
  } while (!finite(res));
  
  return res ;
}

/* Return a random double, but bias for numbers closer to 0 */
double _DEFUN_VOID(randy)
{
  int pow;
  double r= randx();
  r = frexp(r, &pow);
  return ldexp(r, randi() & 0x1f);
}

void
_DEFUN_VOID(test_frexp)
{
  int i;
  double r;
  int t;
  
  float xf;  
  double gives;

  int pow;

  
  /* Frexp of x return a and n, where a * 2**n == x, so test this with a
     set of random numbers */
  for (t = 0; t < 2; t++)   
  {
    for (i = 0; i < 1000; i++)  
    {
      
      double x = randx();   
      line(i);   
      switch (t) 
      {
      case 0:
	newfunc("frexp/ldexp");
	r = frexp(x, &pow);
	if (r > 1.0 || r < -1.0) 
	{ 
	  /* Answer can never be > 1 or < 1 */
	  test_iok(0,1);
	}
	
	gives = ldexp(r ,pow);
	test_mok(gives,x,62);
	break;
      case 1:
	newfunc("frexpf/ldexpf");
	if (x > FLT_MIN && x < FLT_MAX)
	{
	  /* test floats too, but they have a smaller range so make sure x
	     isn't too big. Also x can get smaller than a float can
	     represent to make sure that doesn't happen too */
	  xf = x;
	  r = frexpf(xf, &pow);
	  if (r > 1.0 || r < -1.0) 
	  { 
	    /* Answer can never be > 1 or < -1 */
	    test_iok(0,1);
	  }

	  gives = ldexpf(r ,pow);
	  test_mok(gives,x, 32);
	  
	}
      }

    }
    
  }
  
  /* test a few numbers manually to make sure frexp/ldexp are not
     testing as ok because both are broken */

  r = frexp(64.0, &i);
  
  test_mok(r, 0.5,64);
  test_iok(i, 7);

  r = frexp(96.0, &i);
  
  test_mok(r, 0.75, 64);
  test_iok(i, 7);
  
}

/* Test mod - this is given a real hammering by the strtod type
   routines, here are some more tests.

   By definition

   modf = func(value, &iptr)

      (*iptr + modf) == value

   we test this

*/
void
_DEFUN_VOID(test_mod)
{
  int i;
  
  newfunc("modf");

  
  for (i = 0; i < 1000; i++) 
  {
    double intpart;
    double n;
    line(i);
    n  = randx();
    if (finite(n) && n != 0.0 )
    {
      double r = modf(n, &intpart);
      line(i);
      test_mok(intpart + r, n, 63);
    }
    
  }
  newfunc("modff");
  
  for (i = 0; i < 1000; i++) 
  {
    float intpart;
    double nd;
    line(i);
    nd  = randx() ;
    if (nd < FLT_MAX && finitef(nd) && nd != 0.0)
    {
      float n = nd;
      double r = modff(n, &intpart);
      line(i);
      test_mok(intpart + r, n, 32);
    }
  }


}

/*
Test pow by multiplying logs  
*/
void
_DEFUN_VOID(test_pow)
{
  unsigned int i;  
  newfunc("pow");

  for (i = 0; i < 1000; i++) 
  {
    double n1;
    double n2;
    double res;
    double shouldbe;

    line(i);  
    n1 = fabs(randy());
    n2 = fabs(randy()/100.0);
    res = pow(n1, n2);
    shouldbe = exp(log(n1) * n2);
    test_mok(shouldbe, res,64);
  }

  newfunc("powf");
  
  for (i = 0; i < 1000; i++) 
  {
    double n1;
    double n2;
    double res;
    double shouldbe;

    errno = 0;
    
    line(i);  
    n1 = fabs(randy());
    n2 = fabs(randy()/100.0);
    res = powf(n1, n2);
    shouldbe = expf(logf(n1) * n2);
    if (!errno)
     test_mok(shouldbe, res,28);
  }




}



void
_DEFUN_VOID(test_math2)
{
  test_mod();  
  test_frexp();
  test_pow();
}