Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:43 29 Nov 2024 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 : PicoMite: Floating Point Quirk?

Author Message
DrifterNL

Regular Member

Joined: 27/09/2018
Location: Netherlands
Posts: 58
Posted: 09:54am 21 Apr 2023
Copy link to clipboard 
Print this post

Hi all,
I've been trying to wrap my head around this.
Is this one of those floating point quirks, is my thinking wrong or am I getting something mixed up?:
* PicoMite Version 5.0706


Dim a = 100
Dim b = 0
Dim c = 0.1

start:
Print b
If b = a Then GoTo equ
If b > a Then GoTo gre
If b < a Then Inc b,c
GoTo start

equ:
Print "equal"
Print "a= " a
Print "b= " b
End

gre:
Print "greater than"
Print "a= " a
Print "b= " b
End                  


If I run the program as is I get:

greater than
a= 100
b= 100.1

b = a isn't recognized?
------------------------------------------
When I change c to 0.2 I get:

greater than
a= 100
b= 100

b is greater then a?
------------------------------------------
When I change c to 0.25 I get:

equal
a= 100
b= 100

b = a is recognized, seems to work
Edited 2023-04-21 20:00 by DrifterNL
Floating Point Keeps Sinking Me!
Back To Integer So I Don't Get Injured.
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4253
Posted: 10:06am 21 Apr 2023
Copy link to clipboard 
Print this post

Option default float ?

Yes, this is strange. As if behind the comma, there is a difference that is not printed on screen.

Volhout
Edited 2023-04-21 20:13 by Volhout
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9133
Posted: 10:12am 21 Apr 2023
Copy link to clipboard 
Print this post

This is standard floating point behaviour 0.1 cannot be exactly expressed as a float therefore 1000*0.1 will not equal 100. In general you should never test for equality of floats after any arithmetic but must allow a small delta e.g.

if abs(a-b)<0.000000001
 
DrifterNL

Regular Member

Joined: 27/09/2018
Location: Netherlands
Posts: 58
Posted: 10:28am 21 Apr 2023
Copy link to clipboard 
Print this post

Thanks for the info!
Darn, floating point got me again.  
Floating Point Keeps Sinking Me!
Back To Integer So I Don't Get Injured.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2079
Posted: 10:46am 21 Apr 2023
Copy link to clipboard 
Print this post

Think I might have mentioned this before, Years ago HiSoft BASIC had the == operator which meant "roughly equal to" precisely because of FP wrinkles like this

so  

IF 1000*0.1 == 100

would be true

no idea how the thresholds were set up

As a callow youth taking my first steps in this stuff professionally, It was always drummed into us to "get things into integer ASAP"
 
Print this page


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

© JAQ Software 2024