lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Friday, May 08, 2015 09:19:36 AM Daurnimator wrote:
> 
> math.frexp is deprecated in lua 5.3.

local ceil = math.ceil
local log = math.log
local log2 = log(2)
local nan = 0/0
local inf = 1/0

function frexp(n)
    if n == 0 or n ~= n then
        return n,n
    end
    if n == inf or n == -inf then
        return n,nan
    end
    local logn
    if n < 0 then
        logn = log(-n)
    else
        logn = log(n)
    end    
    local exp = ceil((logn+2^-52)/log2)
    return n/2^exp, exp
end

I'm not sure about the 2^-52 hack. The alternative is to test if mantissa == 1 
and increment the exponent. 

The other way is to string.pack/unpack from double to integer then twiddle the 
bits how most C libraries are implemented. Or the reliable but slow loop and 
divide.

-- 
tom <telliamed@whoopdedo.org>