"08" and "09" are invalid numbers, but "01" through "07" are fine?

950 views
Skip to first unread message

levand

unread,
Mar 12, 2009, 5:08:41 PM3/12/09
to Clojure
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.

(def n 01)
#'user/n

...

(def n 07)
#'user/n

BUT

(def n 08)
clojure.lang.LispReader$ReaderException:
java.lang.NumberFormatException: Invalid number: 08

(def n 09)
clojure.lang.LispReader$ReaderException:
java.lang.NumberFormatException: Invalid number: 09

Same thing with additional leading zeros...

(def n 007)
#'user/n

(def n 008)
clojure.lang.LispReader$ReaderException:
java.lang.NumberFormatException: Invalid number: 008

Jeffrey Straszheim

unread,
Mar 12, 2009, 5:12:57 PM3/12/09
to clo...@googlegroups.com
In Java, numbers prefixed with a "0" are treated as octal.  It should not surprise us, then, that 08 and 09 blow up.

Meikel Brandmeyer

unread,
Mar 12, 2009, 5:13:13 PM3/12/09
to clo...@googlegroups.com
Hi,

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

Shawn Hoover

unread,
Mar 12, 2009, 5:14:34 PM3/12/09
to clo...@googlegroups.com
Indeed. And if you just use 0-7 and it's all good:
user> 010
8

Luke VanderHart

unread,
Mar 13, 2009, 8:14:37 AM3/13/09
to Clojure
Well! You learn something new every day.

Ironically, I knew about octal, but back in the day when I was
learning Java, the book I was reading didn't have a typeface that
distinguished O and 0 very well, and since I never had to use them I
never was corrected. Interesting.

Thanks!

Michael Wood

unread,
Mar 13, 2009, 9:07:34 AM3/13/09
to clo...@googlegroups.com
On Fri, Mar 13, 2009 at 2:14 PM, Luke VanderHart
<luke.va...@gmail.com> wrote:
>
> Well! You learn something new every day.
>
> Ironically, I knew about octal, but back in the day when I was
> learning Java, the book I was reading didn't have a typeface that
> distinguished O and 0 very well, and since I never had to use them I
> never was corrected. Interesting.

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>

David Sletten

unread,
Mar 13, 2009, 9:19:52 AM3/13/09
to clo...@googlegroups.com

On Mar 13, 2009, at 3:07 AM, Michael Wood wrote:
>
> This is pretty standard behaviour.
>
> On the other hand, it's not universal.
>
> sbcl:
>
> * 07
>
> 7
> * 08
>
> 8
>

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


André Thieme

unread,
Mar 15, 2009, 7:11:21 PM3/15/09
to Clojure
On 13 Mrz., 14:19, David Sletten <da...@bosatsu.net> wrote:

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

This is also available in Clojure:
user> 36rCLOJURE
27432414842

Stephen C. Gilardi

unread,
Mar 15, 2009, 7:29:45 PM3/15/09
to clo...@googlegroups.com

On Mar 15, 2009, at 7:11 PM, André Thieme wrote:

> 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

Stephen C. Gilardi

unread,
Mar 15, 2009, 8:00:55 PM3/15/09
to clo...@googlegroups.com

On Mar 15, 2009, at 7:29 PM, Stephen C. Gilardi wrote:

> 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

Aaron Brooks

unread,
Mar 15, 2009, 10:53:15 PM3/15/09
to clo...@googlegroups.com
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.

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... ;-)

-Aaron

David Sletten

unread,
Mar 15, 2009, 11:20:36 PM3/15/09
to clo...@googlegroups.com

On Mar 15, 2009, at 4:53 PM, Aaron Brooks wrote:

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

Stephen C. Gilardi

unread,
Mar 16, 2009, 12:29:03 AM3/16/09
to clo...@googlegroups.com

On Mar 15, 2009, at 10:53 PM, Aaron Brooks wrote:

> 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

Paul Stadig

unread,
Mar 16, 2009, 6:27:00 AM3/16/09
to clo...@googlegroups.com
I couldn't find anything on the http://clojure.org/reader page, but someone had just posted an example of notation for alternative bases.

It looks like you just enter <BASE>r<NUMBER>

user=> 2r1000
8
user=> 3r300
java.lang.NumberFormatException: For input string: "300"
user=> 3r200
18


Paul
Reply all
Reply to author
Forward
0 new messages