0.25 - 0.25 <> 0

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

0.25 - 0.25 <> 0

Post by Shaun_B » Sun Feb 26, 2017 8:39 am

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: 2137
Joined: Sat Sep 17, 2011 5:48 pm

Re: 0.25 - 0.25 <> 0

Post by PokeMon » Sun Feb 26, 2017 11:52 am

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: 312
Joined: Wed Apr 22, 2009 9:22 am

Re: 0.25 - 0.25 <> 0

Post by Shaun_B » Sun Feb 26, 2017 1:06 pm

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: 1771
Joined: Mon Sep 26, 2011 9:56 am
Location: Looking forward to summer in Somerset, UK...

Re: 0.25 - 0.25 <> 0

Post by 1024MAK » Sun Feb 26, 2017 1:54 pm

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

User avatar
XavSnap
Posts: 461
Joined: Sat May 10, 2008 3:23 pm

Re: 0.25 - 0.25 <> 0

Post by XavSnap » Mon Feb 27, 2017 5:13 am

"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.

dr beep
Posts: 1045
Joined: Thu Jun 16, 2011 7:35 am
Location: Boxmeer

Re: 0.25 - 0.25 <> 0

Post by dr beep » Wed Mar 01, 2017 8:14 am

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: 2137
Joined: Sat Sep 17, 2011 5:48 pm

Re: 0.25 - 0.25 <> 0

Post by PokeMon » Wed Mar 01, 2017 9:21 am

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: 1045
Joined: Thu Jun 16, 2011 7:35 am
Location: Boxmeer

Re: 0.25 - 0.25 <> 0

Post by dr beep » Wed Mar 01, 2017 9: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)

Shaun_B
Posts: 312
Joined: Wed Apr 22, 2009 9:22 am

Re: 0.25 - 0.25 <> 0

Post by Shaun_B » Thu Mar 02, 2017 2:04 pm

dr beep wrote:
Wed Mar 01, 2017 9: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: 1045
Joined: Thu Jun 16, 2011 7:35 am
Location: Boxmeer

Re: 0.25 - 0.25 <> 0

Post by dr beep » Fri Mar 03, 2017 10:28 am

This works....
IMG_4324.PNG
IMG_4324.PNG (206.71 KiB) Viewed 225 times

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests