# Strange !!!

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-04-2004
Moni
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; }```
Quote:

9.005
Round of 9.005 is : 9.01
• 01-04-2004
gustavosserra
Yes, it is ok? Didnīt understand what happens?
• 01-04-2004
Moni
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?

Quote:

4
15.01
15.00
3.00
3.01
And the output is:

Quote:

4
15.01
15.00
3.00
3.01
AVG: 9.005
AVG: 9
\$12.01
• 01-04-2004
gustavosserra
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.
• 01-04-2004
Moni
Hmm...you can change pow10(2) by pow(10.00,2)

And about that floor:

You may see this :rolleyes:

Then what might be the answer :confused:
• 01-04-2004
gustavosserra
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"...
• 01-04-2004
gustavosserra
Sorry, I did not see your post.
• 01-04-2004
Moni
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 ???
• 01-04-2004
gustavosserra
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 ".").
• 01-04-2004
Moni
Hmm...but the previous (my first post) code is behaving normally...isn't it :rolleyes:
• 01-04-2004
gustavosserra
Ok, I give up! I donīt know... this is too bizarre!
• 01-04-2004
Salem
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.
• 01-04-2004
gustavosserra
And what could be the solution? I mean... static_cast<int> didnīt work too...
• 01-04-2004
Moni
May be:
Code:

```cout.setf(ios::fixed); cout.precision(2);```
This is causing the ERROR :(
• 01-04-2004
gustavosserra
Are you sure? The error happens before these statements :-(
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last