Question about integer?, float? and number?

For the Compleat Fan
Locked
Jeremy Dunn
Posts: 95
Joined: Wed Oct 13, 2004 8:02 pm
Location: Bellingham WA

Question about integer?, float? and number?

Post by 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

This could create undefined situations because:

Code: Select all

> (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:

Code: Select all

(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

Locked