![]() |
Forum Index : Microcontroller and PC projects : Perils of floating point
Author | Message | ||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10315 |
Came across this today which is the simplest demo I've seen to demonstrate the issues with floating point numbers 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 KingdomPosts: 1646 |
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 KingdomPosts: 2170 |
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 KingdomPosts: 4044 |
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: AustraliaPosts: 6283 |
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 KingdomPosts: 7937 |
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 KingdomPosts: 4044 |
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: AustraliaPosts: 2642 |
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 KingdomPosts: 2170 |
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: AustraliaPosts: 1642 |
Do I feel a new function coming up on FOTS? Bill Keep safe. Live long and prosper. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
' 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: CanadaPosts: 91 |
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 KingdomPosts: 7937 |
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 StatesPosts: 442 |
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: AustraliaPosts: 6283 |
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 StatesPosts: 442 |
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 KingdomPosts: 4044 |
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 KingdomPosts: 4044 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |