Cast to int is a function to convert variable of any datatype to integer type, on the other hand Math. floor function will only floor the decimal number to integer not converting datatype. But result will be different in case of negative values because Cast to Int approaches to zero and Math.
Since a float is bigger than int, you can convert a float to an int by simply down-casting it e.g. (int) 4.0f will give you integer 4. By the way, you must remember that typecasting just get rid of anything after the decimal point, they don't perform any rounding or flooring operation on the value.
In case of casting a float / double value to int , you generally loose the fractional part due to integer truncation.
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.
SO 101, do not change your question after people have replied to your question, instead write a new question.
Why do you think they will have the same result?
float foo = (int)(bar / 3.0) //will create an integer then assign it to a float
float foo = fabs(bar / 3.0 ) //will do the absolute value of a float division
bar = 1.0
foo1 = 0;
foo2 = 0.33333...
EDIT: Because the question may have been modified due to confusion between fabs()
and floor()
.
Given the original question example lines:
1. float foo = (int)(bar / 3.0);
2. float foo = fabs(bar / 3.0);
The difference is that if bar is negative the result will be negative with the first but positive with the second. The first will be truncated to an integer and the second will return the full decimal value including fractional part.
Yes. fabs
returns the absolute value of its argument, and the cast to int causes truncation of the division (down to the nearest int), so the results will almost always be different.
There are two main differences:
As others have pointed out, casting to an integer will truncate towards zero, whereas floor()
will always truncate towards negative infinity; this is different behaviour for a negative operand.
No one (yet) seems to have pointed out another difference - if your argument is greater than or equal to MAX_INT+1
(or less than -MAX_INT-1
) then casting to an int
will result in the top-most bits being dropped (C, probably) or undefined behaviour (C++ and possibly C). EG if your int
is 32 bits, you will only have a sign bit plus 31 bits of data. So using this with a double
that is large in size is going to produce unintended results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With