Page 1 of 1

big probleme between str and val

Posted: Wed Jun 15, 2016 1:35 pm
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 ?

Re: big probleme between str and val

Posted: Wed Jun 15, 2016 1:44 pm
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

Re: big probleme between str and val

Posted: Wed Jun 15, 2016 2:53 pm
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/

Re: big probleme between str and val

Posted: Wed Jun 15, 2016 3:17 pm
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