0.25 - 0.25 <> 0

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

0.25 - 0.25 <> 0

Post by Shaun_B »

Last night I had an issue with ZX BASIC in that I was getting an odd result when LET M=.25 and I took .25 away. The result was not zero. I don't remember exactly but the number returned was something like 1.141322E-10

I fixed it like this:

Code: Select all

IF LEN STR$ M>6 THEN LET M=0
This might be how I had EightyOne set up. In what other circumstances could this happen?

Thanks,

Shaun.
User avatar
PokeMon
Posts: 2264
Joined: Sat Sep 17, 2011 6:48 pm

Re: 0.25 - 0.25 <> 0

Post by PokeMon »

If you calculating with floating point values these may loose precision over the time (during calculations).
You could use INT to round them. Be aware that INT only cuts the value, doesn't round up.

For two digits you could use something like this:

Code: Select all

10 LET A=2/3
20 LET R=INT(A*100)/100
30 PRINT A,R
This should round correctly:

Code: Select all

10 LET A=2/3
20 LET R=INT(A*100+0.5)/100
30 PRINT A,R
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: 0.25 - 0.25 <> 0

Post by Shaun_B »

So where I'm looping and decreasing a value (each loop is LET M=M-.25), I'm fixing with this:

Code: Select all

IF M=INT M THEN LET M=INT M
This should therefore basically call the INT function every 4th loop and will hopefully will fix recursive rounding errors.

Regards,

Shaun.
User avatar
1024MAK
Posts: 5087
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: 0.25 - 0.25 <> 0

Post by 1024MAK »

This problem exists on the ZX Spectrum BASIC as well.

The way that real numbers are handled internally is not always exactly the same. As these are floating point numbers, using the = or <> operators, you often get unwanted results.

In my programs I test for a small range of values using the greater than and less than operators.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: 0.25 - 0.25 <> 0

Post by XavSnap »

"ZxBasic"..."editor"... from XavSnap? http://dskcenter.free.fr/zxtools

Ooops!

Yes, in "ZxBasic editor","ZxToken" and "Vb81 XuR" text input (loaded in the file explorer), the Visual basic give a corrupt value for numbers lower than ".5".
It's a Vb integer fault, due to an internal error!

If a text file is imported to "ZxBasic editor","ZxToken" or "Vb81 Xur" (same import codes used), just convert the number to a VAL"xxxx" function!
For all of values lower than .5 !
:oops:

Example:
10 LET M=.2 >>> 10 LET M=VAL".2"

10 LET M=.123456789 >>> 10 LET M=VAL".123456789"

NOTE:
To save a file in a "ZxBasic editor" format, just type : SAVE"MYPROG.TXT"
The text file will be saved in text format (ASM hex/dec+BASIC) in the current work directory.
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
dr beep
Posts: 2059
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: 0.25 - 0.25 <> 0

Post by dr beep »

You could also just use integers and only display the amount of "money" on screen as possible turns/4.
Then you can decrease the integer and the calculation error will not occur.
User avatar
PokeMon
Posts: 2264
Joined: Sat Sep 17, 2011 6:48 pm

Re: 0.25 - 0.25 <> 0

Post by PokeMon »

For the game it could be useful to think of coins only which are transferred into a sum in display routine only.
This way you can adapt the game more easily when inflate is getting higher or let the user choose it's own currency and set an appropriate amount.
A quarter dollar is quite different to a quarter rubel (RUB) when people from Russia like to play.
This would be a nice add-on to choose the country first (maybe just from a list). 8-)
dr beep
Posts: 2059
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: 0.25 - 0.25 <> 0

Post by dr beep »

And as for keeping the quarters on display on the ZX80:
LET M=INT (T/4)
LET Q=(T-M*4)*2+1
PRINT M;".";"00255075"(Q TO Q+1)
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: 0.25 - 0.25 <> 0

Post by Shaun_B »

dr beep wrote: Wed Mar 01, 2017 10:25 am And as for keeping the quarters on display on the ZX80:
LET M=INT (T/4)
LET Q=(T-M*4)*2+1
PRINT M;".";"00255075"(Q TO Q+1)
Does this work with the 4K ROM?

Thanks,

Shaun.
dr beep
Posts: 2059
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: 0.25 - 0.25 <> 0

Post by dr beep »

This works....
IMG_4324.PNG
(206.71 KiB) Downloaded 422 times
Post Reply