Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Riddle me this? Math is doing stupid things...

Asked by 5 years ago

So I was doing some work with some oddly specific numbers, and found this:

While the simple:

print(0.15 - 0.15)

equals '0', this:

print(0.15000000596046 - 0.15) 

equals '5.9604600144425e-09' for some reason. It is messing up my script, and driving me crazy. Any ideas?

0
That's how computers work. The further away from 0, the less accurate. Amiaa16 3227 — 5y
0
The outcome of the second equation is correct. It's called scientific notation. ThatPreston 354 — 5y
0
use math.floor() or math.ceil() math.floor rounds the number down and math.ceil rounds the number up Elixcore 1337 — 5y
0
Do you want to know how to disable e-notation? DeceptiveCaster 3761 — 5y
0
I already asked that question, search questions simular to yours before asking User#20388 0 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

5.9604600144425e-09 equals 5.9604600144425*10^-9. It's called scientific notation, and is used for dealing with really small/large numbers.

I can't think of any solution other than just not using 0.15000000596046.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You can remove that e-notation. Watch this:

local str = "0.15000000596046"
local editedstr = string.format(".0f", str)
local num = tonumber(editedstr)
print(num - 0.15) 

0
% -- 18 (the % part goes directly next to 18.) DeceptiveCaster 3761 — 5y
0
in line 2 DeceptiveCaster 3761 — 5y

Answer this question