[comp.lang.c] Floor function w/out math library

taranto@cory.Berkeley.EDU (Roger Taranto) (11/29/87)

Does anyone have a C function to calculate the floor of a number
without using the math library?

Thanks,
-Roger
taranto@cory.berkeley.edu

gwyn@brl-smoke.ARPA (Doug Gwyn ) (11/29/87)

In article <5103@zen.berkeley.edu> taranto@cory.Berkeley.EDU (Roger Taranto) writes:
>Does anyone have a C function to calculate the floor of a number
>without using the math library?

I'm not sure what you would consider to be in the "math library",
but there is supposed to be a floor() function in the standard C
library.  If for some reason you don't want to use this, and if
the range of values you want to convert are not too large in
magnitude, consider exploiting the truncation done by the
language when a floating-point quantity is converted to an integer:

	double floor( double x )
		{
		return x >= 0 ? (unsigned long)x :
			(unsigned long)-x == -x ? x :
				-1.0 - (unsigned long)-x;
		}