473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

floor in C++

424 Contributor
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 12773
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 Recognized Expert MVP
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 Contributor
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 Recognized Expert Contributor
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,065 Recognized Expert Moderator Expert
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 New Member
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 Contributor
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

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

Similar topics

5
by: SpaceCowboy | last post by:
I'm using Sun ONE Studio 4 update 1, Community Edition with Nokia Developer's Suite integration. I'm developing a J2ME project with MIDP. I wanted to use the java.math.floor() function, but...
2
by: Murat Tasan | last post by:
here is the situation... i have an array... and i select something from random from it. i pick a random number x by using Math.random() and multiplying it by the length of the array. but this...
4
by: Henke | last post by:
I wanna round an value in a javascript Down to nearest hundred. For example: 3234 = 3200 and 3890 = 3800 Always down and to hundred. I have tried Math.floor but that just round down to ten. Is...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
2
by: Francois Grieu | last post by:
In ISO/IEC 9899:1999, it is unambiguous that floor(-0.5) has the value -1 (converted to double). Was this the case in earlier C standards? Is anyone aware of an implementaton that has it wrong...
0
by: Gabest | last post by:
I was doing a rasterizer when noticed how slow ceil was. What I found was rather interesting, see my report for more detail: ...
4
by: lilmax88 | last post by:
I have a program in which I need to take a numeric value for dollars. There is a "set" function that must screen the value for the following 3 conditions with the indicated handling functionality:...
5
by: john.leidel | last post by:
In doing some work on a library I'm writing, I'm trying to use the `floor()` function to get the largest integral value of a variable. I'm compiling everything using the mpich mpicc built with gcc...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.