big probleme between str and val

Just starting out? Need help? Post your questions and find answers here.
supercdfr
User
User
Posts: 54
Joined: Tue Mar 16, 2010 9:28 pm

big probleme between str and val

Post by supercdfr »

here is a little code :

Code: Select all

retour$ = "25.8"
retour_1$ = "3"
Debug ValF( retour$ )
Debug ValD( retour$ )

Debug ValF( retour$ ) / ValF( retour_1$ )
Debug ValD( retour$ ) / ValD( retour_1$ )
When i execute, i have to obtain has result 8.60
Here, i have 8.5999999999999996

how to make it working correctly ?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: big probleme between str and val

Post by IdeasVacuum »

You have to convert the numerical values back to a string:

Code: Select all

retour$ = "25.8"
retour_1$ = "3"
Debug StrD(ValD(retour$), 2)
Debug StrD(ValD(retour$) / ValD(retour_1$), 2)
If you are working with money values, you need money value functions: http://www.purebasic.fr/english/viewtopic.php?t=32094
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: big probleme between str and val

Post by DarkDragon »

supercdfr wrote:how to make it working correctly ?
By not using floating point arithmetic.

Basically this is because you can't represent 8.60 in binary floating point numbers. Try it yourself: find a limited series which consists of summands of the format 2^x with x a integer number. So 2^3 + 2^(-1) + ... you won't find it.

I googled a bit and I think this guide will be helpful:

http://floating-point-gui.de/
bye,
Daniel
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: big probleme between str and val

Post by sys64802 »

That's nice !
Thanks for the link.

Also in this thread there is a nice PB program to play with the float binary representation.
http://www.purebasic.fr/english/viewtop ... 13&t=64724
Post Reply