Thread: Strange !!!

  1. #1
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104

    Talking 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
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Yes, it is ok? Didnīt understand what happens?
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    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
    Last edited by Moni; 01-04-2004 at 10:57 AM.
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    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.
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    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
    Last edited by Moni; 01-04-2004 at 11:15 AM.
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    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"...
    Nothing more to tell about me...
    Happy day =)

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Sorry, I did not see your post.
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    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 ???
    Last edited by Moni; 01-04-2004 at 11:28 AM.
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  9. #9
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    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 ".").
    Nothing more to tell about me...
    Happy day =)

  10. #10
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Hmm...but the previous (my first post) code is behaving normally...isn't it
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  11. #11
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Ok, I give up! I donīt know... this is too bizarre!
    Nothing more to tell about me...
    Happy day =)

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    And what could be the solution? I mean... static_cast<int> didnīt work too...
    Nothing more to tell about me...
    Happy day =)

  14. #14
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    May be:
    Code:
    cout.setf(ios::fixed);
    cout.precision(2);
    This is causing the ERROR
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  15. #15
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Are you sure? The error happens before these statements :-(
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  3. Strange response from MSVC
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 04-17-2004, 07:40 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM