Bobert13
Prince
- Joined
- Feb 25, 2013
- Messages
- 346
I need a fast method for truncating floats to exactly 7 decimal places. All of the numbers I'll be dealing with will be between 0.0 and 1.0 if that makes this any easier.
The problem I'm having is whenever multiplying two double precision floats during my script, the result is inconsistent. I can literally perform the exact same multiplication 20 times and get 3 different results! I have no idea why nor how this is happening, though it occurs whether I'm using the default lua51_win32.dll or the luaJIT.dll. To get around this, I've decided to track down and truncate every number before multiplying it by another number. I've tried two different methods:
This method is fast, but it leaves numbers in the lower insignificant decimal places. The insignificant digits are consistent; however, the product of multiplying two numbers that have been truncated in this way is not consistent!! It's putting numbers in the insignificant digits because there is no way to represent many decimal numbers in a floating point (0.1 for example [or 1/10] ends up being 0.1000000000000##### in a double precision float).
This method prints out that it's working perfectly. I haven't fully confirmed whether or not the product of two numbers truncated in this way are consistent. It puts no numbers in the insignificant digits. However, it's ridiculously slow. Implementing this in two places in my script doubles the amount of time it takes to run. I'm probably going to have to use some form of truncation in hundreds of places throughout my script so this is unacceptable.
If anybody has any insight on the matter, a better truncation method, or a way I can change the problem so that I'm not running into this issue, I would highly appreciate your help on the matter.
The problem I'm having is whenever multiplying two double precision floats during my script, the result is inconsistent. I can literally perform the exact same multiplication 20 times and get 3 different results! I have no idea why nor how this is happening, though it occurs whether I'm using the default lua51_win32.dll or the luaJIT.dll. To get around this, I've decided to track down and truncate every number before multiplying it by another number. I've tried two different methods:
Code:
math.floor(number * 10^decimals)/ 10^decimals
This method is fast, but it leaves numbers in the lower insignificant decimal places. The insignificant digits are consistent; however, the product of multiplying two numbers that have been truncated in this way is not consistent!! It's putting numbers in the insignificant digits because there is no way to represent many decimal numbers in a floating point (0.1 for example [or 1/10] ends up being 0.1000000000000##### in a double precision float).
Code:
tonumber(string.format("%.7f",number))
This method prints out that it's working perfectly. I haven't fully confirmed whether or not the product of two numbers truncated in this way are consistent. It puts no numbers in the insignificant digits. However, it's ridiculously slow. Implementing this in two places in my script doubles the amount of time it takes to run. I'm probably going to have to use some form of truncation in hundreds of places throughout my script so this is unacceptable.
If anybody has any insight on the matter, a better truncation method, or a way I can change the problem so that I'm not running into this issue, I would highly appreciate your help on the matter.