Am 12.03.2009 um 22:08 schrieb levand:
> Seems like there's a bug here. All the digits less than 8 work. If
> leading zeros aren't allowed, at least the behavior ought to be
> consistent.
Leading zeros indicate octal, which has no digits like 8 or 9...
In so far it's not a bug nor is it inconsistent, I guess.
Sincerely
Meikel
This is pretty standard behaviour. Here's Python:
>>> 07
7
>>> 08
File "<stdin>", line 1
08
^
SyntaxError: invalid token
>>> 010
8
>>>
And perl:
$ perl -e 'print 07, "\n";'
7
$ perl -e 'print 08, "\n";'
Illegal octal digit '8' at -e line 1, at end of line
Execution of -e aborted due to compilation errors.
Although perl gives you a better error message.
And the Unix "printf" command line tool:
$ printf "%d\n" 07
7
$ printf "%d\n" 08
-bash: printf: 08: invalid number
0
And C:
$ gcc -c /tmp/octal.c
/tmp/octal.c:7:20: error: invalid digit "8" in octal constant
/tmp/octal.c: In function 'main':
/tmp/octal.c:9: error: expected ';' before '}' token
I don't know Ruby, but it appears to do the same:
$ ruby
print 07, "\n"
print 08, "\n"
-:2: Illegal octal digit
print 08, "\n"
^
On the other hand, it's not universal.
sbcl:
* 07
7
* 08
8
--
Michael Wood <esio...@gmail.com>
Common Lisp uses a separate syntax for binary/octal/hex literals. Legal:
#b1011, #o377, #xDEADBEEF, #36rZZZ (Base 36 anyone?)
Illegal:
#b2, #o8, #xQUICKSAND
(Of course, #36rCLOJURE => 27432414842 :-) )
Aloha,
David Sletten
> This is also available in Clojure:
> user> 36rCLOJURE
> 27432414842
I see Common Lisp has *print-base* and *print-radix* that control the
base used in printing rational numbers (including integers) and
whether or not the printed output includes the base indicator.
These wouldn't be hard to implement for Clojure. Is there any interest?
--Steve
> I see Common Lisp has *print-base* and *print-radix* that control
> the base used in printing rational numbers (including integers) and
> whether or not the printed output includes the base indicator.
>
> These wouldn't be hard to implement for Clojure. Is there any
> interest?
I see these are present (though currently commented out) in Tom
Faulhaber's cl-format package which I gather is on track to become
part of Clojure at some point.
--Steve
>
> Rather than going to the horrible effort </irony> of looking up to see
> if Clojure had support for binary notation, I had a Clojure prompt so
> I just tried it and got semi-surprising results:
>
> user=> #b010001
> java.lang.Exception: No dispatch macro for: b
> 4097
>
> I'm not surprised that Clojure complains of not knowing what manner of
> macro #b is but I was impressed (?) that it still yielded the correct
> value. Somewhere, deep in Clojure's little heart, it wants to do other
> bases.
>
Umm, that's not really the correct value. Clojure gave you octal
010001 -> 4097 not binary 010001 -> 17
Sorry,
David Sletten
> So, here's a request -- can we get macro dispatch for other base
> numbers? The CL notation is reasonable, already known and quite
> readable. Besides, Clojure tells me it /really/ /wants/ to... ;-)
For integers, Clojure currently supports all the bases from 2 to 36
just like Common Lisp. These are the differences:
- the syntax doesn't use a leading #
- Clojure doesn't provide #b, #o, or #x for binary, octal, hex
- Clojure interprets a leading 0 (zero) as meaning octal
- Clojure interprets a leading 0x as meaning hex
Note that by Clojure's rules, all integers regardless of radix start
with one of the decimal digits 0 through 9.
For ratios, Common Lisp supports providing a radix, but Clojure doesn't.
--Steve