summaryrefslogtreecommitdiff
path: root/src/math/fmod.c
blob: 154350082a7f83921e7627f782aaa8ab3b81e511 (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
# define TGSOURCE "fmod.c"
#include <errno.h>
#include <math.h>
#include "_tgmath.h"

/** floating-point remainder **/

TYPE TGFN(fmod)(TYPE x, TYPE y)
{
	int classx = fpclassify(x);
	int classy = fpclassify(y);

	if (classx == FP_ZERO && classy != FP_ZERO) {
		return x;
	}

	if (classx == FP_INFINITE && classy == FP_ZERO) {
		feraiseexcept(FE_INVALID);
		return NAN;
	}

	if (classx != FP_INFINITE && classy == FP_INFINITE) {
		return x;
	}

	if (y == 0) {
		return 0;
	}

	if (0) {
		errno = ERANGE; /* The result cannot be represented */
		/* RETURN_FAILURE(CONSTANT(HUGE_VAL), A range error occurred); */
		return TGHUGE;
	}

	/* RETURN_SUCCESS(ARGUMENT(x) - VAR(i) * ARGUMENT(y)); */
	return x - x / y;
}

/***
compute the floating-point remainder of ARGUMENT(x)/ARGUMENT(y).
FIXME: I am not sure I understand this.
***/

/*
IMPLEMENTATION(The value returned on a domain error, CONSTANT(HUGE_VAL))
IMPLEMENTATION(Whether ARGUMENT(y) being LITERAL(0) results in a domain error or THIS() returning LITERAL(0))
LINK(m)
STDC(1)
*/