rnbw
New Member
Posts: 4
|
Post by rnbw on Apr 12, 2018 18:49:51 GMT
Simple numeric input routine from Just Basic Conforums
'Numeric check based on code by tooalalytical in Just Basic Forum [start] print "Give me some input. It must be a number." input test$
if str$(val(test$)) = test$ then print "Good. Input is a number" if test$ = chr$(13) then end if else print "No. Not a number" end if
goto [start]
end Text is entered as test$ and then press enter (chr$(13)). A test is then done by checking the numerical value of test$ and then converting this back to a string. If it is a valid number "Good. Input is a number" is displayed on the screen. If it contains any characters not recognised as a number the val(test$) will fail and "No. Not a number" will be displayed on the screen.
Having seen some of the very complicated routines for numeric input, I'm sure this can be put to good use.
insert code here
|
|
rnbw
New Member
Posts: 4
|
Post by rnbw on Apr 12, 2018 19:54:08 GMT
Simple numeric input routine from Just Basic Conforums I also put the routine on LB. Rod has pointed out that it has some problems when starting a number with . or 0. Best to disregard this routine. Better to follow.
|
|
|
Post by B+ on Apr 12, 2018 20:00:47 GMT
Simple numeric input routine from Just Basic Conforums 'Numeric check based on code by tooalalytical in Just Basic Forum [start] print "Give me some input. It must be a number." input test$
if str$(val(test$)) = test$ then print "Good. Input is a number" if test$ = chr$(13) then end if else print "No. Not a number" end if
goto [start]
end Text is entered as test$ and then press enter (chr$(13)). A test is then done by checking the numerical value of test$ and then converting this back to a string. If it is a valid number "Good. Input is a number" is displayed on the screen. If it contains any characters not recognised as a number the val(test$) will fail and "No. Not a number" will be displayed on the screen. Having seen some of the very complicated routines for numeric input, I'm sure this can be put to good use. insert code here [start] print "Give me some input. It must be a number." input test$
if str$(val(test$)) = test$ then print "Good. Input is a number" 'if test$ = chr$(13) then < why? 'end if else print "No. Not a number" end if
goto [start]
|
|
|
Post by B+ on Apr 12, 2018 20:09:10 GMT
Simple numeric input routine from Just Basic Conforums I also put the routine on LB. Rod has pointed out that it has some problems when starting a number with . or 0. Best to disregard this routine. Better to follow. Might be good for integers with another check! [start] print "Give me some input to check if Integer." input test$ if str$(val(test$)) = test$ and instr(test$, ".") = 0 then print "Good. Input is an Integer." else print "No. Not an Integer." end if
goto [start]
|
|
|
Post by Rod on Apr 12, 2018 20:14:08 GMT
Why? well its not Rays code and we have seen it has some quirks. What we really need is a definitive numeric/nonnumeric test sequence. Then we could have some fun. But it might be just as hard to define the number sequence as it will be to validate it.
|
|
|
Post by B+ on Apr 12, 2018 20:56:43 GMT
OK you got me going. This won't pass what arguably might be 0 (a number) like 000 or -0.0 but works pretty good otherwise: while 1 input "Enter some text to check if number > ";test$ if isNumber(test$) then print "Number" else print "Not number." print wend
function isNumber(x$) if val(x$) = 0 then if x$ = "0" then isNumber = NOT(0) else isNumber = 0 else if .5*val(x$) + .5*val(x$) = val(x$) then isNumber = NOT(0) else isNumber = 0 end if end function
|
|
|
Post by B+ on Apr 12, 2018 21:03:38 GMT
Dang! That's no good. VAL is messing it up.
|
|
|
Post by tsh73 on Apr 12, 2018 21:40:38 GMT
Well, VAL converts first part, as long as it could. So val("a") is 0 val("123a") is 123
What
if .5*val(x$) + .5*val(x$) = val(x$)
supposed to check?
|
|
|
Post by B+ on Apr 12, 2018 23:14:38 GMT
That is behaves like a number.
I swear we had this conversation before at the other JB forum. I wrote something that made sure if the string had a - sign there was only 1 at the far left, if the string has a decimal point there was only 1 and that rest of the contents of the string was digits... result: the sound of one hand clapping!
I think I ignored case with E notation altogether for simplicity sake.
Something like this:
while 1 input "Enter some text to check if number > ";test$ if isNumber(test$) then print "Number = ";val(test$) else print "Not number." print wend
function isNumber(x$) for i = 1 to len(x$) c$ = mid$(x$, i, 1) select case case c$ = "-" if i <> 1 then isNumber = 0 : exit function case c$ = "." if dotFlag then isNumber = 0 : exit function else dotFlag = NOT(0) case instr("0123456789", c$) <> 0 'OK case else isNumber = 0 : exit function end select next isNumber = NOT(0) end function
Will identify a number but will it pass VAL?
How can you use a number that passes test if VAL won't convert it correctly?
|
|
|
Post by tsh73 on Apr 13, 2018 8:31:13 GMT
>>but will it pass VAL you can try and see. For me it always pass VAL. (but it accepts "-", "." and "" as a number. )
|
|
|
Post by B+ on Apr 13, 2018 12:59:41 GMT
>>but will it pass VAL you can try and see. For me it always pass VAL. (but it accepts "-", "." and "" as a number. ) OK so,
while 1 input "Enter some text to check if number > ";test$ if isNumber(test$) then print "Number = ";val(test$) else print "Not number." print wend
function isNumber(x$) for i = 1 to len(x$) c$ = mid$(x$, i, 1) select case case c$ = "-" if i <> 1 then isNumber = 0 : exit function case c$ = "." if dotFlag then isNumber = 0 : exit function else dotFlag = NOT(0) case instr("0123456789", c$) <> 0 'OK case else isNumber = 0 : exit function end select next if x$ = "" or x$ = "-" or x$ = "." then isNumber = 0 else isNumber = NOT(0) end function
Starting a number with several 0's seems to be OK but starting with spaces is not OK. OK fix that too! while 1 input "Enter some text to check if number > ";test$ if isNumber(test$) then print "Number = ";val(test$) else print "Not number, ";val(test$) print wend
function isNumber(x$) x$ = trim$(x$) for i = 1 to len(x$) c$ = mid$(x$, i, 1) select case case c$ = "-" if i <> 1 then isNumber = 0 : exit function case c$ = "." if dotFlag then isNumber = 0 : exit function else dotFlag = NOT(0) case instr("0123456789", c$) <> 0 'OK case else isNumber = 0 : exit function end select next if x$ = "" or x$ = "-" or x$ = "." then isNumber = 0 else isNumber = NOT(0) end function
OK now ready for e?
|
|
|
Post by tsh73 on Apr 13, 2018 13:25:14 GMT
VAL do weird things. I was trying VAL("+1") - and it works But I would never expect this print val("++1") print val("+-1") print val("--1") and even print val("+-+--+++--1") (!) I really think two signs is too much. But! We can make it count parity of '-' for us!
for k = 1 to 10 s$="":m=0 for i = 1 to 10 if rnd(0)<.5 then s$=s$+"+" else s$=s$+"-": m=m+1 next print "is ";s$;" has odd number of '-'?", if val(s$;"1")<0 then print "Odd ";m else print "Even ";m next
|
|
|
Post by B+ on Apr 13, 2018 15:25:26 GMT
Oh thanks for heads up about + sign and whole strings of + and -, it was too simple before. OK how about this?
while 1 input "Enter some text to check if number > ";test$ print "Trim: ";trim$(test$) if isNumber(test$) then print "Number = ";val(test$) else print "Not number, ";val(test$) print wend
function isNumber(x$) x$ = trim$(x$) if x$ = "" or x$ = "-" or x$ = "." then isNumber = 0 : exit function epos = instr(x$, "e") for i = 1 to len(x$) c$ = mid$(x$, i, 1) select case case c$ = "-" or c$ = "+" if i = 1 or (i = (epos + 1)) then while c$ = "-" or c$ = "+" i = i + 1 c$ = mid$(x$, i, 1) wend i = i - 1 'OK else Print "Failed - sign" 'debug isNumber = 0 : exit function end if case c$ = "." if dotFlag then Print "Failed too many decimals." isNumber = 0 : exit function else if epos and i > epos then Print "Failed decimal past e." isNumber = 0 : exit function else dotFlag = NOT(0) end if end if case instr("0123456789", c$) <> 0 'OK case else if c$ = "e" and i = epos then 'OK else Print "Failed in case else." 'debug isNumber = 0 : exit function end if end select next isNumber = NOT(0) end function
|
|
|
Post by B+ on Apr 13, 2018 15:29:57 GMT
Ooops! forgot strings without a digit!
while 1 input "Enter some text to check if number > ";test$ print "Trim: ";trim$(test$) if isNumber(test$) then print "Number = ";val(test$) else print "Not number, ";val(test$) print wend
function isNumber(x$) x$ = trim$(x$) epos = instr(x$, "e") for i = 1 to len(x$) c$ = mid$(x$, i, 1) select case case c$ = "-" or c$ = "+" if i = 1 or (i = (epos + 1)) then while c$ = "-" or c$ = "+" i = i + 1 c$ = mid$(x$, i, 1) wend i = i - 1 'OK else Print "Failed - sign" 'debug isNumber = 0 : exit function end if case c$ = "." if dotFlag then Print "Failed too many decimals." isNumber = 0 : exit function else if epos and i > epos then Print "Failed decimal past e." isNumber = 0 : exit function else dotFlag = NOT(0) end if end if case instr("0123456789", c$) <> 0 hasDigit = 1 case else if c$ = "e" and i = epos then 'OK else Print "Failed in case else." 'debug isNumber = 0 : exit function end if end select next if hasDigit then isNumber = NOT(0) else isNumber = 0 end function
|
|
|
Post by B+ on Apr 13, 2018 15:43:55 GMT
OK, so a problem if the number is too long:
Output
|
|