forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_h.c
582 lines (428 loc) · 14.7 KB
/
math_h.c
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
# math.h
Mathematical functions.
C99 made many improvements to it. It seems that the C community is trying to replace FORTRAN by C
for numerical computations, which would be a blessing as it would mean that the system programming
croud (C) would be closer to the numerical programming one (FORTRAN).
# Redundant mathematical functions
Many functions are redundant, but are furnished because of possible speedups and better precision.
For exapmle, `sqrt` and `pow` are redundant since in theoryin theory `sqrt(x) == pow(x,0.5)`.
However, many hardware platforms such as x86 implement a `sqrt` as a single instruction,
if you use `sqrt` it will be simpler for the compiler to use the x86 sqrt instruction
if it is available.
Using the hardware instruction may be faster.
It will also possibly be more precise since it is likelly that sqrt
would need several floating point operations to implement, each one meaning a loss of precision,
while the hardware can do them a single operation.
I guess however that good compilers will optimize `pow(x, 0.5)` to `sqrt(x)`.
Anyways, it is better to play on the safe side, and always use the most specific operation possible.
*/
#include "common.h"
const double ERR = 10e-6;
int main(void) {
/*
# Math errors
# Errors
The following errors exist for the standard library functions.
# Domain error
Value outside of function domain. E.g.: `sqrt(-1.0)`.
TODO undefined or implementation defined?
Detection: if `math_errhandling & MATH_ERRNO != 0`, `errno = ERANGE`
and a "divide-by-zero" floating point exception is raised.
# Pole error
Function has a pole at a point. Ex: `log(0.0)`, `tgamma(-1.0)`.
Return value: HUGE_VAL.
Detection if `math_errhandling & MATH_ERRNO != 0`, `errno = ERANGE`
and a "divide-by-zero" floating point exception is raised.
# Range errors
Occur if the result is too large or too small to fint into the return type.
There are two types of range errors overflow and underflow.
In both cases, if `math_errhandling & MATH_ERRNO != 0`,
`errno = ERANGE` and a "divide-by-zero" floating point exception is raised.
# Overflow
For exapmle, around poles, functions can have arbitrarily large values,
so it is possible that for a given input close enough to the pole that the output is too large to reprent.
Return value: HUGE_VAL, HUGE_VALF, or HUGE_VALL, acording to function's return type.
# Underflow
The output is too small to represent
Return value: an implementation-defined value whose magnitude is no greater than the smallest
normalized positive number in the specified type;
*/
printf("math_errhandling & MATH_ERRNO = %d\n", math_errhandling & MATH_ERRNO);
/*
# fabs
Floating point abs.
Integer version is in stdlib
*/
{
/* Absolute values, float version: */
assert(fabsl(-1.1) == 1.1);
}
#if __STDC_VERSION__ >= 199901L
/*
# fminl
# fmaxl
Max and min for floats.
*/
{
assert(fminl(0.1, 0.2) == 0.1);
assert(fmaxl(0.1, 0.2) == 0.2);
}
#endif
/*
# Rounding
*/
{
/*
# floor
# ceil
*/
{
assert(fabs(floor(0.5) - 0.0) < ERR);
assert(fabs(ceil(0.5) - 1.0) < ERR);
}
/*
# trunc
Never raises any errors because the new result always fits in the data type (magnitide decresases).
*/
{
assert(fabs(trunc(1.5) - 1.0) < ERR);
assert(fabs(trunc(-1.5) - -1.0) < ERR);
}
/*
# round
Away from 0 on mid case.
*/
{
assert(fabs(round( 1.25) - 1.0) < ERR);
assert(fabs(round( 1.5 ) - 2.0) < ERR);
assert(fabs(round( 1.75) - 2.0) < ERR);
assert(fabs(round(-1.5 ) - -2.0) < ERR);
}
/*
# modf
Decompose into fraction and integer parts.
*/
{
double d;
assert(fabs(modf(12.34, &d) - 0.34) < ERR);
assert(fabs(d - 12.0 ) < ERR);
}
/*
# frexp
Decompose into:
- integer exponent
- normalized mantissa, a fraction between 0.5 (inclusive) and 1.0 (exclusive)
Guaranteed to power of 2 representation,
even though this is not true for floating point?
E.g. FLT_RADIX macros indicate it.
*/
{
int i;
/* 1.5 = 0.75 * 2^1 */
assert(frexp(1.5, &i) == 0.75);
assert(i == 1);
/* 3.0 = 0.75 * 2^2 */
assert(frexp(3.0, &i) == 0.75);
assert(i == 2);
}
}
/*
# fma
Fused multiple add or floating point multiply and add.
Does addition and multiplication on one operation,
with a single rounding, reduncing rounding errors.
Has hardware implementations on certain platforms.
*/
{
assert(fabs(fma(2.0, 3.0, 4.0) - (2.0 * 3.0 + 4.0)) < ERR);
}
/* # Exponential functions */
{
/* # exp */
{
assert(fabs(exp(1.0) - 2.71) < 0.01);
}
/*
# ln
See log.
# log
Calculates the ln.
*/
{
assert(fabs(log(exp(1.0)) - 1.0) < ERR);
assert(fabs(log2(8.0) - 3.0) < ERR);
assert(fabs(log10(100.0) - 2.0) < ERR);
}
/*
# sqrt
Range is positive or zero. Negatives are a range error.
To get complex on negative values, use `csqrt`.
*/
{
assert(fabs(sqrt(4.0) - 2.0) < ERR);
/* GCC 4.7 -O3 is smart enough to see that this is bad: */
{
float f = -4.0;
/*printf("sqrt(-4.0) = %f\n", sqrt(f));*/
}
{
float f;
volatile float g;
f = -4.0;
errno = 0;
g = sqrt(f);
if (math_errhandling & MATH_ERRNO)
assert(errno == EDOM);
printf("sqrt(-4.0) = %f\n", f);
}
}
#if __STDC_VERSION__ >= 199901L
/*
# hypot
Hypotenuse: sqrt(x^2 + y^2)
*/
{
assert(fabs(hypot(3.0, 4.0) - 5.0) < ERR);
}
#endif
#if __STDC_VERSION__ >= 199901L
/*
# cbrt
CuBe RooT
*/
{
assert(fabs(cbrt(8.0 ) - 2.0) < ERR);
assert(fabs(cbrt(-8.0) - -2.0) < ERR);
}
#endif
/* # pow */
{
assert(fabs(pow(2.0, 3.0) - 8.0 ) < ERR);
}
}
/* # trig */
{
float f = sin(0.2);
assert(fabs(sin(0.0) - 0.0) < ERR);
assert(fabs(cos(0.0) - 1.0) < ERR);
/*
# PI
There is no predefined macro for PI. TODO why not? so convenient...
This is a standard way to get PI.
The only problem is a possible slight calculation overhead.
But don't worry much about it. For example in gcc 4.7, even with `gcc -O0` trigonometric functions
are calculated at compile time and stored in the program text.
*/
{
assert(fabs(acos(-1.0) - 3.14) < 0.01);
}
}
/* # erf: TODO0 understand */
/*
# factorial
There seems to be no integer factorial function,
but `gamma(n+1)` coincides with the factorials of `n` on the positive integers,
and may be faster to compute via analytic approximations that can be done to gamma
and/or via a hardware implementation, so just use gamma.
# gamma
Wiki link: <http://en.wikipedia.org/wiki/Gamma_function>
Extension of the factorials to the real numbers because:
- on the positive integergs:
gamma(n+1) == n!
- on the positive reals:
gamma(x+1) == x * gamma(x)
Has a holomorphic continuation to all imaginary plane, with poles on 0 and negative integers.
Implemented in C as `tgamma`.
# tgamma
True Gamma function. TODO0 Why True?
Computes the gamma function.
ANSI C says that it gives either domain or pole error on the negative integers.
# lgamma
lgamma = ln tgamma
*/
{
assert(fabs(tgamma(5.0) - 4*3*2) < ERR);
assert(fabs(tgamma(3.5) - 2.5*tgamma(2.5)) < ERR);
errno = 0;
volatile double d = tgamma(-1.0);
if (math_errhandling & MATH_ERRNO) {
if (errno == ERANGE)
assert(d == HUGE_VAL);
else
assert(errno == EDOM);
}
assert(fabs(lgamma(3.5) - log(tgamma(3.5))) < ERR);
}
/* Floating point manipulation functions. */
{
/*
# ldexp
ldexp(x, y) = x * (2^y)
*/
{
assert(fabs(ldexp(1.5, 2.0) - 6.0) < ERR);
}
/*
# nextafter
Return the next representable value in a direction.
If both arguments equal, return them.
# nexttowards
TODO diff from nextafter
*/
{
printf("nexttowards(0.0, 1.0) = %a\n", nexttoward(0.0, 1.0));
assert(nexttoward(0.0, 0.0) == 0.0);
printf("nextafter (0.0, 1.0) = %a\n", nextafter(0.0, 1.0));
}
}
/*
# Division by 0
Time to have some fun and do naughty things.
The outcome of a division by zero depends on wether it is an integer of floating operation.
# isfinite
# isinf
# isnan
# isnormal
TODO
# fpclassify
FP_INFINITE, FP_NAN, FP_NORMAL, FP_SUBNORMAL, FP_ZERO
*/
{
/*
# floating point exception
In x86, the following generates a floating point exception,
which is handled by a floating point exception handler function.
In Linux the default handler is implemented by the OS and sends a signal to our application,
which if we don't catch will kill us.
*/
if (0) {
/* gcc 4.7 is smart enough to warn on literal division by 0: */
{
/*int i = 1 / 0;*/
}
/* gcc 4.7 is not smart enough to warn here: */
{
volatile int i = 0;
printf("int 1/0 = %d\n", 1 / i);
/* On gcc 4.7 with `-O3` this may not generate an exception, */
/* as the compiler replaces 0 / X by 0. */
printf("int 0/0 = %d\n", 0 / i);
}
}
/*
# HUGE_VAL
Returned on overflow. TODO example.
Can equal `INFINITY`.
*/
{
/* double */
printf("HUGE_VAL = %f\n", HUGE_VAL);
/* float */
printf("HUGE_VALF = %f\n", HUGE_VALF);
/* long double */
printf("HUGE_VALL = %Lf\n", HUGE_VALL);
}
/*
# INFINITY
Result of operations such as:
1.0 / 0.0
Type: float.
There are two infinities: positive and negative.
It is possible that `INFINITY == HUGE_VALF`.
*/
{
/* [-]inf or [-]infinity implementation defined. */
printf("INFINITY = %f\n", INFINITY);
printf("-INFINITY = %f\n", -INFINITY);
volatile float f = 0;
assert(1 / f == INFINITY);
assert(isinf(INFINITY));
assert(!isnan(INFINITY));
assert(INFINITY + INFINITY == INFINITY);
assert(INFINITY + 1.0 == INFINITY);
assert(INFINITY - 1.0 == INFINITY);
assert(isnan(INFINITY - INFINITY));
assert(INFINITY * INFINITY == INFINITY);
assert(INFINITY * -INFINITY == -INFINITY);
assert(INFINITY * 2.0 == INFINITY);
assert(INFINITY * -1.0 == -INFINITY);
assert(isnan(INFINITY * 0.0));
assert(1.0 / INFINITY == 0.0);
assert(isnan(INFINITY / INFINITY));
/* Comparisons with INFINITY all work as expected. */
assert(INFINITY == INFINITY);
assert(INFINITY != - INFINITY);
assert(-INFINITY < - 1e100);
assert(1e100 < INFINITY);
}
/*
# NAN
Not a number.
Result of operations such as:
0.0 / 0.0
INFINITY - INFINITY
INFINITY * 0.o
INFINITY / INFINITY
And any operation involving NAN.
The sign of NAN has no meaning.
*/
{
/* [-]NAN or [-]NAN<more characters> implementation defined. */
printf("NAN = %f\n", NAN);
printf("-NAN = %f\n", -NAN);
/* TODO why do both fail? */
/*assert(0 / f == -NAN);*/
/*assert(0 / f == NAN);*/
volatile float f = 0;
assert(isnan(0 / f));
assert(isnan(NAN));
assert(!isinf(NAN));
assert(isnan(NAN));
assert(isnan(NAN + 1.0));
assert(isnan(NAN + INFINITY));
assert(isnan(NAN + NAN));
assert(isnan(NAN - 1.0));
assert(isnan(NAN * 2.0));
assert(isnan(NAN / 1.0));
assert(isnan(INFINITY - INFINITY));
assert(isnan(INFINITY * 0.0));
assert(isnan(INFINITY / INFINITY));
/*
NAN is not ordered. any compairison to it yields false!
This is logical since 0 is neither smaller, larger or equal to NAN.
*/
{
assert(!(0.0 < NAN));
assert(!(0.0 > NAN));
assert(!(0.0 == NAN));
}
}
#if __STDC_VERSION__ >= 199901L
/*
# isunordered
Macro.
isunordered(x, y)
Equals:
isnan(x) || isnan(y)
Likely exists because it is possible to optimize it in x86:
http://stackoverflow.com/questions/26053934/is-it-feasible-to-add-this-optimization-to-gcc?rq=1
*/
{
assert(!isunordered(1.0, 1.0));
assert(isunordered(NAN, 1.0));
assert(isunordered(1.0, NAN));
assert(isunordered(NAN, NAN));
}
#endif
}
/*
# FLT_EVAL_METHOD
# float_t
# double_t
*/
{
printf("FLT_EVAL_METHOD = %d\n", FLT_EVAL_METHOD);
}
return EXIT_SUCCESS;
}