468,463 Members | 2,058 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

floor in C++

424 256MB
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.
Mar 19 '08 #1
7 12358
weaknessforcats
9,207 Expert Mod 8TB
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.
Mar 19 '08 #2
JosAH
11,448 Expert 8TB
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.
Casting fails miserably for numbers >= 2**32; a double can still store them and
possible fractions exactly while an int can't even store the integral part of the number.

kind regards,

Jos
Mar 19 '08 #3
arnaudk
424 256MB
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.
So then I would use C++'s static_cast<int>(x) . The problem with much "safe" C++ code is that it often slows the program down, not that this matters in most cases but I need to evaluate functions millions of times so I'm sensitive to this (C is hard to beat in this respect).

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?
Mar 19 '08 #4
oler1s
671 Expert 512MB
Which brings up the question. Do you have the program running properly, and have you determined the floor to be the bottleneck?
Mar 19 '08 #5
Banfa
9,051 Expert Mod 8TB
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?
If 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.

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.
Mar 20 '08 #6
fual
28
This is how you do "double to long" floor, ceil, round and truncate in C++:
Expand|Select|Wrap|Line Numbers
  1. #include <cmath>
  2. namespace math
  3. {
  4.   inline void floor( const double& value, long& floored ){
  5.     floored = static_cast<long>( ::floor( value ) );
  6.   }
  7.  
  8.   inline void ceil( const double& value, long& ceiling ){
  9.     ceiling = static_cast<long>( ::ceil( value ) );
  10.   }
  11.  
  12.   inline void round( const double& value, long& rounded ){
  13.     rounded = ( value < 0 ) ? static_cast<long>( value - 0.5 ) :
  14.                               static_cast<long>( value + 0.5 );
  15.   }
  16.  
  17.   inline void truncate const double& value, long& truncated ){
  18.     truncated = static_cast<long>( value );
  19.   }
  20. }// math
  21.  
Remember that you need to deal with the negative case, this is done most efficiently with the inbuilt ceil and floor functions.
Mar 20 '08 #7
arnaudk
424 256MB
Which brings up the question. Do you have the program running properly, and have you determined the floor to be the bottleneck?
No, I was just being pedantic. :-)

If 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.
Good point, I'll keep that in mind.

This is how you do "double to long" floor, ceil, round and truncate in C++:
Thanks. So I guess the way to floor numbers in C++ is ultimately via cmath.h functions in general.
May 15 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by SpaceCowboy | last post: by
2 posts views Thread by Murat Tasan | last post: by
4 posts views Thread by Henke | last post: by
2 posts views Thread by Francois Grieu | last post: by
reply views Thread by Gabest | last post: by
5 posts views Thread by john.leidel | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.