views:

221

answers:

6

I was looking at this question. Basically having a leading zero causes the number to be interpreted as octal. I've ran into this problem numerous times in multiple languages.

Why doesn't the language explicitly require you to specify octal with a function call or a type (in strong typed languages) like:

oct variable = 2;

I can understand why hexadecimal (0x0234) has this format. Hex is pretty useful. An integer from the database will never have an x in it.

But octal numbers 0123 look like ints and are a pain to deal with. I've never used octal for anything.

Can anyone explain the rationale behind this usage? Is it just a bit of historical cruft?

+4  A: 

It's largely historic. The best solution I've seen is in the new version of Python, where octal is indicated with a special prefix character "o", much like hexadecimal's "x" prefix:

0o10 == 0x8 == 8
John Millikin
+2  A: 

99.9% of the reason it exists is to support chmod() calls, i.e. chmod(fd, 0755).

It does rather seem like a format more like hex's would be superior.

chaos
Ha! I guess I have used octal for something :)
Byron Whitlock
+2  A: 

It exists since working with 3-bit segments is almost as useful as working with 4-bit segments. This was more true in the past (e.g., seven-segment LEDs, chmod, etc.).

The real question is why haven't more languages adopted octal and binary notations in a more regular fashion:

10 == 0b1010 == 0o12 == 0x0A

I know that Python finally adopted the 0o8 notation... not sure if they have adopted the binary one as well. I guess a better question is Why does this still trip people up?

D.Shawley
it trips people up because octals are used so rarely that people forget about them
Kip
Python has adopted 0b and 0o prefixes, as of 2.6 / 3.0
John Millikin
+1  A: 

Yes, it's historical. C uses this way to specify literals in octal, and possibly it was used somewhere before that.

I've experienced it in Javascript, where parsing dates stops working in august. Up to july it works as '07' parsed as octal is still seven, but '08' is not a valid number... (The solution is to specify the number base in the parseInt call.)

In C# there are no binary or octal literals, perhaps the reasoning is that you shouldn't do as much bit fiddling that the language needs it...

Guffa
+1  A: 

I hate this too, I don't know why it's been carried forward into so many modern languages. I once knew someone who had a zip code like "09827" when he lived in NYC. Sometimes he had to input his zip code as "9827," because the leading zero would lead to error messages (since 9's and 8's are illegal characters in octal numbers).

Kip
A: 

Personally, I blame the programmer in this case. Why are you formatting an integer by zero padding? Zero padding is for strings, not numeric types.

Tim Hoolihan
If you think that's an outré style decision, you ought to see some of the code I maintain.
chaos
i can see zero padding a number, but in a string.
Tim Hoolihan
Compilers should make life easier for the programmer, not the other way around.
Mark Ransom
a compiler is supposed to make up for the programmer not knowing basic numeric datatypes?
Tim Hoolihan
in a scripting language you can pull data from a zero padded field from a database. Instead of internally being a string it is stored as an octal number. This is an issue when the data is incomming, not when you type it in code.
Byron Whitlock