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
    if n == inf or n == -inf then
        return n,nan
    local logn
    if n < 0 then
        logn = log(-n)
        logn = log(n)
    local exp = ceil((logn+2^-52)/log2)
    return n/2^exp, exp

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 

tom <>