# floor() problems

• 06-06-2008
swgh
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; }```
• 06-06-2008
laserlight
Quote:

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.
• 06-06-2008
matsp
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
• 06-06-2008
Elysia
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.
• 06-06-2008
tabstop
Quote:

Originally Posted by matsp

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().
• 06-06-2008
matsp
Quote:

Originally Posted by tabstop
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
• 06-06-2008
swgh
Thanks guys for the tips. I appreciate it.