6 ms·
I did not realize until checking out this source that dividing by zero in JS outputs "Infinity". https://github.com/kitcambridge/evil.js/blob/gh-pages/evil.js#
by adamb_ 13y ago
I did not realize until checking out this source that dividing by zero in JS outputs "Infinity".
https://github.com/kitcambridge/evil.js/blob/gh-pages/evil.js#L87 https://github.com/kitcambridge/evil.js/blob/gh-pages/evil.j...
- wyuenho 13y agoNot always. 0/0 == NaN, but of course typing that into the console will yield false.
- _mtr 13y agoBecause, of course, NaN != NaN
- mbel 13y agoThis is happening because all numbers in JavaScript are IEEE 754 [0] double. +/-infinite may be returned for finite, non zero number divided by zero. This is also common behavior in many other languages with support for IEEE 754 floats, from the top of my head: JVM, .NET languages and Haskell do so too. [0] http://en.wikipedia.org/wiki/IEEE_754 http://en.wikipedia.org/wiki/IEEE_754
- RKoutnik 13y agoLonger answer on StackOverflow (including V8 source): http://stackoverflow.com/a/21893602/1216976 http://stackoverflow.com/a/21893602/1216976
- derefr 13y agoAnd, awfully-enough, even though Infinity is a required part of IEEE754, and happens all the time in Javascript, JSON doesn't recognize it.
- hrjet 13y agoIIRC, JSON is a strict subset of JS and Python.
- riffraff 13y agois it? "null" is not valid python, I think
- mercurial 13y agoIt isn't. It would be None. I believe parent is mistaken.
- hrjet 13y agoI stand corrected; I am not that familiar with python. Though I remember reading somewhere that JSON can be evaled in both JS and Python. Perhaps the python bit is not true.
- kibibu 13y agoJSON looks more-or-less like a Python dict, but you still wouldn't be able to parse {"x": NaN} or {"x": Infinity}
- derefr 13y agoOddly, there's actually no language that I know of that has NaN and Infinity constants. Many languages pretty-print those float values that way, but none of the parsers can interpret them back in.
- GeneralMayhem 13y ago...do you mean other than JavaScript? They're not actually parser-level constants, they're just defined as globals/on the window object, but they work.
- wisty 13y agoIf you do this first: null = None Infinity = float('Infinity') NaN = float('nan') (But no, it's not quite Python)
- polemic 13y agoWhy is that awful? JSON is not JavaScript. The string encoded value of the integer 1 is not a IEEE754 numeric either - it's just the character 1. In modern usage: JSON is to JavaScript as JavaScript is to Java.
- derefr 13y agoIt's awful because you would expect a network encoding of a set of types to be able to encode all the valid values of those types. If you claim that your encoding can represent floats, but it can't represent Infinity or NaN, then your encoding cannot, in fact, represent floats.
- polemic 13y agoIt makes no such claim! See http://www.json.org/ http://www.json.org/ - it is very cleary defined. *It's a string notation for a number. Don't get hung up on the implementations of floats or integers - it's irrelevant!
- mcv 13y ago> In modern usage: JSON is to JavaScript as JavaScript is to Java. This is totally wrong. Javascript has nothing whatsoever to do with Java. It only has a few very superficial syntactical similarities (curly brackets, semi-colon at the end of each line, and that awful 'new' keyword). JSON, on the other hand, stands for "Javascript Object Notation", and can be interpreted as Javascript (with 'eval'). It's explicitly meant for storing and transmitting javascript data, so when there's a javascript data value that is not supported by JSON, that is absolutely relevant.
- polemic 13y agoIn modern usage, you don't eval it. In modern usage is used for far more that just "javascript" applications. Of the four letters in the acryonym, the "J" is the least significant. Yes, it's javascript-y, but that's it. So many misunderstandings come from focusing on the "J". So, as JavaScript has basically nothing to do with Java (aside from superficial syntax similarities) so does JSON to JavaScript, for the same reasons. This "JSON is a subset of JavaScript" is just not true - at best it's "inspired by and interoperable with".