Strange !!!

This is a discussion on Strange !!! within the C++ Programming forums, part of the General Programming Boards category; It is not a good solution, but you can add a small fraction to the number that will be rounded. ...

  1. #16
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    It is not a good solution, but you can add a small fraction to the number that will be rounded. Then, the floor function will return that number and ignore the small fraction.
    Code:
    inline double round(double X, int k)
    {
       double tempDouble = X*pow(10.0,k) + 0.5 + 0.000000001;
       return ( floor(tempDouble) / pow(10.0,k) );
    }
    Nothing more to tell about me...
    Happy day =)

  2. #17
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    Hmm...but why? this strange things happenning???
    I am really looking for the answer.............
    I've manged to get another solution of the problem...so now only I need to just WHY ???
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  3. #18
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Salem already gave you guys the answer... it's the innaccuracy of a float... decimal math is very innacurate (in my experience to the tenth, or +/- 0.01)... if you keep running it, eventually you'll probably end up with the right answer... either way, it'll be hard to ensure the right answer every single time.

    I modded your code a bit and came out with this output:
    Code:
    4
    15.01
    15
    3
    3.01
     AVG: 9.005
     AVG: 9.00488
    $12.00
    I used the same input as you... here's the code I used:
    Code:
    #include<iostream>
    #include<cmath>
    
    using namespace std;
    
    inline double round(double X, int intk)
    {
    	double k=static_cast<double>(intk);
    	return ( floor( X*pow(k,10.0) + 0.5) / pow(k,10) );
    }
    
    
    
    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;
    }
    ------------------------------------------------------------------------------------
    EDIT 2:
    ------------------------------------------------------------------------------------
    I rewrote your code to actually do some real rounding... the only thing is that it doesn't clear out the number it acutually rounded... my brain is fried and I've been working at it for 20 minutes, so I'm giving up on that part... here's what I have:
    Code:
    inline double round(double X, int k)
    {
    	double decimal=X-static_cast<int>(X);	//take out whole numbers
    	double half=(5.0/(pow(10.0,k)));	//half of the requested decimal place
    	double decimaltemp;
    	
    	//cout<<"K: "<<k<<endl<<"DECIMAL: "<<decimal<<endl<<"HALF: "<<half<<endl;
    	
    	if(decimal>=half)	//if it's bigger than that
      	{
      		//cout<<"ROUND UP"<<endl;
      		decimal+=1.0/(pow(10.0,k-1));	//add one to next biggest decimal
      		//cout<<"DECIMAL: "<<decimal<<endl;
      		
      		/*
    
                                   NOTE: do something here to get rid of the decimal that was just rounded.
                                   you have to find out it's value and delete it from itself, leaving a zero in it's place.                
                   
    		decimaltemp=static_cast<int>(decimal*(pow(10.0,k)));
    		cout<<"DECMIALTEMP: "<<decimaltemp<<endl;
    		decimaltemp-=decimaltemp/pow(10.0,k);
    		cout<<"DECIMALTEMP: "<<decimaltemp<<endl;
    		decimal-=decimaltemp;
    		cout<<"DECIMAL: "<<decimal<<endl;
    		*/
    		
       	return (static_cast<int>(X)+decimal);	//return the next decimal + 1
      	}
     	else	//if it's smaller
      	{
      		//cout<<"ROUND DOWN"<<endl;
      		//decimal=zeroout(decimal,k);	//kill rounded to zero
       	return (static_cast<int>(X)+decimal);//return the next decimal
     	}
    }
    all the couts that I commented out are for debugging... you can just ignore those. this code gives me this output:
    Code:
    4
    15
    15.1
    3
    3.1
     AVG: 9.05
     AVG: 9.15
    $11.80
    4
    15
    15.1
    3
    3
     AVG: 9.025
     AVG: 9.025
    $12.05
    I'll leave it up to you to find out how to get rid of the .05 in there.
    Last edited by major_small; 01-05-2004 at 12:19 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Yeah, what major_small just said

    Seriously, you need a good understanding of what all the constants in float.h mean, and back that up with reading at least some of the document I referenced in my previous reply.

    Any serious attempt at using floats should not be attempted without it IMO
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #20
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    For those with an interest in such things, this is an example of what can happen if you don't appreciate how computers store numbers:

    http://www.ima.umn.edu/~arnold/disasters/patriot.html

Page 2 of 2 FirstFirst 12
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, 09:32 AM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 11:21 AM
  3. Strange response from MSVC
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 04-17-2004, 08:40 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 02:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM

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