function luaU.from_double(N,X)local function K(X)local N=X%256 return(X-N)/256,string.char(N)end local V=0 if X<0 then V=1 X=-X end local f,q=math.frexp(X)if X==0 then f,q=0,0 elseif X==1/0 then f,q=0,2047 else f=(f*2-1)*math.ldexp(.5,53)q=q+1022 end local s,A=""X=math.floor(f)for N=1,6,1 do X,A=K(X)s=s..A end X,A=K(q*16+X)s=s..A X,A=K(V*128+X)s=s..A return s end
-----------------------------------------------------------------------
-- converts a IEEE754 double number to an 8-byte little-endian string
-- * luaU:from_double() and luaU:from_int() are adapted from ChunkBake
-- * supports +/- Infinity, but not denormals or NaNs
-----------------------------------------------------------------------
function luaU:from_double(x)
	local function grab_byte(v)
		local c = v % 256
		return (v - c) / 256, string.char(c)
	end
	local sign = 0
	if x < 0 then
		sign = 1
		x = -x
	end
	local mantissa, exponent = math.frexp(x)
	if x == 0 then -- zero
		mantissa, exponent = 0, 0
	elseif x == 1 / 0 then
		mantissa, exponent = 0, 2047
	else
		mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, 53)
		exponent = exponent + 1022
	end
	local v, byte = "" -- convert to bytes
	x = math.floor(mantissa)
	for i = 1, 6 do
		x, byte = grab_byte(x)
		v = v .. byte -- 47:0
	end
	x, byte = grab_byte(exponent * 16 + x)
	v = v .. byte -- 55:48
	x, byte = grab_byte(sign * 128 + x)
	v = v .. byte -- 63:56
	return v
end
