Why is this???

Everything else that doesn't fall into one of the other PB categories.
Gary.Maine
User
User
Posts: 34
Joined: Mon Oct 01, 2018 7:10 pm
Location: Winterport, Maine USA

Why is this???

Post by Gary.Maine »

a.f = 1.222
Debug a.f

Returns

1.22200000286102
BarryG
Addict
Addict
Posts: 3322
Joined: Thu Apr 18, 2019 8:17 am

Re: Why is this???

Post 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
Gary.Maine
User
User
Posts: 34
Joined: Mon Oct 01, 2018 7:10 pm
Location: Winterport, Maine USA

Re: Why is this???

Post 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.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Why is this???

Post by Josh »

Gary.Maine wrote: to PB from PB
:D
sorry for my bad english
infratec
Always Here
Always Here
Posts: 6871
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Why is this???

Post by infratec »

Naybe from PowerBasic to PureBasic

But a float is a float.
In PB and in PB :mrgreen:
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Why is this???

Post by blueb »

- 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
Gary.Maine
User
User
Posts: 34
Joined: Mon Oct 01, 2018 7:10 pm
Location: Winterport, Maine USA

Re: Why is this???

Post by Gary.Maine »

Yes, from PowerBasic to PureBasic! Thanks for the help.... :D
Gary.Maine
User
User
Posts: 34
Joined: Mon Oct 01, 2018 7:10 pm
Location: Winterport, Maine USA

Re: Why is this???

Post 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??
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Why is this???

Post by skywalk »

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
Gary.Maine
User
User
Posts: 34
Joined: Mon Oct 01, 2018 7:10 pm
Location: Winterport, Maine USA

Re: Why is this???

Post 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
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Why is this???

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Gary.Maine
User
User
Posts: 34
Joined: Mon Oct 01, 2018 7:10 pm
Location: Winterport, Maine USA

Re: Why is this???

Post by Gary.Maine »

Sorry,
Can you please explain?

Why is this example different under Windows than under Linux?
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Why is this???

Post by skywalk »

What is the goal of your code?
Why focus on the format of a debug window?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Gary.Maine
User
User
Posts: 34
Joined: Mon Oct 01, 2018 7:10 pm
Location: Winterport, Maine USA

Re: Why is this???

Post 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
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Why is this???

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply