How to return very long integer in Lua - parsing

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.

Related

Lua Dot After Integer

I have a script that return a double value. When it return a integer value I need to add a dot after integer. Is there any clean way to do that.
Example
return 3/2 --> 1.5 (its ok )
return 8/2 --> 4 (its not ok. I need to print 4. (4 with dot). )
return 8/2--> 4. (This is what I want)
Ps. Im not a native english speaker. If I wrote sth funny sorry about that
return 1 + .0
will convert an integer 1 to a float 1.0
Usually you would divide by a float if you want a float result.
return 8/2.0
I don't see why you would want to print that number with a decimal point if there are no decimals. That doesn't make too much sense.
If you insist to do so you should use string.format to format your string.
if math.type(n) == "integer" then
print(string.format("%d.", n))
elseif math.type(n) == "float" then
print(string.format("%f", n))
end

Convert Table Elements to Integers

I'm trying to create a list of integers, similar to python where one would say
x = input("Enter String").split() # 1 2 3 5
x = list(map(int,x)) # Converts x = "1","2",3","5" to x = 1,2,3,5
Here's my code asking for the input, then splitting the input into a table, i need help converting the contents of the table to integers as they're being referenced later in a function, and i'm getting a string vs integer comparison error. I've tried changing the split for-loop to take a number but that doesn't work, I'm familiar with a python conversion but not with Lua so I'm looking for some guidance in converting my table or handling this better.
function main()
print("Hello Welcome the to Change Maker - LUA Edition")
print("Enter a series of change denominations, separated by spaces")
input = io.read()
deno = {}
for word in input:gmatch("%w+") do table.insert(deno,word) end
end
--Would This Work?:
--for num in input:gmatch("%d+") do table.insert(deno,num) end
Just convert your number-strings to numbers using tonumber
local number = tonumber("1")
So
for num in input:gmatch("%d+") do table.insert(deno,tonumber(num)) end
Should do the trick

Losing precision in Lua

I have a function in lua, which is given 2 vectors, return the lambda multiplier of first vector to second one, here is my code
function Math.vectorLambda( v1,v2 )
local v1Length,v2Length=math.sqrt(v1.x^2+v1.y^2),math.sqrt(v2.x^2+v2.y^2)
if v1Length==0 then
return nil
else
local dotProduct=v1.x*v2.x+v1.y*v2.y
print(dotProduct,v1Length,v2Length,math.abs(dotProduct)==(v1Length*v2Length))
if math.abs(dotProduct)==(v1Length*v2Length) then
if v1.x~=0 then
return v2.x/v1.x
else
return v2.y/v1.y
end
else
return nil
end
end
end
However, if
--this is what I get from terminal and I believe that it does not display the whole number--
v1={0.51449575542753,-0.85749292571254}
v2={-10,16.666666666667}
the output is
-19.436506316151 1 19.436506316151 false
which is saying the absolute value of dotProduct and v1Length*v2Length are not the same...
What is the reason for above, rather than I am blind? :(
BTW, the function is not stable..with exactly the same vectors, the function might has the same output except math.abs(dotProduct)==(v1Length*v2Length) gives true and hence return correct answer rather than nil, why?
Floats are tricky. You most likely have differences on the smaller decimal places (I don't know for sure, since I get true here). Try printing the numbers with a bigger precision, using a function like:
function prec_print(v, prec)
local format = '%.' .. prec .. 'f'
print(string.format(format, value))
end
In any case, you should almost never use == to compare floating point equality. For floats, it's quite easy to get false for simple things like a+b-b==a. What you should probably do is to check whether de difference of the two values is less than some threshold:
function almost_equal(float1, float2, threshold)
return math.abs(float1 - float2) <= threshold
end
But it's actually trickier than that (if, say, float1 and float2 are too far apart). Anyway, this read is mandatory for anyone working with floats: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Cheers!

Handling floats in redis

I need to use Redis in a project I am using and was wondering if there was anyway to do proper mathmatic operations and comparisons on floats using LUA scripts(or anyway really). For example, I have a field, and need to multiply it by another field, and compare it to a third field. For example
local staticVal = .2
local dynamicVal2 = redis.pcall('GET', 'dynamicVal2')
local calcVal = dynamicVal * staticVal
local compareVal = 100
if calcVal < compareVal then
return false
else
return true
Is there a possible way to do this, or do I have to just make the GET calls from another language and do the comparison there?
Thank you
Edit:
Or the ability to just compare floating point numbers would be helpful. It seems that a dictionary comparison is done rather than a numerical comparison.
Edit 2:
SET val1 10.5
SET val2 3.5
EVAL "local val1 = redis.pcall('GET','val1'); local val2 = redis.pcall('GET','val2'); if val1 > val2 then return val1 else return val2 end" 0
It seems that a dictionary comparison is done rather than a numerical comparison.
local val1 = redis.pcall('GET','val1');
local val2 = redis.pcall('GET','val2');
if val1 > val2 then ...
Check the type of val1 and val2 (e.g. print(type(val1))). My guess is that they are strings, which is why you're getting a lexical comparison rather than a numerical one.
Lua's native number type is floating point and it has no problem comparing them. If your values are indeed strings, you just need to convert them into numbers (e.g. tonumber(val1)) before comparing them.
Of course you can: in Lua, all the numbers are floats. It is actually more difficult to work with large integer values than with floats (due to the internal numbers representation).
From redis-cli:
set dynamicVal2 100000.0
eval "local staticVal = .2 ; local dynamicVal = tonumber(redis.call('GET', 'dynamicVal2')); local calcVal = dynamicVal * staticVal; local compareVal = 100; if calcVal < compareVal then return false; else return true; end;" 0
(integer) 1
Now using Lua for the example you gave is not that useful: what is done with Lua on server-side could be easily done on client-side with similar efficiency. And it is actually better to do it on client-side if you can. Like with many other data stores, the more you can do on client-side for the same number of roundtrips, the best it is.
It would have been more useful if the Lua script was effectively used to avoid multiple roundtrips to Redis.

Lua base converter

I need a base converter function for Lua. I need to convert from base 10 to base 2,3,4,5,6,7,8,9,10,11...36 how can i to this?
In the string to number direction, the function tonumber() takes an optional second argument that specifies the base to use, which may range from 2 to 36 with the obvious meaning for digits in bases greater than 10.
In the number to string direction, this can be done slightly more efficiently than Nikolaus's answer by something like this:
local floor,insert = math.floor, table.insert
function basen(n,b)
n = floor(n)
if not b or b == 10 then return tostring(n) end
local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local t = {}
local sign = ""
if n < 0 then
sign = "-"
n = -n
end
repeat
local d = (n % b) + 1
n = floor(n / b)
insert(t, 1, digits:sub(d,d))
until n == 0
return sign .. table.concat(t,"")
end
This creates fewer garbage strings to collect by using table.concat() instead of repeated calls to the string concatenation operator ... Although it makes little practical difference for strings this small, this idiom should be learned because otherwise building a buffer in a loop with the concatenation operator will actually tend to O(n2) performance while table.concat() has been designed to do substantially better.
There is an unanswered question as to whether it is more efficient to push the digits on a stack in the table t with calls to table.insert(t,1,digit), or to append them to the end with t[#t+1]=digit, followed by a call to string.reverse() to put the digits in the right order. I'll leave the benchmarking to the student. Note that although the code I pasted here does run and appears to get correct answers, there may other opportunities to tune it further.
For example, the common case of base 10 is culled off and handled with the built in tostring() function. But similar culls can be done for bases 8 and 16 which have conversion specifiers for string.format() ("%o" and "%x", respectively).
Also, neither Nikolaus's solution nor mine handle non-integers particularly well. I emphasize that here by forcing the value n to an integer with math.floor() at the beginning.
Correctly converting a general floating point value to any base (even base 10) is fraught with subtleties, which I leave as an exercise to the reader.
you can use a loop to convert an integer into a string containting the required base. for bases below 10 use the following code, if you need a base larger than that you need to add a line that mapps the result of x % base to a character (usign an array for example)
x = 1234
r = ""
base = 8
while x > 0 do
r = "" .. (x % base ) .. r
x = math.floor(x / base)
end
print( r );

Resources