2
print((1 / 10 - 0.1) == 0) -- true
print((1 * 0.1 - 0.1) == 0) -- true

print((3 / 10 - 0.3) == 0) -- true
print((3 * 0.1 - 0.3) == 0) -- false

the last one is false, it means is not the 0.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    If you wrap 3 * 0.1 in brackets does it work? In the second example, it could be doing 1 * 0 if in the event that lua evaluates + 1 before it does * / – Jay Killeen Aug 09 '18 at 04:13
  • what will be showed with print(3 * 0.1 - 0.3) and print((3 * 0.1) - 0.3) ? – A. Denis Aug 09 '18 at 07:11
  • 2
    Floating point operations does not have infinite precision. Consider reading this guide to get a better understanding: https://floating-point-gui.de/ – Moberg Aug 09 '18 at 07:34
  • @JayKilleen Lus definitely abides by the general rules of operation precedence (`*/` is evaluated before `+-`). – Moberg Aug 09 '18 at 07:35
  • i read this guid but, i dont know why the '/' divided result is correct, the '*' multipy result is the false. any guide ? thank you – Goudan Yuan Aug 09 '18 at 12:45
  • 1
    @GoudanYuan - `3 / 10 - 0.3` is always zero because by default `0.3 == approx(3/10)` and `3 / 10 - 0.3 == approx(3/10) - approx(3/10) == 0`. On the other side, `3 * 0.1 - 0.3 == 3 * approx(1/10) - approx(3/10)`, so it may be a non-zero. – Egor Skriptunoff Aug 09 '18 at 13:09
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Luatic Oct 19 '22 at 09:14

1 Answers1

2
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> function a(x) return ("%A"):format(x) end
> a(0.1)
0X1.999999999999AP-4
> a(0.3)
0X1.3333333333333P-2
> a(0.1 * 3)
0X1.3333333333334P-2

They are different due to rounding errors.

More details:

0.1 =
0X1.9999999999999999999... * 2^(-4) =
(rounding)
0X1.999999999999A * 2^(-4) =
0X1.999999999999AP-4

0.3 =
0X1.333333333333333333... * 2^(-2) =
(rounding)
0X1.3333333333333 * 2^(-2) =
0X1.3333333333333P-2   

0.1 * 3 =
0X1.999999999999AP-4 * 3 =
0X1.999999999999A * 2^(-4) * 3 =
0X4.CCCCCCCCCCCCE * 2^(-4) =
0X1.33333333333338 * 2^(-2) =
(rounding)
0X1.3333333333334  * 2^(-2) =
0X1.3333333333334P-2
Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23