Question about integer?, float? and number?

Started by Jeremy Dunn, January 13, 2007, 02:48:07 PM

Previous topic - Next topic

Jeremy Dunn

Lutz,



NewLISP currently allows direct numerical input in number bases 8, 10 and 16 with the 0 and 0x prefixes for the binary bases. The integer?, float? and number? functions all return true if you input an octal or hexadecimal base number into them. I ran into a situation where I needed one or more of these functions to return true only for decimal representations. Could we have an extra argument on these functions where the user could specify one or more bases that they wish the function to test for? If no base is specified then all bases are tested for otherwise only the bases listed will be tested. For example:



(integer? n) - all bases tested for

(integer? n 8) - only base 8 integers acceptable

(integer? n '(10 16)) - decimal and hexadecimal acceptable

Lutz

#1
This could create undefined situations because:


> (int "0111" 0 2)
7
> (int "0111" 0 8)
73
> (int "0111" 0 16)
273
> (int "0111" 0 10)
111
>


the same string can be interpreted in different number bases.



But for octal? and decimal? you could do this:
(define (octal? x) (= (eval-string x) (int x nil 8)))

(define (decimal? x) (= (eval-string x) (int x nil 10)))

(octal? "10") => ni
(octal? "010") =? true

(decimal? "10") => true
(decimal? "010") => nil


Lutz