Thread: 47.0000 = 46.9999 ?!?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    170

    Question 47.0000 = 46.9999 ?!?

    This has been this was for a while, but I don't know why it is happening. I divide 47/1 and get (double) 47.0000. This value is stored correctly as I watch through the debugger. However, when it is placed into an edit on the dialog box it is changed to 46.9999 for no reason that I can see.

    Anyone have a possible explanation for this?

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    Can you post some of the code please
    http://uk.geocities.com/ca_chorltonkids

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Kind of weird....

    I tried it out in MFC and it worked aok - 47.0000;

    Code:
    double x = 47/1;
    	CString str;
    	str.Format(_T("%.4f"),x);
    
    	GetDlgItem(IDC_EDIT1)->SetWindowText(str);
    Perhaps you had beter post some code like crag2804 said....

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I don't know what to post its not in my code.

    I traced in the debugger all the way to the DDX_Text() call and it is 47.0000 all the way to there. It is a double in the class wizard.

    I made a seperate feild identically and did the same calculation and it stays 47 there. I check the properties on the 2 and they are identical. I tried stretching the feild wider but it just added more 9s.

    It has to be something the the MFC DDX_Text() or something.
    Best Regards,

    Bonkey

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    here is where it changes. I traced it down inside MFC.

    Code:
    // Before the sprintf call.
    //value	47.000000000000
    
    _stprintf(szBuffer, _T("%.*g"), nSizeGcvt, value);
    //After the _sprintf call   
    //+	szBuffer	0x0065f1f8 "46.9999999999999"
    
    AfxSetWindowText(hWndCtrl, szBuffer);
    Best Regards,

    Bonkey

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I fixed it, but I can't really explain it. I still think it as MFC problem.

    I looked at my math again and it was doing this 1 / (1 / 47) so I changed it to 47/1. This should not make a difference, but it did.

    Thanks for your help.
    Best Regards,

    Bonkey

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Glad you got it working...I was going to suggest that if you still couldnt get the DDX to work as you wanted, you could try derive that MFC function in your code and than alter it as you needed....as long as it was declared virtual by MFC, it should work....

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by bonkey
    I fixed it, but I can't really explain it. I still think it as MFC problem.

    I looked at my math again and it was doing this 1 / (1 / 47) so I changed it to 47/1. This should not make a difference, but it did.

    Thanks for your help.
    It does make a difference. Looking at the calulation you first divide 1 by 47, and then 1 by that answer.
    1/47 is ~ 0,0212765957446808510638297872340426 so obviously it can't be stored as an exact number, so it is rounded to, say 0,021276595744680 (just a guess).
    Then when you do 1 / ANS, you won't get back the 47 since the last answer was rounded, instead you get a number close to 47.

    Hope this made some sense...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Originally posted by Magos
    It does make a difference. Looking at the calulation you first divide 1 by 47, and then 1 by that answer.
    1/47 is ~ 0,0212765957446808510638297872340426 so obviously it can't be stored as an exact number, so it is rounded to, say 0,021276595744680 (just a guess).
    Then when you do 1 / ANS, you won't get back the 47 since the last answer was rounded, instead you get a number close to 47.

    Hope this made some sense...
    It does, but by variable WAS 47. like in my code sample above. Maybe it was rounding and some extra precision was introduced during the conversion.
    Best Regards,

    Bonkey

Popular pages Recent additions subscribe to a feed