|
Hi Tomás, > Thanks for the explanation. But now I think that _all_ numbers would > have to stay in mpi format to ensure everything works. Since an > operation between two numbers could overflow, it would be wise to use > every number as an mpi-number. In other words, the ability to add > small integers like 1 or 137 without "converting" them could lead to > miscalculation, don't you think? Yes, basically, if you are doing large number calculations then all results need to be mpi objects, but constants used in the calculations can be written as simple integers. The mpi methods also accept Lua numbers as operands but generally return mpi objects as results. You could explicitly convert all constants and input operands to mpi objects, but then the code would be mpi-specific. I want to be able to write Lua code (say, in a library function) which is independent of whether the operands are big integers or Lua numbers. For example, I can write a function to calculate factorials like this: function factorial(n) local result = 1 while n > 1 do result = result * n n = n - 1 end return result end Now this function doesn't care whether you call it with a Lua integer or an mpi object; it works either way: factorial(5) [ 120 ] mpi = require "geneas.mpi" factorial(mpi(50)) [ 30414093201713378043612608166064768844377641568960512000000000000 ] This is a bit contrived but it illustrates what I'm trying to achieve. The only problem at the moment is the equality operator! -a On 10/06/2022 01:24, Tomás Guisasola wrote:
Hi AndrewI think you are saying that if a number fits in a Lua integer then the mpi module should simply return an integer.Yes.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.Sure.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.Thanks for the explanation. But now I think that _all_ numbers would have to stay in mpi format to ensure everything works. Since an operation between two numbers could overflow, it would be wise to use every number as an mpi-number. In other words, the ability to add small integers like 1 or 137 without "converting" them could lead to miscalculation, don't you think? Regards, Tomás