Interesting Javascript Number Problem

Registered: 25.05.12 19:13
Timezone: UTC +3
Posts: 19

I just found this interesting article about numbers in JavaScript. Apparently JavaScript doesn't actually have integers, just floating point numbers, and so it actually just approximates integers the same way decimal numbers get approximated. So you end up with problems like (I copied these results from my JS console):

>>> 10000000000000000 === 10000000000000001
>>> 10000000000000000+1

Here's the article:

Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 165

You must browse reddit smile. I saw that this morning. Pretty interesting. I wasn't aware that javascript handled numbers that way.

Registered: 03.09.11 00:08
Timezone: UTC +1
Posts: 31

Thank you Senzon for sharing that article with us thumbup

Registered: 25.08.11 02:05
Timezone: UTC +0
Posts: 11

Interesting! I have to say, I wasn't aware of that either...

I guess on the one hand it "reduces complexity" in that it saves on multiple types for JS to store and manipulate/carry out any operations on numbers. If they're ALL in floating point, it saves casting and conversion - perhaps at the expense of memory and speed though.

However, it looks like they wrap it up to the Nth degree to hide that fact. JS must waste a lot of time just determining types? For any given variable JS must have to inspect the variable contents first - which seems a bit backwards and more intensive than just having typed variable in the first place, negating any benefit of a single numeric type?

Meh, maybe I am over analysing it smile CPU Architecture exam in two days time, I'll blame that!