Page 2 of 2
Re: Why is this???
Posted: Mon Jan 06, 2020 8:29 am
by Michael Vogel
I think Skywalk gave already a lot of important information, just one small note - certain floating point numbers can't be exact when represented in binary because it's "incomaptible" with the binary system.
In general there are (only) two possibilities dealing with that:
1. beware of the problems (see the example below how to reduce side effects)
2. use integers only for calculations (112345992 in your example instead of 112345.992)
Code: Select all
a.d=112345.992
b.d=112345.991
; a.d=0.1 : b.d=0.3
Debug a-b
Debug (a-b)*1000
Debug a*1000-b*1000
Re: Why is this???
Posted: Mon Jan 06, 2020 2:12 pm
by Tenaja
Fixed point floats might be another alternative. It may take more than a cursory search to find one, or find one in C and convert it.
Here's another thread on your same topic.
viewtopic.php?f=13&t=68508&p=507226
Re: Why is this???
Posted: Tue Jan 07, 2020 12:09 am
by Gary.Maine
skywalk,
Thanks, I understand the difference between Double and Float. I guess what is bothering me is the why I need to format the varable in Linux but not Windows. I guess the easiest thing to do is to always format before any calcuations.
BTW I think it is to cold up here in Maine for Trolls!
Regards,
Gary
Re: Why is this???
Posted: Tue Jan 07, 2020 3:12 am
by skywalk
Nooooooo!
Your calculations proceed until you decide to display or report to a database or some other recipient.
You can calculate the voltage required to turn on a switch, then send the number to a power supply.
You can read the temperature off a remote sensor and send a response to an air duct or a fan.
Why would these require a certain number of displayed decimals?
Maybe explain the goal of your PowerBasic code?
Re: Why is this???
Posted: Tue Jan 07, 2020 3:29 am
by Gary.Maine
Sorry, I am not making myself clear....
If I run this code under Linux:
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
I get this output:
112345.9919999999983701854944229
112,345.992
112,344.992
112344.9919999999983701854944229
If I run the same code Under Windows 10 I get this output:
112345.992
112,345.992
112,344.992
112344.992
I need to make sure both versions are the same. I have yet to test OSX.
The issue here is not so much Float vs Double, but Lunux vs Windows.
I hope this helps?
Thanks again,
Gary
Re: Why is this???
Posted: Tue Jan 07, 2020 6:18 am
by Paul
Gary.Maine wrote:
I need to make sure both versions are the same. I have yet to test OSX.
Both Linux and OSX return the same results.
Even if you change your number slightly...
Code: Select all
A.d =112345.994 ;<-- changed slightly for 2nd test
Debug A.d
Debug FormatNumber(A.d, 3)
B.d = A.d - 1
Debug FormatNumber(B.d, 3)
Debug B.d
Windows returns
112345.99400000001
112,345.994
112,344.994
112344.99400000001
and Linux and OSX return
112345.9940000000060535967350006
112,345.994
112,344.994
112344.9940000000060535967350006
One thing to note, both Linux and OSX are 64bit and return a much longer number.
Running the code on both Windows x86 and x64 return a shorter number when I would have expected Windows x64 to return the same as Linux and OSX.
I can only speculate that the numbers are slightly different because both Windows x64 and x86 compilers are handling the number as 32bit
Of course after you format the result for what you want the user to see, the results end up being all the same anyway.
Re: Why is this???
Posted: Tue Jan 07, 2020 7:10 am
by wilbert
Gary.Maine wrote:The issue here is not so much Float vs Double, but Linux vs Windows.
PureBasic relies on the OS to format a number (a C function of the
printf family).
The Windows implementation is probably slightly different from macOS / Linux.
Re: Why is this???
Posted: Tue Jan 07, 2020 8:29 am
by Michael Vogel
As shown in my example above,
no OS will be able to handle (the decimal number) 0.1 exactly in binary...
...
here's is more information why.
Re: Why is this???
Posted: Tue Jan 07, 2020 9:35 pm
by Gary.Maine
Michael Vogel wrote:As shown in my example above,
no OS will be able to handle (the decimal number) 0.1 exactly in binary...
...
here's is more information why.
Thanks
Re: Why is this???
Posted: Tue Jan 07, 2020 9:37 pm
by Gary.Maine
wilbert wrote:Gary.Maine wrote:The issue here is not so much Float vs Double, but Linux vs Windows.
PureBasic relies on the OS to format a number (a C function of the
printf family).
The Windows implementation is probably slightly different from macOS / Linux.
So I have found!
Thanks