Thread: floor() problems

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    floor() problems

    I am doing an exercise and I have to make a program that uses the funtion floor() to round a decimal down to the nearest integer.

    I have never used floor() before but I am aware it is in the <cmath> header. After doing a bit of online searching on it I discovered it takes and returns and double value.

    This is what I have done but I am getting an identical output each time and the function is not working. If I attempt to change anything to an int I get a warning about "possible loss of data".

    Can anyone offer my any advice?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    // function prototype
    double roundToInteger ( double );
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
        std::cout << std::setprecision( 2 ) << std::fixed;
    
       for ( double i = 1.0; i <= 10.0; i++ )
       {
          std::cout << i << " rounded is: " << roundToInteger ( i )
    	     << std::endl;
       }
    
        std::cin.get(); // freeze console output window
       
        return 0; // return value from int main
    }
    
    // function that rounds and returns the passed
    // value to the nearest integer
    double roundToInteger ( double num )
    {
    	double x = 0;
    
    	x = floor ( num );
    
    	return x;
    }
    Double Helix STL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am getting an identical output each time
    That's because you are providing input for which floor() has no net effect. The idea is to provide input that are in between integral values, and then see the effect that floor() would have.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "not working"?

    Mine lists:
    Code:
    1.00 rounded is: 1.00
    2.00 rounded is: 2.00
    3.00 rounded is: 3.00
    4.00 rounded is: 4.00
    5.00 rounded is: 5.00
    6.00 rounded is: 6.00
    7.00 rounded is: 7.00
    8.00 rounded is: 8.00
    9.00 rounded is: 9.00
    10.00 rounded is: 10.00
    which is what I expect the function to give.

    Floor will return the integral value (with the fraction chopped off -> chopping towards zero). Since your double values are already integers, you can't see the difference.

    If you where to add for example 0.7 on each iteration, the two columns would show different values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe swgh is confused that i++ adds 0.1 to the value or something each iteration, which it doesn't. It adds 1, so the loop is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, which have no decimals to chop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matsp View Post

    Floor will return the integral value (with the fraction chopped off -> chopping towards zero). Since your double values are already integers, you can't see the difference.

    If you where to add for example 0.7 on each iteration, the two columns would show different values.

    --
    Mats
    Eh -- for positive numbers, that's true, but not for negatives. Floor always goes down, so floor(-2.5) is -3. To chop toward zero, use trunc().

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Eh -- for positive numbers, that's true, but not for negatives. Floor always goes down, so floor(-2.5) is -3. To chop toward zero, use trunc().
    Ok, thanks for the correction. I tend to use floor/trunc very rarely (I think about once or twice in my life), so I must confess I'm a bit rusty on how they work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks guys for the tips. I appreciate it.
    Double Helix STL

Popular pages Recent additions subscribe to a feed