News for hackers. Stuff that matters.
Re:Why does parseInt(1/0, 19) return 18? by JoeCortopassi on Tuesday July 10, 2012 @01:28AM (#4222261)
The only reason something like this makes it to the front page, is because of the popularity of things like node.js right now. This is a bug, not a feature.
Re:Why does parseInt(1/0, 19) return 18? by chris_wot on Tuesday July 10, 2012 @02:36AM (#4222431)
No, no it's not. It's actually part of ECMA-262.

Javascript allows numbers to be a number, NaN or positive/negative infinity. It is well specified within the spec that "Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule already stated above." [1]

Before you get your knickers in a twist, this is compliant with IEEE 754; although the IEEE define it as an exception.

1. ECMA-262, pg 74, "11.5.2 Applying the / Operator"

Re:Why does parseInt(1/0, 19) return 18? by JoeCortopassi on Tuesday July 10, 2012 @12:29PM (#4224610)
I understand that this is part of the spec. But that is a bug to allow division by zero to return a string, and then allow a string to have it's first letter indexed by a math function as a representation of a non base 10 number, is a bug! Just because it is part of the spec doesn't make it a sane addition to the language.
Re:Why does parseInt(1/0, 19) return 18? by chris_wot on Wednesday July 11, 2012 @08:55AM (#4229002)
Division by zero doesn't return a string. It returns a well defined value that is within the domain of the data type, which is positive infinity. parseInt does what it is specified to do - it takes the numerical value (positive infinity) and converts it to a string, then searches the string for the first character that it recognises as a contiguous number ("I", which is 18 in base 19). If it finds one, then it returns the value as an integer, if not then it returns NaN.

So not a bug, though I agree the string search is particularly silly. It can't be a bug if it does what the specified algorithm says it does. It would be a bug if it gave a different result.

Re:Why does parseInt(1/0, 19) return 18? by JonoW on Tuesday July 10, 2012 @02:33AM (#4222419)
The crazy part is that it's not a bug; it's behaving exactly as it's specified to.