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