Will the casting operation int (x) on a double x have the same effect as floor (x) (except that floor returns a double)? If this is true, then I can floor floats by recasting them instead of calling the math.h floor function.
floor in C++
Collapse
X
-
You should not be casting like this in C++.
The cast destroys the type safety of your code.
Weaknesses in C make casting a necessity but continuing to do that in C++ indicates a flawed design. -
Originally posted by arnaudkWill the casting operation int (x) on a double x have the same effect as floor (x) (except that floor returns a double)? If this is true, then I can floor floats by recasting them instead of calling the math.h floor function.
possible fractions exactly while an int can't even store the integral part of the number.
kind regards,
JosComment
-
Originally posted by weaknessforcatsYou should not be casting like this in C++.
The cast destroys the type safety of your code.
Weaknesses in C make casting a necessity but continuing to do that in C++ indicates a flawed design.
I guess the "correct" way to do it would be to use the function floor() which increases the memory footprint and probably the speed of my program. Worse, I could avoid C functions alltogether and use some .net functions which would then also make my program "safer", even slower and completely unportable.
At the moment, it seems my cast to int is simple and works. Do you know of a simple fast yet "safe" way to floor floating point numbers?Comment
-
Originally posted by arnaudkAt the moment, it seems my cast to int is simple and works. Do you know of a simple fast yet "safe" way to floor floating point numbers?
A prime example of this is money, rather than using a double to hold pounds with the pence in decimal places use a integer to hold the cash value in pence.Comment
-
This is how you do "double to long" floor, ceil, round and truncate in C++:
[CODE=cpp]
#include <cmath>
namespace math
{
inline void floor( const double& value, long& floored ){
floored = static_cast<lon g>( ::floor( value ) );
}
inline void ceil( const double& value, long& ceiling ){
ceiling = static_cast<lon g>( ::ceil( value ) );
}
inline void round( const double& value, long& rounded ){
rounded = ( value < 0 ) ? static_cast<lon g>( value - 0.5 ) :
static_cast<lon g>( value + 0.5 );
}
inline void truncate const double& value, long& truncated ){
truncated = static_cast<lon g>( value );
}
}// math
[/CODE]
Remember that you need to deal with the negative case, this is done most efficiently with the inbuilt ceil and floor functions.Comment
-
Originally posted by oler1sWhich brings up the question. Do you have the program running properly, and have you determined the floor to be the bottleneck?
Originally posted by BanfaIf you are flooring that suggests that you are using a fixed number of decimal places in which case it is possible you could just use plain old integers for your numbers, as long as the maximum limit of you calculations will fit within the constraints of the integers width.
Originally posted by fualThis is how you do "double to long" floor, ceil, round and truncate in C++:Comment
Comment