Why is this???

Everything else that doesn't fall into one of the other PB categories.
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Why is this???

Post 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
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Why is this???

Post 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
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 »

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

Re: Why is this???

Post 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?
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, 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
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1251
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Why is this???

Post 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.
Image Image
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Why is this???

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Why is this???

Post 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.
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 »

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
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 »

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
Post Reply