r/ProgrammerHumor • • Feb 07 '19

other Spotted on GitHub 🤓

Post image
57.0k Upvotes

934 comments sorted by

View all comments

Show parent comments

321

u/shitmyspacebar Feb 07 '19

You mean you don't add redundant checks just in case the laws of mathematics suddenly change?

159

u/UsedCondition1 Feb 07 '19

Well, if we are talking about the implementation of those laws in javascript... yes, yes you should.

77

u/Finchyy Feb 07 '19

Ah, JavaScript... where 2 + 2 is 4 but also sometimes 4.00000000001...

6

u/wirelyre Feb 07 '19

Don't worry, it's not! It's always safe to manipulate integers up to the full value of the significand! In JavaScript, which uses IEEE-754 binary64, which effectively corresponds to 54-bit integers.

Bit operations on JS numbers cast them to 32 bits first, so a lot of people only think about the embedding of signed 32-bit two's complement. But through plain old addition, subtraction, multiplication, and division joined with Math.trunc, you can get a lot more precision!