• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Floating numbers print (1 Viewer)

Felecarp

Newcomer
Joined
Jul 26, 2022
Messages
2
Reaction score
0
Lua:
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
JIT: ON SSE2 SSE3 SSE4.1 AMD fold cse dce fwd dse narrow loop abc sink fuse
> print(.4 * 384)
153.6
> print(.4 * 384 == 153.6)
false
>
Python:
Python 3.10.5 (main, Jun  8 2022, 02:00:39) [GCC 10.2.1 20201203] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> .4 * 384
153.60000000000002
>>>
Is it a way in lua to do not see this oddity using print ?
 

Felecarp

Newcomer
Joined
Jul 26, 2022
Messages
2
Reaction score
0
Another example
Lua:
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
JIT: ON SSE2 SSE3 SSE4.1 AMD fold cse dce fwd dse narrow loop abc sink fuse
> print(298+2/3)
298.66666666667
> print(128+4/9*384)
298.66666666667
> print(298+2/3 == 128+4/9*384)
false
>
Python:
Python 3.10.5 (main, Jun  8 2022, 02:00:39) [GCC 10.2.1 20201203] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 298 + 2/3
298.6666666666667
>>> 128 + 4/9 * 384
298.66666666666663
>>>
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
79
Reaction score
10
Try using string.format to see what's going on, in my case I did:
Lua:
print(string.format("%.20f", 298+2/3))
print(string.format("%.20f", 128+4/9*384))
print(298+2/3 == 128+4/9*384)
And got:
Code:
298.66666666666668561447
298.66666666666662877105
false
 
Top