1. Types and stuff
- Posted by Falkon <Falkn13 at IBM.NET> Apr 25, 1998
- 492 views
- Last edited Apr 26, 1998
While we're on the subject of big numbers and structured data with limited types and all, I thought I'd share my big oopsie of the day. --------------------------------------- include get.e type Number( sequence string ) sequence check check = value( string ) --make sure it's a valid object, and a number at that. if (check[1] = GET_SUCCESS) and atom( check[2] ) then return( 1 ) end if return( 0 ) end type sequence test, currentcash test = "4 3x65 Fred" --or how about "4,685,736,552"? currentcash = value( test ) If Number(test) then printf( 1, "%d", currentcash[2] ) else puts( 1, "Not a valid number" ) end if ---------------------------------------------------- Okay, quick, off the top of your head...what's the output? Both of 'em evaluate to numbers. The actual output though, is 4. In either case. After rereading the description of value() carefully, I realize it's supposed to work that way. But I had _assumed_ that the first case would turn up a value of { 4, {3,'x',6,5}, "Fred" } or even {4, "3x65", "Fred" } and therefore would fail the atom() check. Just thought I'd share that...imagining the looks on peoples faces when they found out the national debt was $3. Falkon
2. Types and stuff
- Posted by Ad Rienks <Ad_Rienks at COMPUSERVE.COM> Apr 26, 1998
- 521 views
Falkon wrote: >While we're on the subject of big numbers and structured data >with limited types and all, I thought I'd share my big oopsie of >the day. >--------------------------------------- include get.e >type Number( sequence string ) sequence check check =3Dvalue( string ) > --make sure it's a valid object, and a number at that. = >if (check[1] =3D GET_SUCCESS) and atom( check[2] ) then = > return( 1 ) end if return( 0 ) end type >sequence test, currentcash test =3D "4 3x65 Fred" --or howabout "4,685,736,552"? currentcash =3D value( test ) >If Number(test) then printf( 1, "%d", currentcash[2] ) else = > puts( 1, "Not a valid number" ) end if >---------------------------------------------------- > Okay, quick, off the top of your head...what's the output? = >Both of 'em evaluate to numbers. The actual output though, is >4. In either case. After rereading the description of value() >carefully, I realize it's supposed to work that way. But I had >_assumed_ that the first case would turn up a value of { 4, >{3,'x',6,5}, "Fred" } or even {4, "3x65", "Fred" } and >therefore would fail the atom() check. Just thought I'd share >that...imagining the looks on peoples faces when they found out >the national debt was $3. >Falkon I think the following type definition should do the trick: type number(sequence string) if compare(string, {}) =3D 0 then -- empty string is also not a valid number! return 0 end if for n =3D 1 to length(string) do if not find(string[n], "0123456789.") then return 0 end if end for -- if you 'fall through' the test, everything is alright return 1 end type The above test is for decimal numbers, it's easy to test for integers jus= t by leaving out the period in the second element of the find function. IMO a better solution would be to reprogram the type definition as a function, that can be inside a loop, checking for good input. Actually no= reprogramming is necessary, as a type definition is a special case of a function. -- example.ex -- input routine object input input =3D {} -- empty while not number(input) do clear_screen() puts(1, "A number please") input =3D gets(0) -- the last or only character is always a newline! input =3D input[1..length(input) - 1] end while input =3D value(input) input =3D input[2] -- input contains a valid number now! printf(1, "\nYour input was: %f", input) Hope this helps, Ad