Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Float math producing messed up results?

Author
Message
Elspin
16
Years of Service
User Offline
Joined: 30th Mar 2008
Location:
Posted: 20th Aug 2008 04:10
I noticed I was getting odd results in my math on floats, so I ran a test with this code:

dbWriteToClipboard( dbStr( 36.0f - 0.2f ) );

and got this as a result:
"35.7999992371"

This is only happening in my darkGDK-built projects, nowhere else. Is this something darkGDK does?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 20th Aug 2008 04:45
Float(or floating point) precision, search the forums to find a trillion threads talking about this. This is an issue with ALL IEEE 754-1985 floats when being displayed as strings, no matter the language, you could limit the amount of decimals or add some tolerance to the output to mask the effect.

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 20th Aug 2008 04:48
Quote: "This is only happening in my darkGDK-built projects, nowhere else. Is this something darkGDK does? "


Actually, that's something that floating-point numbers do all the time (especially single-precision). The only reason that it might act differently elsewhere would be VC++ implicitly converting the single-precision values to double-precision to keep them accurate.

Take a look at these pages to see why they behave in that way.

http://en.wikipedia.org/wiki/Single_precision

http://en.wikipedia.org/wiki/Double_precision

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
Elspin
16
Years of Service
User Offline
Joined: 30th Mar 2008
Location:
Posted: 21st Aug 2008 00:02
So basically what I should get out of this is:

The float math is actually correct, the float->string conversion is what's actually messed up, and I should convert to a double before printing if I want to see it how it actually is?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 21st Aug 2008 00:44
No, it's probably a little of both. The biggest thing to remember about floats is that they are not exact - they give you a representation of the nearest binary match to the number you actually need. For example, in the same way that one-third can't be represented as a decimal, binary can't represent 0.1 exactly.

Certain C and C++ library functions have been designed to fiddle with the numbers you give them to make them more palatable:


The 7 represents the total number of characters in the number (including the decimal point) and the 4 represents the number of digits after the decimal point.

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 21st Aug 2008 06:09
Quote: "For example, in the same way that one-third can't be represented as a decimal, binary can't represent 0.1 exactly."


In my experience, double-precision can represent 0.1, but not single precision. Either way, doubles are slower if used intensively, but are far more accurate.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 21st Aug 2008 15:19
No, it just appears to when outputting the value using standard C library routines.

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 21st Aug 2008 17:43
Quote: "No, it just appears to when outputting the value using standard C library routines."


Oh.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 21st Aug 2008 19:25
It's not as bad as it sounds - every X86 & compatible with an embedded FP unit uses this system, and the world hasn't ended yet.

Just bear in mind that 4-byte floats provide 7 significant digits of accuracy (eg 1.234567 or 1234.567) and 8-byte double floats provide 15 significant digits (eg 1.23456789012345 or 12345678.9012345).

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 21st Aug 2008 20:44
Quote: "It's not as bad as it sounds - every X86 & compatible with an embedded FP unit uses this system, and the world hasn't ended yet."


I know. It still sucks to work with, at times.

I recently found this, though. Haven't had time to try it out, but it looks promising.

http://nicolas.brodu.free.fr/en/programmation/streflop/index.html

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo

Login to post a reply

Server time is: 2024-09-30 05:40:52
Your offset time is: 2024-09-30 05:40:52