Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upUse Math.trunc in Basics.truncate #591
Conversation
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
justinmimbs
Jul 7, 2016
Right on. But since Math.trunc isn't universally supported yet, may I suggest using n < 0 ? Math.ceil(n) : Math.floor(n) in the meantime?
justinmimbs
commented
Jul 7, 2016
|
Right on. But since |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Yes, thanks for your suggestion. I made another commit. |
lukewestby
added
the
bug
label
Aug 20, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
lukewestby
Aug 20, 2016
Member
Like @pisys mentioned, the current truncate only works for numbers in (-858993459, 858993459). I benchmarked @pisys's change against a couple other methods that don't exhibit this issue and this method seems to be pretty much on par with x - x % 1 and about 10x faster than parseInt(). I think it's good to merge in.
|
Like @pisys mentioned, the current |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Sep 22, 2016
Member
I'm putting this in #721 because it will change how people's programs work.
As far as I know, JS does not have 64 bit integers. So when converting from float to int, why would we expect to get anything besides a 32 bit integer?
|
I'm putting this in #721 because it will change how people's programs work. As far as I know, JS does not have 64 bit integers. So when converting from float to int, why would we expect to get anything besides a 32 bit integer? |
evancz
closed this
Sep 22, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
yjkogan
commented
Sep 22, 2016
|
Yeah my understanding is that all numbers in javascript are |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
lukewestby
Sep 22, 2016
Member
JS will faithfully represent integers up to 53 bits even if the engine opts to use ints instead of doubles internally to store them at 32 bits and below. As a result, other core functions that call into built-in Math stuff will work on larger values whereas things that are done by hand like truncate will clamp to 32 bits. An SSCCE that demonstrates this situation:
truncate 900719925474097.4 == round 900719925474097.4 -- False
truncate 900719925474097.4 == floor 900719925474097.4 -- Also Falseround and floorare both implemented directly as the functions in Math so they preserve the significand.
|
JS will faithfully represent integers up to 53 bits even if the engine opts to use ints instead of doubles internally to store them at 32 bits and below. As a result, other core functions that call into built-in Math stuff will work on larger values whereas things that are done by hand like truncate 900719925474097.4 == round 900719925474097.4 -- False
truncate 900719925474097.4 == floor 900719925474097.4 -- Also False
|
pisys commentedMay 10, 2016
Native Basics.truncate is implemented using a bitwise operator. The problem is that Javascript transforms the bit length from 64 to 32 bit for any number involved in bitwise operation.
ES6 offers Math.trunc which offers the same functionality but keeps the bit length. We should use this instead.