Why is this???
-
- User
- Posts: 34
- Joined: Mon Oct 01, 2018 7:10 pm
- Location: Winterport, Maine USA
Why is this???
a.f = 1.222
Debug a.f
Returns
1.22200000286102
Debug a.f
Returns
1.22200000286102
Re: Why is this???
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:
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
-
- User
- Posts: 34
- Joined: Mon Oct 01, 2018 7:10 pm
- Location: Winterport, Maine USA
Re: Why is this???
Thanks,
I am in the process of converting some apps to PB from PB! It has been more work than i expected.
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???
Naybe from PowerBasic to PureBasic
But a float is a float.
In PB and in PB
But a float is a float.
In PB and in PB
Re: Why is this???
This might help...
viewtopic.php?p=457319#p457319
viewtopic.php?p=457319#p457319
- It was too lonely at the top.
System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
-
- User
- Posts: 34
- Joined: Mon Oct 01, 2018 7:10 pm
- Location: Winterport, Maine USA
Re: Why is this???
Yes, from PowerBasic to PureBasic! Thanks for the help....
-
- User
- Posts: 34
- Joined: Mon Oct 01, 2018 7:10 pm
- Location: Winterport, Maine USA
Re: Why is this???
Thank you for the replies, But still not sure why this code...
Returns
112345.9919999999983701854944229
112,345.992
112,344.992
112344.9919999999983701854944229
What am I missing??
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
112345.9919999999983701854944229
112,345.992
112,344.992
112344.9919999999983701854944229
What am I missing??
Re: Why is this???
I get this for v571 x64 on Windows 10.
Code: Select all
112345.992
112,345.992
112,344.992
112344.992
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- User
- Posts: 34
- Joined: Mon Oct 01, 2018 7:10 pm
- Location: Winterport, Maine USA
Re: Why is this???
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
Thanks, Gary
Re: Why is this???
No, it's not a bug to print floating point numbers with arbitrary decimal places.
You have to code what you want to display.
You have to code what you want to display.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- User
- Posts: 34
- Joined: Mon Oct 01, 2018 7:10 pm
- Location: Winterport, Maine USA
Re: Why is this???
Sorry,
Can you please explain?
Why is this example different under Windows than under Linux?
Can you please explain?
Why is this example different under Windows than under Linux?
Re: Why is this???
What is the goal of your code?
Why focus on the format of a debug window?
Why focus on the format of a debug window?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- User
- Posts: 34
- Joined: Mon Oct 01, 2018 7:10 pm
- Location: Winterport, Maine USA
Re: Why is this???
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
Any insight would be welcome?
Thanks, Gary
Re: Why is this???
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.
You have to read up on floating point storage as was mentioned above.
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.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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum