Code: Select all
math.cos(math.rad(90))
I thought cos(90 deg) was 0. My calculator IRL and google also gives me 0.
Code: Select all
math.cos(math.rad(90))
Code: Select all
return ('%0.19f'):format(math.cos(math.pi/2)) -- same as if you pass in math.rad(90)
> 0.0000000000000000612
This is exactly the reason. The value returned is the cosine of that approximation. Something similar happens with math.sin(math.pi).
Nevermind that math.cos function itself only returns approximate cosine values (and of not particularly great accuracy, FYI).
Apparently that happens on LuaJIT only. The closest number to Pi/2 that can be exactly represented in double precision floating point is:
Code: Select all
$ lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> =("%.17g"):format(math.cos(math.pi/2))
6.123233995736766e-17
Code: Select all
$ luajit
LuaJIT 2.0.4 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/
JIT: ON CMOV SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse
> =("%.17g"):format(math.cos(math.pi/2))
6.1230317691118863e-17
Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest