Page 1 of 2

0.25 - 0.25 <> 0

Posted: Sun Feb 26, 2017 9:39 am
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.

Re: 0.25 - 0.25 <> 0

Posted: Sun Feb 26, 2017 12:52 pm
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

Re: 0.25 - 0.25 <> 0

Posted: Sun Feb 26, 2017 2:06 pm
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.

Re: 0.25 - 0.25 <> 0

Posted: Sun Feb 26, 2017 2:54 pm
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

Re: 0.25 - 0.25 <> 0

Posted: Mon Feb 27, 2017 6:13 am
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.

Re: 0.25 - 0.25 <> 0

Posted: Wed Mar 01, 2017 9:14 am
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.

Re: 0.25 - 0.25 <> 0

Posted: Wed Mar 01, 2017 10:21 am
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-)

Re: 0.25 - 0.25 <> 0

Posted: Wed Mar 01, 2017 10:25 am
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)

Re: 0.25 - 0.25 <> 0

Posted: Thu Mar 02, 2017 3:04 pm
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.

Re: 0.25 - 0.25 <> 0

Posted: Fri Mar 03, 2017 11:28 am
by dr beep
This works....
IMG_4324.PNG
(206.71 KiB) Downloaded 422 times