27

Fuck whoever invented octal literals with just a zero prefix. 042 should never not equal 42. How hard is it to have 0x42 for hex, 0b11 for binary and 0c42 for octal

Comments
  • 8
    Oh, fun with number systems...

    I remember scripts failing on 01.01.2008 because someone used some numeric constants with 0-prefix for years in the date calculation.

    That was fun.
  • 5
    @irene Because that's how decimal works and nobody in their right mind thinks that you can't put zeroes in front of a number
  • 2
    At least dates are cool.. No wait forget what I said
  • 2
    0-prefixed octal literal have been deprecated in JavaScript. The syntax error only occurs in strict mode for backwards compability.
  • 1
    @irene Not only do decimal numbers work that way, all numbers in positional numeral systems with a zero work exactly like that
  • 0
    @12bitfloat What languages are affected by this? I agree that a leading zero shouldn't be changing the number base. It is very common to prefix numbers with leading zeros.
  • 0
    @Demolishun JavaScript, PHP even Java
  • 1
    @irene 0*10^2 + 4*10^1 + 2*10^0. No special case
  • 0
    @12bitfloat Add C++ to that list. This bothers me...
  • 0
    @irene That's how this sorta number system is defined. 00042 is a completely valid number, exactly like 0.42000 is a valid number
  • 1
    @irene Yes. Are you honestly in defense of this retarded ass design flaw? 0c42 is more concise and more consistent
  • 0
    @irene What about trailing zeroes for decimals like 0.42000. Surely they're not valid either?
  • 0
    @irene https://en.wikipedia.org/wiki/... I'm just gonna leave this here...
  • 0
    Did I understand this conversation right?

    @12bitfloat thinks that 042 shouldn't be an octal representation in any language but resolve to an integer instead (042 == 42) and octal should be prefixed with other characters than numbers, like 0c42.

    @irene thinks that 042 shouldn't be a valid number in any base because same integer shouldn't have multiple representations (042 != 42).

    If I understood correctly, I must agree with @irene.
  • 2
    @joas Not that it shouldn't be. It *is* a valid number. And it shouldn't be interpreted as octal but simply as decimal. Sometimes you want to zeropad numbers e.g. in matrix calculations so it lines up nicely. That's how I discovered this in the first place because my math functions returned the wrong results. 042 is a valid decimal number and there's absolutely no reason you shouldn't be able to use it as an integer literal
  • 4
    I think the bigger lament here is not the leading zero. It is really that a leading zero can introduce a bug by silently changing the base of a number. Annoying as fuck when trying to debug.
  • 1
    @irene Well that I don't agree with. In math decimal can have leading zeros, so it is definitely confusing if they were different base or different kind of numbers.
  • 0
    @irene If you are thinking binary format or value of an integer. You are right. There's no leading zero. And there shouldn't be. The value of 0123 is 123.
  • 1
    Project Euler should know better then:

    https://projecteuler.net/problem=11

    Has leading zeros on this problem. I wonder how many people have tried to solve this problem and didn't realize the leading zeros were turning numbers into octal.

    Out of curiosity, does anyone actually use octal numbers for anything? I don't think I ever used octal for anything but PLC programming 20 years ago. And that was very limited as well. Some PLCs used octal addresses for things.
  • 0
    @irene I was talking about standard decimal representation in math not octals
  • 2
    32 replies? Alright here we go *cough cough*: Hitler!
Add Comment