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.)
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.
a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int. floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part, and returns the result as a double.
floor returns a double while a / b where both a and b are integers yields an integer value.
With the correct cast the value is the same.
If typeof operator existed in C (it does not) we would have:
(typeof (a /b)) floor(a / b) == a / b
EDIT: Now if the question is: is there any difference between:
(double) (a / b)
and
floor(a / (double) b)
the answer is yes. The results differ with respect to negative values.
Casting to an int will truncate toward zero. floor() will truncate toward negative infinite. This will give you different values if bar were negative.
As was said before, for positive numbers they are the same, but they differ for negative numbers. The rule is that int rounds towards 0, while floor rounds towards negative infinity.
floor(4.5) = (int)4.5 = 4
floor(-4.5) = -5
(int)(-4.5) = -4
This being said, there is also a difference in execution time. On my system, I've timed that casting is at least 3 times faster than floor.
I have code that needs the floor operation of a limited range of values, including negative numbers. And it needs to be very efficient, so we use the following function for it:
int int_floor(double x)
{
return (int)(x+100000) - 100000;
}
Of course this will fail for very large values of x (you will run into some overflow issues) and for negative values below -100000, etc. But I've clocked it to be at least 3 times faster than floor, which was really critical for our application. Take it with a grain of salt, test it on your system, etc. but it's worth considering IMHO.
I'm working on a project that doesn't use any c libraries and can't figure it out