PatchworkOS
966e257
A non-POSIX operating system.
Theme:
Default
Round
Robot
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
4
double
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
}
modf
double modf(double value, double *iptr)
Definition
modf.c:5
isinf
#define isinf(x)
Definition
math.h:37
isnan
#define isnan(x)
Definition
math.h:39
fmod
double fmod(double x, double y)
Definition
fmod.c:5
NAN
#define NAN
Definition
math.h:20
fabs
double fabs(double x)
Definition
fabs.c:3
x
int64_t x
Definition
main.c:152
round
double round(double x)
Definition
round.c:4
src
libstd
user
functions
math
round.c
Generated on Mon Dec 15 2025 21:55:53 for PatchworkOS by
1.9.8