xref: /plan9/sys/src/libc/port/floor.c (revision 3e12c5d1bb89fc02707907988834ef147769ddaf)
1 #include <u.h>
2 #include <libc.h>
3 /*
4  * floor and ceil-- greatest integer <= arg
5  * (resp least >=)
6  */
7 
8 double
floor(double d)9 floor(double d)
10 {
11 	double fract;
12 
13 	if(d < 0) {
14 		fract = modf(-d, &d);
15 		if(fract != 0.0)
16 			d += 1;
17 		d = -d;
18 	} else
19 		modf(d, &d);
20 	return d;
21 }
22 
23 double
ceil(double d)24 ceil(double d)
25 {
26 	return -floor(-d);
27 }
28