Getting integers to behave acceptably in JavaScript is harder than it seemed at first, due to the underlying Number representation.
Because we're using |0, we're pretending Int is in the range of Int32. We currently don't apply |0 to the result of multiplication so you can end up with numbers out of that range (so this should probably be considered a bug). If we do apply it to multiplication results, multiplication is no longer associative when a product overflows the Int32 range.
Even if we don't use |0, there is still a problem here, as JavaScript still has an upper and lower bound on values that are safe to represent as integers, ±9007199254740991, and additionally, using Math.floor everywhere will be significantly slower than |0.
I suppose technically Number is not law abiding either due to precision and representation problems. Do we just have to accept that both these types aren't true Semirings (etc)?
Getting integers to behave acceptably in JavaScript is harder than it seemed at first, due to the underlying
Numberrepresentation.Because we're using
|0, we're pretendingIntis in the range ofInt32. We currently don't apply|0to the result of multiplication so you can end up with numbers out of that range (so this should probably be considered a bug). If we do apply it to multiplication results, multiplication is no longer associative when a product overflows theInt32range.Even if we don't use
|0, there is still a problem here, as JavaScript still has an upper and lower bound on values that are safe to represent as integers, ±9007199254740991, and additionally, usingMath.flooreverywhere will be significantly slower than|0.I suppose technically
Numberis not law abiding either due to precision and representation problems. Do we just have to accept that both these types aren't trueSemirings (etc)?