octal 0 notation causes hard to find bugs

342 views
Skip to first unread message

Notan

unread,
Nov 12, 2009, 5:20:52 PM11/12/09
to golang-nuts
Octal notation makes 010 different from 10 which can be very hard for
beginners, and even more experienced users. I know that this is the
way it is in C, C++ and some other derived languages as well.
It took me days to spot an error like
...
2039,
4556,
0145,
4453
...
in a generated constant list.

A notation of 0o10 would be much clearer and more consistent with 0x10
for hex. One may write 0O10 if obfuscation is the goal.
By the way 0b010 for binary would be handy sometimes. (An optional 0d
for decimal makes the whole thing round).

Chas. Owens

unread,
Nov 12, 2009, 5:30:41 PM11/12/09
to Notan, golang-nuts
While you are at it, take a page from Perl's book and allow constants
like 100_000, it makes long number much easier to read.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

Valentin Nechayev

unread,
Nov 13, 2009, 3:23:46 PM11/13/09
to golang-nuts
On Nov 13, 00:20, Notan <notan.k...@gmail.com> wrote:
> Octalnotation makes 010 different from 10 which can be very hard for
> beginners, and even more experienced users. I know that this is the
> way it is in C, C++ and some other derived languages as well.
> It took me days to spot an error like
> ...
> 2039,
> 4556,
> 0145,
> 4453
> ...
> in a generated constant list.

I vote for this too. Real need of octal constants seems disappeared
with end of PDP-11 line. Now they give only confusing and weird bugs
which are quite hard to diagnose. I understand authors copied C style
but it's proper time to remove such legacy while it's possible.

> A notation of 0o10 would be much clearer and more consistent with 0x10
> for hex. One may write 0O10 if obfuscation is the goal.
> By the way 0b010 for binary would be handy sometimes. (An optional 0d
> for decimal makes the whole thing round).

Well, this is well-known syntax (used e.g. in Python); but also there
are another approaches:
Erlang - p#n (p - base, n - number), so 8#10 as octal "10" = decimal
"8"
Ada - #p#n
Scheme - #on (not checked by me), e.g. #o10

I would support any of them but not the horrible mush with leading
'0' ;(

Note this is also discussed in bug#42.

Gherald

unread,
Nov 13, 2009, 3:51:03 PM11/13/09
to golang-nuts
> A notation of 0o10 would be much clearer and more consistent with 0x10
> for hex. One may write 0O10 if obfuscation is the goal.
> By the way 0b010 for binary would be handy sometimes. (An optional 0d
> for decimal makes the whole thing round).

> While you are at it, take a page from Perl's book
> and allow constants like 100_000

Agreed. I vote for all three:

- Warning/error on "0145" (obsolete, for if someone copy-pastes from a
C-like language)
- 0o for octal, 0b for binary (useful !!) , and optional 0d
- optional _ separators for long-number legibility

Ian Lance Taylor

unread,
Nov 14, 2009, 10:46:03 AM11/14/09
to Notan, golang-nuts
Notan <notan...@gmail.com> writes:

> Octal notation makes 010 different from 10 which can be very hard for
> beginners, and even more experienced users. I know that this is the
> way it is in C, C++ and some other derived languages as well.
> It took me days to spot an error like
> ...
> 2039,
> 4556,
> 0145,
> 4453
> ...
> in a generated constant list.
>
> A notation of 0o10 would be much clearer and more consistent with 0x10
> for hex. One may write 0O10 if obfuscation is the goal.

As you say, a leading 0 means octal in a number of popular languages,
including C, C++, Java, and Python. I don't think we are going to
break from the pack in this regard.


> By the way 0b010 for binary would be handy sometimes. (An optional 0d
> for decimal makes the whole thing round).

I've put this idea on the list for consideration.

Ian

Ian Lance Taylor

unread,
Nov 14, 2009, 10:47:40 AM11/14/09
to Chas. Owens, Notan, golang-nuts
"Chas. Owens" <chas....@gmail.com> writes:

> While you are at it, take a page from Perl's book and allow constants
> like 100_000, it makes long number much easier to read.

I've put this on the list for future consideration. Thanks.

Ian

Valentin Nechayev

unread,
Nov 14, 2009, 10:53:53 AM11/14/09
to Ian Lance Taylor, Notan, golang-nuts
On Sat, Nov 14, 2009 at 5:46 PM, Ian Lance Taylor <ia...@google.com> wrote:

As you say, a leading 0 means octal in a number of popular languages,
including C, C++, Java, and Python.
No, Python rejects it now:
=== cite ===
 Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0.
=== end cite === (http://docs.python.org/3.1/reference/lexical_analysis.html#literals)
And, on the same page:
octinteger     ::=  "0" ("o" | "O") octdigit+

--
-netch-

ianr

unread,
Nov 19, 2009, 3:32:35 AM11/19/09
to golang-nuts
Hi,

On Nov 14, 5:46 pm, Ian Lance Taylor <i...@google.com> wrote:
> As you say, a leading 0 meansoctalin a number of popular languages,
> including C, C++, Java, and Python.  I don't think we are going to
> break from the pack in this regard.

With respect, "herd mentality" does not appear to be one of Go's
design goals :-)

IMHO the "leading 0 means octal" notation is a relic of the 'terse'
past, whereas we now have the opportunity to promote clarity: e.g.
0o644, 0xDEAD_BEEF, 100_000, 0b1010_0101 etc.

Ian

Russ Cox

unread,
Nov 19, 2009, 3:49:54 AM11/19/09
to ianr, golang-nuts
> IMHO the "leading 0 means octal" notation is a relic of the 'terse'
> past, whereas we now have the opportunity to promote  clarity:  e.g.
> 0o644, 0xDEAD_BEEF, 100_000, 0b1010_0101 etc.

http://code.google.com/p/go/issues/detail?id=151

also, clear is not the opposite of terse.
Reply all
Reply to author
Forward
0 new messages