Numeric Input routine

Anything QL Software or Programming Related.
Post Reply
RWAP
RWAP Master
Posts: 2839
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Numeric Input routine

Post by RWAP »

I have been asked why the following code will not work on Minerva / SMSQ/e to check the validity of an input routine:

Code: Select all

10 GO SUB 9500
110 PRINT valid
120 STOP
9500 REMark
9502 valid=0
9505 INPUT x$
9506 IF x$='' THEN RETurn
9507 l=LEN(x$)
9510 FOR x=1 TO l
9515 IF x=1 AND x$(x)='-' THEN NEXT x
9520 IF x$(x)<'.' OR x$(x)>'9' THEN RETurn
9530 END FOR x
9540 valid=1
9550 RETurn
The problem is that if you enter '1213' or '12.12' when prompted, it still falls foul of line 9520.

Now at first glance, there would not appear to be too much wrong with this code, but it is not comparing values properly in 9520 and it may be linked to how Minerva handles the value .

Any ideas?

I did re-write the code to a working routine, in much improved form:

Code: Select all

10 INPUT x$
20 valid=validate(x$)
110 PRINT valid
120 STOP
9500 DEFine FuNction validate(num$)
9501 LOCal x, valid
9506 IF num$='' THEN RETurn 0
9507 l=LEN(num$)
9510 FOR x=1 TO l
9515 IF x=1 AND num$(x)='-' THEN NEXT x
9520 IF CODE(x$(x))<CODE('.') OR CODE(x$(x))>CODE('9') THEN EXIT x
9525 NEXT x
9527 RETurn 1
9530 END FOR x
9545 RETurn 0
9550 END DEFine
However, the original problem with 9520 is certainly odd and unexpected!


User avatar
tofro
Font of All Knowledge
Posts: 2702
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Numeric Input routine

Post by tofro »

Rich,
your original code works OK for me on SMSQ/E 3.13 (Except that it still has a 'hole' in it that would accept more than one decimal point - But that's in your second snippet as well), even with the input you mentioned as triggering the fault.

It is, however, a known fact that Minerva seems to change character sort order from original SuperBASIC. Maybe the reason of what you're sseing is hidden somewhere there.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
RWAP
RWAP Master
Posts: 2839
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Numeric Input routine

Post by RWAP »

True, the better code for handling figures is:

Code: Select all

10 INPUT x$
20 valid=validate(x$,2)
110 PRINT valid
120 STOP
9500 DEFine FuNction validate(num$,dp)
9501 LOCal x, decimal, postdecimal
9502 decimal=0: postdecimal=0
9506 IF num$='' THEN RETurn 0
9507 l=LEN(num$)
9510 FOR x=1 TO l
9515 IF x=1 AND num$(x)='-' THEN NEXT x
9520 IF CODE(x$(x))<CODE('.') OR CODE(x$(x))>CODE('9') THEN EXIT x
9521 IF x$(x)='.' THEN decimal=decimal+1 
9522 IF decimal>1 THEN EXIT x
9523 IF decimal=1 THEN postdecimal=postdecimal+1
9524 IF postdecimal>dp+1 THEN EXIT x
9525 NEXT x
9526 IF postdecimal=1 THEN RETURN 0
9527 RETurn 1
9530 END FOR x
9545 RETurn 0
9550 END DEFine
This checks for the number of decimal places also!

:D


User avatar
tofro
Font of All Knowledge
Posts: 2702
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Numeric Input routine

Post by tofro »

Yep, looks like It's what I just said:

First, tried in on Minerva 1.98 - And it fails as you said.

The reason is that

Code: Select all

print '1' < '.'
will yield "1" on Minerva
and "0" everywhere else.

I was under the impression you could change this behaviour by a poke but couldn't find it in the manual I have.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
RWAP
RWAP Master
Posts: 2839
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Numeric Input routine

Post by RWAP »

Interesting - I can't find any reference to this change in the updates_doc or the Minerva manual - so sorry, it is missing from the SBASIC/ SuperBASIC Reference Manual.

Has anyone found any reference to this to say what changes were made?


User avatar
tofro
Font of All Knowledge
Posts: 2702
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Numeric Input routine

Post by tofro »

I'm sure I have a reference to that somewhere. (But where?)

They were argueing that the QL's character comparison routines should work like "Sorting in the phonebook" and not based on ASCII code.

I'll use the week-end to try and find that - Could have been in one of the old QL World magazines

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
RWAP
RWAP Master
Posts: 2839
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Numeric Input routine

Post by RWAP »

I admit that it does sort of ring a bell - but I checked the updates_doc and versions_doc as well as the manual and cannot find any reference to it.

You would think that there would be a mention of it in the Minerva source code or SMSQ Reference manual when it comes to ut.cstr - but not that I can find.
:?


Post Reply