3 ms·
> JS Can't even perform integer math Can you explain the integer math thing? What exactly can't it do?
by redidas 14y ago
> JS Can't even perform integer math
Can you explain the integer math thing? What exactly can't it do?
- snprbob86 14y agoJavaScript's Number type is double floating point: $ python Python 2.7.2 (default, Jun 20 2012, 16:23:33) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> x = 1000000000000000001 >>> x 1000000000000000001 >>> x == x + 1 False $ irb irb(main):001:0> x = 1000000000000000001 1000000000000000001 irb(main):002:0> x == x + 1 false $ clj Clojure 1.4.0 user=> (def x 1000000000000000001) #'user/x user=> x 1000000000000000001 user=> (= (inc x) x) false $ node > x = 1000000000000000001 1000000000000000000 > x === x + 1 true
- redidas 14y agoThanks for the example. I'm curious as to why this discounts a tool meant for performing IO and various web development tasks in general. Has anyone run into a integer math related issue when using node (that was a deal-breaker)?
- jjm 14y agoDon't be scared, see https://github.com/JasonGiedymin/pi-node https://github.com/JasonGiedymin/pi-node
- riffraff 14y agopeople usually run into the issue from time to time when using JSON to transmit numbers to a javascript process, for example[0][1][2] [0] http://stackoverflow.com/questions/8663298/json-transfer-of-bigint-12000000000002539-is-converted-to-12000000000002540 http://stackoverflow.com/questions/8663298/json-transfer-of-... [1] http://stackoverflow.com/questions/6320908/twitter-json-in-reply-to-status-id-wrong http://stackoverflow.com/questions/6320908/twitter-json-in-r... [2] http://stackoverflow.com/questions/209869/what-is-the-accepted-way-to-send-64-bit-values-over-json http://stackoverflow.com/questions/209869/what-is-the-accept...
- ender7 14y agoI don't see how this is any different from overflowing an int32 in any other programming language. Sure, with an int, x != x + 1 even after an overflow, but your program is still going to crash when it tries to bill you for negative two billion widgets. If you're dancing on the edge of the limits of numerical representation then you need to write code to protect against bad things. If you don't write said code, your program is going to fail to work correctly, no matter what language you use.
- mietek 14y agoThe difference is, in other programming languages you usually have a choice to use an integer number type.
- hythloday 14y ago"Overflowing an int" is something that I worry about when I'm thinking in terms of memory layout of data. I shouldn't have to worry about it when working at a higher level of abstraction. Python handles this correctly: >>> int('0x7fffffff', 16) 2147483647 >>> int('0x7fffffff', 16) + 1 2147483648L