floor() problems

This is a discussion on floor() problems within the C++ Programming forums, part of the General Programming Boards category; I am doing an exercise and I have to make a program that uses the funtion floor() to round a ...

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

    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;
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    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,185
    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,475
    Thanks guys for the tips. I appreciate it.
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21