3 ms·
Understand how computers handle different data types and what their min/max values are. Someone sent me the following code today (in Javascript): ord=Math.ran
by terryjsmith 16y ago
Understand how computers handle different data types and what their min/max values are. Someone sent me the following code today (in Javascript):
ord=Math.random()*10000000000000000;
A) It's unnecessary and will get "trimmed" down to 2147483647 on 32-bit systems.
B) Clearly the programmer that wrote this has absolutely no idea that his number is going to require a 64-bit long int (on applicable systems) just to generate a random number to make their URL non-cached. What a waste.
</minor rant>
- Erwin 16y agoThe result of this is a 64-bit double (there are no other numeric data types in JS and this is the data type regardless of the underlying CPU architecture). That has just under 16 digits of precision so the above code will not lose much by multiplying by 10^16 and rounding. Why would you think this this calculation would get clamped to the range of a signed 32-bit integer?