Efficient Round Function #208
Replies: 4 comments 1 reply
-
That's interesting I don't really understand it :( I pulled this one off a forum somewhere, years ago:
but I got to thinking and came up with this: Does it work?
|
Beta Was this translation helpful? Give feedback.
-
This is the explanation from https://stackoverflow.com/questions/18313171/lua-rounding-numbers-and-then-truncate It takes advantage of the limits of double precision numbers and FPU rounding. Adding and subtracting 2^52 to any number between 0 and 2^52-1 rounds it by pushing it to the range 2^52..2^53, where the double precision floats lose all their decimals, before adjusting back. To cover negatives, I added 2^51 in order to ensure the intermediate result is still in the range 2^52..2^53. |
Beta Was this translation helpful? Give feedback.
-
Or on that page the even more succinct
and another one on that page similar to the one I came up with using lua's ternary operatory emulation
|
Beta Was this translation helpful? Give feedback.
-
I do 't think they work in all cases. 0.49 for example returns 1. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
There is no math.round function.
I found this nice little round function that only uses addition
Beta Was this translation helpful? Give feedback.
All reactions