lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Tomás,

I think you are saying that if a number fits in a Lua integer then the
mpi module should simply return an integer.

But now what happens when we multiply two numbers which both fit in a
Lua integer but the product does not.

mpi(1000000000000000) * mpi(1000000000000000) -> ?

If the two operands were converted to Lua integers then the result would
just overflow, because Lua doesn't know that they are supposed to be big
integers! That would defeat the whole object of the mpi module.

Of course my example of mpi(1) == 1 is trivial. In general we might have
a complex big integer calculation which we are comparing with an integer
constant, eg:

local a = <some mpi calculation>
if (a == 137)
...

So the numbers have to stay in mpi format even if they would fit into a
Lua integer.

-a


On 09/06/2022 20:46, Tomás Guisasola wrote:
Hi Eduardo and Andrew

But there's a problem when comparing for equality, because the __eq
metamethod is only called when _both_ operands are tables or userdata!

if a == 1 then          -- metamethod __eq
         print "true"
else
         print "false"
end
[ false ]

What I didn't understand is why you have to represent the number 1 as
a table.  In other words, why don't you make the result of `mpi(1)` be
the number 1 ?

Regards,
Tomás