Page 1 of 2
Why is this???
Posted: Sat Dec 14, 2019 2:38 am
by Gary.Maine
a.f = 1.222
Debug a.f
Returns
1.22200000286102
Re: Why is this???
Posted: Sat Dec 14, 2019 2:52 am
by BarryG
Floats are no good for accuracy. See the very bottom of here for why:
https://www.purebasic.com/documentation ... ables.html
This is not a PureBasic problem; it exists for
any programming language.
To solve your problem, use doubles instead:
Code: Select all
a.d = 1.222
Debug a.d ; Shows 1.222
Re: Why is this???
Posted: Sat Dec 14, 2019 3:02 am
by Gary.Maine
Thanks,
I am in the process of converting some apps to PB from PB! It has been more work than i expected.
Re: Why is this???
Posted: Sat Dec 14, 2019 9:46 am
by Josh
Gary.Maine wrote: to PB from PB

Re: Why is this???
Posted: Sat Dec 14, 2019 12:49 pm
by infratec
Naybe from PowerBasic to PureBasic
But a float is a float.
In PB and in PB

Re: Why is this???
Posted: Sat Dec 14, 2019 12:59 pm
by blueb
Re: Why is this???
Posted: Tue Dec 17, 2019 2:18 am
by Gary.Maine
Yes, from PowerBasic to PureBasic! Thanks for the help....

Re: Why is this???
Posted: Mon Jan 06, 2020 2:58 am
by Gary.Maine
Thank you for the replies, But still not sure why this code...
Code: Select all
A.d =112345.992
Debug A.d
Debug FormatNumber(A.d, 3)
B.d = A.d - 1
Debug FormatNumber(B.d, 3)
Debug B.d
Returns
112345.9919999999983701854944229
112,345.992
112,344.992
112344.9919999999983701854944229
What am I missing??
Re: Why is this???
Posted: Mon Jan 06, 2020 3:13 am
by skywalk
I get this for v571 x64 on Windows 10.
Code: Select all
112345.992
112,345.992
112,344.992
112344.992
Re: Why is this???
Posted: Mon Jan 06, 2020 3:23 am
by Gary.Maine
I am running PureBasic 5.71 LTS (Linux - x64) on Ubuntun 18.04. I have not tried this under Windows. Could this be a Linux Bug?
Thanks, Gary
Re: Why is this???
Posted: Mon Jan 06, 2020 3:28 am
by skywalk
No, it's not a bug to print floating point numbers with arbitrary decimal places.
You have to code what you want to display.
Re: Why is this???
Posted: Mon Jan 06, 2020 3:47 am
by Gary.Maine
Sorry,
Can you please explain?
Why is this example different under Windows than under Linux?
Re: Why is this???
Posted: Mon Jan 06, 2020 3:57 am
by skywalk
What is the goal of your code?
Why focus on the format of a debug window?
Re: Why is this???
Posted: Mon Jan 06, 2020 4:26 am
by Gary.Maine
My goal is to convert some of my old PowerBasic code. And I am working on some new STUFF! I just want to understand this issue. Do I always need to round up / down a number? I just want to understand why a var "112,345.992" is set to "112345.9919999999983701854944229" ?
Any insight would be welcome?
Thanks, Gary
Re: Why is this???
Posted: Mon Jan 06, 2020 6:03 am
by skywalk
Ok, I am assuming you are a real person and not a troll.
You have to read up on floating point storage as was mentioned above.
https://www.purebasic.com/documentation/reference/variables.html wrote:Special information about Floats and Doubles
A floating-point number is stored in a way that makes the binary point "float" around the number, so that it is possible to store very large numbers or very small numbers. However, you cannot store very large numbers with very high accuracy (big and small numbers at the same time, so to speak).
Another limitation of floating-point numbers is that they still work in binary, so they can only store numbers exactly which can be made up of multiples and divisions of 2. This is especially important to realize when you try to print a floating-point number in a human readable form (or when performing operations on that float) - storing numbers like 0.5 or 0.125 is easy because they are divisions of 2. Storing numbers such as 0.11 are more difficult and may be stored as a number such as 0.10999999. You can try to display to only a limited range of digits, but do not be surprised if the number displays different from what you would expect!
This applies to floating-point numbers in general, not just those in PureBasic.
Like the name says the doubles have double-precision (64-bit) compared to the single-precision of the floats (32-bit). So if you need more accurate results with floating-point numbers use doubles instead of floats.
The exact range of values, which can be used with floats and doubles to get correct results from arithmetic operations, looks as follows:
Float: +- 1.175494e-38 till +- 3.402823e+38
Double: +- 2.2250738585072013e-308 till +- 1.7976931348623157e+308
More information about the 'IEEE 754' standard you can get on Wikipedia.
If you are worried about inaccuracies in your math, then scale your number system to a large integer value and return to original scale when calculations are done.