floor in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    floor in C++

    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by arnaudk
      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

      Comment

      • arnaudk
        Contributor
        • Sep 2007
        • 425

        #4
        Originally posted by weaknessforcats
        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?

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          Which brings up the question. Do you have the program running properly, and have you determined the floor to be the bottleneck?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by arnaudk
            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.

            Comment

            • fual
              New Member
              • Feb 2008
              • 28

              #7
              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

              • arnaudk
                Contributor
                • Sep 2007
                • 425

                #8
                Originally posted by oler1s
                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. :-)

                Originally posted by Banfa
                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.

                Originally posted by fual
                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.

                Comment

                Working...