-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfrexp.lua
More file actions
24 lines (22 loc) · 775 Bytes
/
frexp.lua
File metadata and controls
24 lines (22 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- math.frexp() replacement for Lua 5.3 when compiled without LUA_COMPAT_MATHLIB.
-- The C approach is just to type-pun the float, but we can't do that here short
-- of stupid loadstring() tricks, which would be both architecture and version
-- dependent and a maintenance headache at best. So instead we use math.
local abs,floor,log = math.abs,math.floor,math.log
local log2 = log(2)
return function(x)
if x == 0 then return 0.0,0.0 end
local e = floor(log(abs(x)) / log2)
if e > 0 then
-- Why not x / 2^e? Because for large-but-still-legal values of e this
-- ends up rounding to inf and the wheels come off.
x = x * 2^-e
else
x = x / 2^e
end
-- Normalize to the range [0.5,1)
if abs(x) >= 1.0 then
x,e = x/2,e+1
end
return x,e
end