PatchworkOS
Loading...
Searching...
No Matches
modf.c
Go to the documentation of this file.
1#include <math.h>
2#include <stdint.h>
3
4// https://cppreference.com/w/c/numeric/math/modf.html
5double modf(double x, double* iptr)
6{
7 if (x == 0.0 || x == -0.0)
8 {
9 *iptr = x;
10 return x;
11 }
12
13 if (isinf(x))
14 {
15 *iptr = x;
16 return x == INFINITY ? 0.0 : -0.0;
17 }
18
19 if (isnan(x))
20 {
21 *iptr = NAN;
22 return NAN;
23 }
24
25 double intPart;
26 double fracPart = 0.0;
27 if (x > 0.0)
28 {
29 intPart = floor(x);
30 fracPart = x - intPart;
31 }
32 else
33 {
34 intPart = ceil(x);
35 fracPart = x - intPart;
36 }
37 *iptr = intPart;
38 return fracPart;
39}
double ceil(double x)
Definition ceil.c:5
#define isinf(x)
Definition math.h:37
#define isnan(x)
Definition math.h:39
double floor(double x)
Definition floor.c:4
#define NAN
Definition math.h:20
#define INFINITY
Definition math.h:19
double modf(double x, double *iptr)
Definition modf.c:5
int64_t x
Definition main.c:152