top | item 4222032

Why does parseInt(1/0, 19) return 18?

55 points| onemach | 13 years ago |stackoverflow.com | reply

66 comments

order
[+] mattdeboard|13 years ago|reply
Contrary to what some people may think, this is non-obvious & surprising behavior which has nothing to do with JS being a "dynamic language". It requires knowing that the IEEE floating point standard arbitrarily defines 1/0 as positive infinity instead of being (the more widely accepted, obvious and unsurprising) undefined value. I believe JS is a powerful language but calling this reasonable behavior smacks of Stockholm Syndrome.
[+] lifthrasiir|13 years ago|reply
It is mainly related to the fact that parseInt in JS slightly converts a positive infinity to a string "Infinity". Base 19 does not add anything to the humor. And well, neither does the presence of a positive infinity itself.

So yes, this is nothing to do with JS being a dynamic language (which main feature is not a variable/slot but a value having a type), but it's something to do with it being a weakly typed language (which main feature is an implicit and generally unpredictable type conversion). Of course many popular dynamic languages are still strongly typed in this sense.

[+] lucian1900|13 years ago|reply
I would say the (bigger) problem is in JavaScript randomly calling toString on things, especially when you're calling parseInt on a number already.
[+] ibotty|13 years ago|reply
that js implements the floating point standard is not js' fault. but that parse("1something unreasonable", 2) == 1 is just wrong. it should generate an exception.
[+] MrEnigma|13 years ago|reply
If this was in PHP people would be crying out what a bad and horrible language it is.

Javascript it's seen as an amusing conundrum.

[+] asto|13 years ago|reply
The reason javascript won't get nearly as much ridicule as PHP is because there's no other choice with javascript. Most arguments against PHP are in the form - I'm using <super awesome language> because PHP has .... faults.

Also, the fact that very few people code pure javascript, instead choosing frameworks like jquery, goes to show how much people like it!

[+] chris_wot|13 years ago|reply
I know! In PHP, try the equivalent:

  print intval(NaN, 19);
Result in PHP 5.2.17 is:

  0
Clearly, that is correct.

PHP does the following:

  intval('42', 8); // => 34
  intval(42, 8);   // => 42
Now try:

  print intval(strval("19.99"*100));
  print "<br>";
  print intval("19.99"*100);
This returns:

  1999
  1998
[+] Lazare|13 years ago|reply
First, Javascript isn't nearly as bad as PHP. Second, even the biggest fans of Javascript recognize its problems (the same can't be said of PHP). Third, even the biggest decractors of Javascript recognize its one overwhelming strength: It's the only language to run in the browser.
[+] bentlegen|13 years ago|reply
The reality is that nobody writes parseInt(1/0, 19). Get off your high horse.
[+] JoeCortopassi|13 years ago|reply
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.
[+] LBarret|13 years ago|reply
Please put Python or Ruby or X in the browser.

Javascript is no better than PHP. It is there - like Php in its time- because we have no other choice.

We're supposed to be professionals and one of our main tool is a joke.

[+] tikhonj|13 years ago|reply
Python also has plenty of stupid edge cases while being less functional, flexible but more complex than JavaScript. My theory is that Python gets good press because nobody has to use Python, so the people who don't like it just use a different language instead of complaining. Obviously, this does not hold for JavaScript.

If you're going to complain about language choices, don't force everybody on Python!

I can't comment on Ruby because I have never had to use it.

What would be perfect is Scheme in the browser. And, apparently, we almost had it except marketing or management forced Eich to make it look like Java resulting in JavaScript.

[+] saraid216|13 years ago|reply
No one is stopping you from rolling your own browser and taking market share away from the current Big Three.

Or from forking Chrome, since it's open source, and then offering your patch back to the central repo. Not sure if Firefox is open source, but could use that instead, too, if it is.

[+] chris_wot|13 years ago|reply
Ah! Nice work - that makes total sense.

Incidentally, PHP doesn't have the concept of positive or negative Infinity, just NaN. However, when I tried:

  print inval(1/0);
I got an exception. But point taken :-)

Edit: I might not be correct about positive or negative infinity in PHP... apologies if I have at all mislead anyone!

Also: got a proxy error, so this got reposted, then I deleted one of the posts, but the other post got marked deleted. So third post about this!

[+] saurik|13 years ago|reply
Please read my comment below regarding this behavior in PHP. In addition, I will add that 1/0 here is not returning NaN (if you attempt to print it as a string you won't get "NaN"), and intval itself actually has the same behavior as JavaScript. (edit: Oh, I just realized the other comment I replied to was also you. ;P)
[+] jaimebuelta|13 years ago|reply
Am I the only one that has laugh maniacally after reading this? X-D