472,802 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,802 software developers and data experts.

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 12740
weaknessforcats
9,208 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,065 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

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
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.