Source

gdc / gdc.test / runnable / testmath.d

  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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// PERMUTE_ARGS:

import std.stdio;
import std.c.stdio;
import std.math;
//import std.math2;

extern (C) int sprintf(char*, in char*, ...);

/******************************************
 * Compare floating point numbers to n decimal digits of precision.
 * Returns:
 *	1	match
 *	0	nomatch
 */

private int equals(real x, real y, uint ndigits)
{
    printf("equals: x = %Lg\n", x);
    printf("equals: y = %Lg\n", y);
    printf("equals: ndigits = %d\n", ndigits);
    if (signbit(x) != signbit(y))
	return 0;

    if (isinf(x) && isinf(y))
	return 1;
    if (isinf(x) || isinf(y))
	return 0;

    if (isnan(x) && isnan(y))
	return 1;
    if (isnan(x) || isnan(y))
	return 0;

    char bufx[30];
    char bufy[30];
    assert(ndigits < bufx.length);

    int ix;
    int iy;
    ix = sprintf(bufx.ptr, "%.*Lg", ndigits, x);
    assert(ix < bufx.length);
    iy = sprintf(bufy.ptr, "%.*Lg", ndigits, y);
    assert(ix < bufy.length);

    printf("bufx = '%.*s'\n", ix, bufx[0 .. ix].ptr);
    printf("bufy = '%.*s'\n", iy, bufy[0 .. iy].ptr);

    return bufx[0 .. ix] == bufy[0 .. iy];
}

/****************************************
 * Simple function to compare two floating point values
 * to a specified precision.
 * Returns:
 *	1	match
 *	0	nomatch
 */

private int mfeq(real x, real y, real precision)
{
    if (x == y)
	return 1;
    if (isnan(x))
	return isnan(y);
    if (isnan(y))
	return 0;
    return fabs(x - y) <= precision;
}


/*************************************************/

void testldexp()
{
    static real vals[][3] =	// value,exp,ldexp
    [
	[	0,	0,	0],
	[	1,	0,	1],
	[	-1,	0,	-1],
	[	1,	1,	2],
	[	123,	10,	125952],
	[	real.max,	int.max,	real.infinity],
	[	real.max,	-int.max,	0],
	[	real.min,	-int.max,	0],
    ];
    int i;

    for (i = 0; i < vals.length; i++)
    {
	real x = vals[i][0];
	int exp = cast(int)vals[i][1];
	real z = vals[i][2];
	real l = ldexp(x, exp);

	//printf("ldexp(%Lg, %d) = %Lg, should be %Lg\n", x, exp, l, z);
	assert(equals(z, l, 7));
    }
}

/*************************************************/

void testldexp2()
{
    real r;

    writefln("%f", ldexp(3.0L, 3));
    r = ldexp(3.0L, 3);
    assert(r == 24);

    writefln("%f", ldexp(cast(real) 3.0, cast(int) 3));
    r = ldexp(cast(real) 3.0, cast(int) 3);
    assert(r == 24);

    real n = 3.0;
    int exp = 3;
    writefln("%f", ldexp(n, exp));
    r = ldexp(n, exp);
    assert(r == 24);
}


/*************************************************/

void testacos()
{
    assert(equals(acos(0.5), std.math.PI / 3, 16));
}

/*************************************************/

void testasin()
{
    assert(equals(asin(0.5), PI / 6, 16));
}

/*************************************************/

void testatan()
{
    assert(equals(atan(std.math.sqrt(3.0)), PI / 3, 16));
}

/*************************************************/

void testatan2()
{
    assert(equals(atan2(1.0L, std.math.sqrt(3.0L)), PI / 6, 16));
}

/*************************************************/

void testtan()
{
    assert(equals(tan(PI / 3), std.math.sqrt(3.0), 16));
}

/*************************************************/

void testfrexp()
{
    int exp;
    real mantissa = frexp(123.456, exp);
    assert(equals(mantissa * pow(2.0L, cast(real)exp), 123.456, 19));
}

/*************************************************/

void testceil()
{
    assert(ceil(+123.456) == +124);
    assert(ceil(-123.456) == -123);
}

/*************************************************/

void testfloor()
{
    assert(floor(+123.456) == +123);
    assert(floor(-123.456) == -124);
}

/*************************************************/

void testlog10()
{
    assert(equals(log10(1000), 3, 19));
}

/*************************************************/

void testlog()
{
    assert(equals(log(E), 1, 19));
}

/*************************************************/

void testlog2()
{
    assert(equals(log2(1024), 10, 19));
}

/*************************************************/

void testlog1p()
{
    assert(equals(log1p(E - 1), 1, 19));
}

/*************************************************/

void testexp()
{
    printf("exp(3.0) = %Lg, %Lg\n", exp(3.0), E * E * E);
    assert(equals(exp(3.0), E * E * E, 16));
}

/*************************************************/

void testpow()
{
    assert(equals(pow(2.0L, 10.0L), 1024, 19));
}

/*************************************************/

void testcosh()
{
    assert(equals(cosh(1.0), (E + 1.0 / E) / 2, 16));
}

/*************************************************/

void testsinh()
{
    assert(equals(sinh(1.0), (E - 1.0 / E) / 2, 16));
}

/*************************************************/

void testtanh()
{
    assert(equals(tanh(1.0), sinh(1.0) / cosh(1.0), 15));
}

/*************************************************/

void testacosh()
{
    assert(isnan(acosh(0.5)));
    assert(equals(acosh(cosh(3.0)), 3, 16));
}

/*************************************************/

void testasinh()
{
    assert(asinh(0.0) == 0);
    assert(equals(asinh(sinh(3.0)), 3, 16));
}

/*************************************************/

void testatanh()
{
    assert(atanh(0.0) == 0);
    assert(equals(atanh(tanh(0.5L)), 0.5, 16));
}

/*************************************************/

void testerf()
{
}

/*************************************************/

void testerfc()
{
}

/*************************************************/

void testexp2()
{
    assert( core.stdc.math.exp2f(0.0f) == 1 );
    assert( core.stdc.math.exp2 (0.0)  == 1 );
    assert( core.stdc.math.exp2l(0.0L) == 1 );
}

/*************************************************/

void testlogb()
{
}

/*************************************************/

void testcbrt()
{
    assert(equals(cbrt(125), 5, 19));
    assert(equals(cbrt(-125), -5, 19));
}

/*************************************************/

void testilogb()
{
}

/*************************************************/

enum ZX80 = sqrt(7.0f);
enum ZX81 = sqrt(7.0);
enum ZX82 = sqrt(7.0L);

void testsqrt()
{
}

/*************************************************/

void test1()
{
    float f = sqrt(2.0f);
    printf("%g\n", f);
    assert(fabs(f * f - 2.0f) < .00001);

    double d = sqrt(2.0);
    printf("%g\n", d);
    assert(fabs(d * d - 2.0) < .00001);

    real r = sqrt(2.0L);
    printf("%Lg\n", r);
    assert(fabs(r * r - 2.0) < .00001);
}

void test2()
{
    float f = fabs(-2.0f);
    printf("%g\n", f);
    assert(f == 2);

    double d = fabs(-2.0);
    printf("%g\n", d);
    assert(d == 2);

    real r = fabs(-2.0L);
    printf("%Lg\n", r);
    assert(r == 2);
}


void test3()
{
    float f = sin(-2.0f);
    printf("%g\n", f);
    assert(fabs(f - -0.909297f) < .00001);

    double d = sin(-2.0);
    printf("%g\n", d);
    assert(fabs(d - -0.909297f) < .00001);

    real r = sin(-2.0L);
    printf("%Lg\n", r);
    assert(fabs(r - -0.909297f) < .00001);
}


void test4()
{
    float f = cos(-2.0f);
    printf("%g\n", f);
    assert(fabs(f - -0.416147f) < .00001);

    double d = cos(-2.0);
    printf("%g\n", d);
    assert(fabs(d - -0.416147f) < .00001);

    real r = cos(-2.0L);
    printf("%Lg\n", r);
    assert(fabs(r - -0.416147f) < .00001);
}


void test5()
{
    float f = tan(-2.0f);
    printf("%g\n", f);
    assert(fabs(f - 2.18504f) < .00001);

    double d = tan(-2.0);
    printf("%g\n", d);
    assert(fabs(d - 2.18504f) < .00001);

    real r = tan(-2.0L);
    printf("%Lg\n", r);
    assert(fabs(r - 2.18504f) < .00001);
}


/*************************************************/

int main()
{
    testldexp();
    testldexp2();
    testacos();
    testasin();
    testatan();
    testatan2();
    testtan();
    testfrexp();
    testceil();
    testfloor();
    testlog10();
    testlog();
    testlog2();
    testlog1p();
    testexp();
    testpow();
    testcosh();
    testsinh();
    testtanh();
    testacosh();
    testasinh();
    testatanh();
    testerf();
    testerfc();
    testexp2();
    testlogb();
    testcbrt();
    testilogb();
    testsqrt();

    test1();
    test2();
    test3();
    test4();
    test5();

    printf("Success\n");
    return 0;
}