floating point - C: difference between (int)x and floor(x)? - Stack Overflow
Does anyone know how to create a floor function in c
Propagation of Error Breaks with the Floor Function.
Consider how the equation you give at the start arises (i.e. the conditions under which it is derived). If those conditions don't hold, why would the equation apply?
More on reddit.comCompilation error; why can't gcc recognize the floor function here?
Videos
One big difference is that of negative numbers; if you change myF to -5.6, then casting to an int returns -5 while floor(myF) is -6.
As to which is preferable, as a rule of thumb I'd say to only cast to an int if you know that's what you need -- and since you're asking here, chances are that you probably want floor.
(Also note that with printf formatting, %ld is a long integer; a double is %lf.)
floor(n) returns the mathematical floor of n, that is, the greatest integer not greater than n. (int)n returns the truncation of n, the integer whose absolute value is no greater than that of n. Similarly, ceil(n) returns the mathematical ceiling of n, or the smallest integer not smaller than n. As AraK pointed out, the number returned by floor() or ceil() may not fit within the range of int.
I'm working on a project that doesn't use any c libraries and can't figure it out