문제

I am trying to return very long integer number but my result returns as "7.6561197971049e+016". How do I make it return 76561197971049296 ?

local id64 = 76561197960265728
Z = string.match("STEAM_0:0:5391784", 'STEAM_%d+:%d+:(%d+)')
Y = string.match("STEAM_0:0:5391784", 'STEAM_%d+:(%d+):%d+')
--For 64-bit systems
--Let X, Y and Z constants be defined by the SteamID: STEAM_X:Y:Z.
--Let V be SteamID64 identifier of the account type (0x0110000100000000 in hexadecimal format).
--Using the formula W=Z*2+V+Y
if Z == nil then
    return "none"
else
    return Z*2+id64+Y
end

I installed lbc arbitrary precision now with this code

return  bc.add(bc.number(id64),bc.number(2)):tostring()

it returns 70000000000000002 but if I delete 3 digits from id64 it displays correctly.

How can I get correct result without deleting the digits?

도움이 되었습니까?

해결책

You need to use strings for long numbers. Otherwise, the Lua lexer converts them to doubles and loses precision in this case. Here is code using my lbc:

local bc=require"bc"
local id64=bc.number"76561197960265728"
local Y,Z=string.match("STEAM_0:0:5391784",'STEAM_%d+:(%d+):(%d+)')
if Z == nil then
    return "none"
else
    return (Z*2+id64+Y):tostring()
end

다른 팁

check out this library for arbitrary precision arithmetics. this so post might be of interest to you as well.

Assuming your implementation of Lua supports that many significant digits in the number type, your return statement is returning that result.

You're probably seeing exponential notation when you convert the number to a string or printing it. You can use the string.format function to control the conversion:

assert( "76561197971049296" == string.format("%0.17g", 76561197971049296))

If number is an IEEE-754 double, then it doesn't work. You do have to know how your Lua is implemented and keep in mind the the technical limitations.

If you have luajit installed, you can do this:

local ffi = require("ffi")

steamid64 = tostring(ffi.new("uint64_t", 76561197960265728) + ffi.new("uint64_t", tonumber(accountid)))
steamid64 = string.sub(steamid64, 1, -4) -- to remove 'ULL at the end'

Hope it helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top