Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:51 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Perils of floating point

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10315
Posted: 02:37pm 24 Dec 2022
Copy link to clipboard 
Print this post

Came across this today which is the simplest demo I've seen to demonstrate the issues with floating point numbers

  Quote  a=100-99.9
b=10-9.9
If a=b Then
Print "same"
Else
Print "different"
EndIf


This will fail on any and all languages even using 64-bit floating point numbers
Edited 2022-12-25 00:39 by matherp
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 03:28pm 24 Dec 2022
Copy link to clipboard 
Print this post

My favourite quote:

"if you can't solve the problem with integers, you don't understand the problem"

[prepares to be flamed]  
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 04:49pm 24 Dec 2022
Copy link to clipboard 
Print this post

I was taught early on in my career and have always remembered, FP is only an approximation and "rounding" errors happen everywhere - get everything to integers as quickly as you can.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 07:30pm 24 Dec 2022
Copy link to clipboard 
Print this post

Or at least avoid

if a=b then

whenever a & b are floating point (especially if they are the results of calculations, though the example shows they need not be).

Use a test for them being nearly equal, along the lines of

if abs(a - b) < ... then


John
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:46pm 24 Dec 2022
Copy link to clipboard 
Print this post

The PureBasic forum is regularly hit with complaints/queries about floating point "issues"

A method I have used to check for single or double floats (early MMBasic is single point):
x = 1000000000: y = x + 1: IF y = x THEN ? "single" ELSE ? "double"

It tends to confuse.

Jim
VK7JH
MMedit
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 08:16pm 24 Dec 2022
Copy link to clipboard 
Print this post

When I were a lad we were taught *never* to use FP for currency calculations. They all had to be done as integers then corrected to the required number of decimal places *for display purposes only*.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 08:47pm 24 Dec 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  When I were a lad we were taught *never* to use FP for currency calculations. They all had to be done as integers then corrected to the required number of decimal places *for display purposes only*.

I think it's why some languages (*) provide BCD or similar.

(*) COBOL for one, with such as PIC 9(5) meaning up to a 5-dgit number (if I'm remembering correctly).

BCD = binary coded decimal.

John
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2642
Posted: 09:01pm 24 Dec 2022
Copy link to clipboard 
Print this post

Ok there are more views on this than people on the planet.
In the artificial worlds of pure mathematics and digital computers integers are the gold standard. For engineering and the rest of the material world integers are the approximation. Nothing is exactly this or that. FP attempts to deal with the vagueness of reality.
@JohnS said "Use a test for them being nearly equal," and that is what we need to do.
Remember the days of slide-rules and analogue computers? Three digit accuracy only and they built bridges and spaceships.

Edit
Perhaps for floats "=" should invoke a function that returns "is nearly equal to".
Edited 2022-12-25 07:07 by phil99
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 10:06pm 24 Dec 2022
Copy link to clipboard 
Print this post

it has just occurred to me that in my Amiga days of HiSoft Basic, they had the == comparitor precisely because of this.

If a==b ' means if a is roughly equal to b

can't remember what the limits of "rough" were though
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1642
Posted: 10:14pm 24 Dec 2022
Copy link to clipboard 
Print this post

Do I feel a new function coming up on FOTS?

Bill
Keep safe. Live long and prosper.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 11:20pm 24 Dec 2022
Copy link to clipboard 
Print this post

  Turbo46 said  Do I feel a new function coming up on FOTS?

Bill


'
a = 10000.000001
b = 10000
d = 10000.001
IF approx(a,b) THEN
? "Near enough for me"
ELSE
? "no way"
ENDIF

IF approx(d,b) THEN
? "Near enough for me"
ELSE
? "no way"
ENDIF

FUNCTION approx(a!, b!, c!) AS INTEGER
IF c! = 0 THEN c! = 0.00001
approx = (ABS(a!-b!) < c!)
END FUNCTION

VK7JH
MMedit
 
scruss
Regular Member

Joined: 20/09/2021
Location: Canada
Posts: 91
Posted: 02:00am 25 Dec 2022
Copy link to clipboard 
Print this post

And yet the MSX manages it with ease:



Decimal maths as an option is a required part of the ANSI Full BASIC standard. I don't think anyone's ever implemented it properly: maybe the Decimal BASIC out of Japan does.

Microsoft toyed with using decimal floating point for a while, so the Tandy 100, MSX and one of the early Mac BASICs has it. It's very slow unless you have dedicated BCD float hardware, which AFAIK only IBM Power 9 has
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 08:32am 25 Dec 2022
Copy link to clipboard 
Print this post

I still have my old Goldstar MSX (from the pre-LG days). Never got a disk for it though. It's built like a tank!
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 06:18pm 25 Dec 2022
Copy link to clipboard 
Print this post

  matherp said  Came across this today which is the simplest demo I've seen to demonstrate the issues with floating point numbers

  Quote  a=100-99.9
b=10-9.9
If a=b Then
Print "same"
Else
Print "different"
EndIf


This will fail on any and all languages even using 64-bit floating point numbers


ANSI Standard BASIC used BCD arithmetic and prints "same" for the following code. Is it supposed to print "different"? What do you mean by fail?

LET a=100-99.9
LET b=10-9.9
IF a=b THEN
  PRINT"same"
ELSE
  PRINT"different"
END IF
END

Edited 2022-12-26 04:23 by toml_12953
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:32pm 25 Dec 2022
Copy link to clipboard 
Print this post

  toml_12953 said  
ANSI Standard BASIC used BCD arithmetic and prints "same" for the following code. Is it supposed to print "different"? What do you mean by fail?

BCD arithmetic is not the same as floating point maths. Each has its place.
Peter is reminding us that floating point can result in surprises for use humans.

Jim
VK7JH
MMedit
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 10:36pm 25 Dec 2022
Copy link to clipboard 
Print this post

  TassyJim said  
  toml_12953 said  
ANSI Standard BASIC used BCD arithmetic and prints "same" for the following code. Is it supposed to print "different"? What do you mean by fail?

BCD arithmetic is not the same as floating point maths. Each has its place.
Peter is reminding us that floating point can result in surprises for use humans.

Jim


There is BCD floating point arithmetic which is what ANSI/ISO Standard BASIC uses. Perhaps he meant Binary floating point, in which case he should have specified it.

BCD Floating Point
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 03:11pm 26 Dec 2022
Copy link to clipboard 
Print this post

  toml_12953 said  ANSI Standard BASIC used BCD arithmetic

hmm... the world's least-used BASIC?

The default is BCD I gather.

Anyone after speed (if using ANSI/ISO BASIC) will use OPTION ARITHMETIC to turn off that rather odd/unexpected choice of default.

John
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 03:24pm 26 Dec 2022
Copy link to clipboard 
Print this post

  toml_12953 said  There is BCD floating point arithmetic which is what ANSI/ISO Standard BASIC uses. Perhaps he meant Binary floating point, in which case he should have specified it.

BCD Floating Point

It's unusual to say "BCD floating point". Usually it's called Decimal FP, e.g. in IEEE 754, which must by now be considered the standard.

Equally, FP by now does mean Binary FP, you just leave out the "Binary", whereas you'd add "Decimal" (or BCD if you like?) in the rather rare case that's what you mean.

John
Edited 2022-12-27 01:25 by JohnS
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025