[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [5.4] The string metatable and arbitrary-precision arithmetic
- From: Dirk Laurie <dirk.laurie@...>
- Date: Tue, 26 Jun 2018 15:30:44 +0200
There is only one incompatibility listed in the Lua 5.4 (work2) manuall.
> The coercion of strings to numbers in arithmetic and bitwise operations has been removed from the core language. The string library does a similar job for arithmetic (but not for bitwise) operations using the string metamethods. However, unlike in previous versions, the new implementation preserves the implicit type of the numeral in the string. For instance, the result of "1" + "2" now is an integer, not a float.
If backward compatibility of coercion arithmetic is not desired, these
metamethods could be replaed to give arbitrary-precision arithmetic.
For example:
$ sudo luarocks install lbc
$ lua5.4 -l bc
Lua 5.4.0 Copyright (C) 1994-2017 Lua.org, PUC-Rio
meta = getmetatable''
for k,v in pairs(bc) do
if k~='__index' then meta[k] = bc[k] end
end
function fermat(n)
if n==0 then return '3'
else return (fermat(n-1)-1)^2+1
end
end
fermat(7)
340282366920938463463374607431768211457