1. Strange !!!

Do you think the output of this code alright???

Code:
```#include<iostream.h>
#include<math.h>

inline double round(double X, int k)
{
return ( floor( X*pow10(k) + 0.5) / pow10(k) );
}

int main()
{
double x;

while(cin >> x)
cout << " Round of " << x << " is :   " << round(x,2) << endl;

return 0;
}```
9.005
Round of 9.005 is : 9.01

2. Yes, it is ok? Didnīt understand what happens?

3. Hmm...if it is OK!
Then can you tell me...why this same function fails in this section???

Which one is correct then? I want the first one (above) but why this is behaving strangley in this ???

Code:
```#include<iostream.h>
#include<math.h>

inline double round(double X, int k)
{
return ( floor( X*pow10(k) + 0.5) / pow10(k) );
}

int main()
{
int n = 0;

while(cin >> n)
{
if(n == 0)
return 0;

double trip[1000] = {0};
double sum = 0,ans = 0;

for(int i=0;i<n;++i)
{
cin >> trip[i];
sum += trip[i];
}

double avg = sum/double(n);
cout << " AVG: " << avg << endl;
avg = round(avg,2);

cout << " AVG: " << avg << endl;

for(int i=0;i<n;++i)
if(trip[i] > avg)
ans += trip[i]-avg;

cout.setf(ios::fixed);
cout.precision(2);

cout << '\$' << ans << endl;
}

return 0;
}```
Here is input?

4
15.01
15.00
3.00
3.01
And the output is:

4
15.01
15.00
3.00
3.01
AVG: 9.005
AVG: 9
\$12.01

4. If I am not mistaken, floor returns an int. This function pow10 is not supported by my compiler, but if just gives 10 power, then you have two integers. So, you have integer division.

5. Hmm...you can change pow10(2) by pow(10.00,2)

And about that floor:

You may see this

Then what might be the answer

6. Sorry! My answer was wrong!
I read this defition:
The function floor() returns the largest integer not greater than arg. For example,

y = 6.04;
x = floor( y );

would result in x being set to 6.0.

But in place of that "not greater" could be "nor greater nor equal"...

7. Sorry, I did not see your post.

8. Hmm...I also know about that floor and ceil but when I went in programming field I found this! (the link above!) But if you now compile, can you explain about its strange behaviour ???

9. In your example the floor function was receiving the number 901. Well, the next integer not greater and not equal to 901 is 900.
I am trying to think a solution. I tryied to cast the number to an integer, but didnīt worked and this is reaaaally a strange behavior, look:
Code:
```double round(double X, int k)
{
return ( static_cast<int>(X*pow(10.0,k) + 0.5) / pow(10.0,k) );
}```
The cast is acting like the floor function, when it should only "remove" the float point numbers (I do not konw how to say in english the numbers after the ".").

10. Hmm...but the previous (my first post) code is behaving normally...isn't it

11. Ok, I give up! I donīt know... this is too bizarre!

12. http://docs-pdf.sun.com/800-7895/800-7895.pdf
A rounded float is still an approximation, so 12.34 may still be 12.399999 no matter what you do.

13. And what could be the solution? I mean... static_cast<int> didnīt work too...

14. May be:
Code:
```cout.setf(ios::fixed);
cout.precision(2);```
This is causing the ERROR

15. Are you sure? The error happens before these statements :-(

Popular pages Recent additions