How to preserve decimals when doing divisons with very large numbers?

Hi all,

I am trying to solve a math issue in Corona which I have not been able to do so far - maybe you can help.

Here is the problem: I need to do fractions where the denumerator can be very, very large (e.g. 1234500000 / 4756). I need the result of the division to be precise which means that the result of the division needs to show decimals (if any). However, in the example above, Corona handles the division as follows 1.235E+09 / 4756. If I print the result of the division to the console I will not see any decimals. I guess this is because I am using quite large denumerators where Corona just rounds the result.

However, I really need to see the decimals. Is there any way this can be solved? Is there something like “BigInteger”? I am really stuck here.

Many thanks,
Jens [import]uid: 101883 topic_id: 23397 reply_id: 323397[/import]

I tried your example and I’m actually getting:

1234500000 / 4756 = 259566.86291001

Was this a smaller example than what you are using?

You have to keep in mind that when dealing with floating point numbers, that they don’t have unlimited precision. If I’m not mistaken, LUA handles everything as 64bit, so a 64bit floating point number will be able to store around 16 base10 digits. If you have a really big integer part, the precision will not be enough to show any decimal part. If the number is big enough, even some of the integer part will be lost. [import]uid: 61899 topic_id: 23397 reply_id: 93740[/import]

Yes, the example is smaller than what I am actually using. The enumerator I would be using could look like this:

enumerator = (2*3*67*97*31*29*37*5*61*67*97*101*23*17*67*11*19*41*2*…)

There could be as much as 49 multiplications (using primes) so the enumerator could get quite big. Later on in the game the enumerator will become significantly smaller but it will be very large in the beginning and I need to take care of that.

I saw some biginteger classes for lua when searching the internet - maybe I need to try one of these. I just hoped there was an easier way in Corona to preserve decimals when doing some really huge calculations.

Best,
Jens [import]uid: 101883 topic_id: 23397 reply_id: 93744[/import]

It doesn’t depend on Corona. It’s a Lua limitation. But to be honest Lua is already very precise as it uses 64bit numbers by default.

The thing about floating point numbers is that they are able to represent numbers in a very wide range, from really big numbers to really small numbers, but there are some limitations.

For starters you can’t really represent an arbitrarily big or small number, because the exponent is represented in 11 bits, so you can have 2048 different ones, which means going from -1023 to 1024. Considering some reserved values, it’s actualy -1022 to 1023.

Then, you have 52 bits to represent the digits of the number. This is roughly, as I said before, 16 digits in base 10. So if your number is bigger than around… 1000000000000000, you won’t have enough precision to represent any decimal place.

To overcome this limitation you have to use other libraries that allow the use of bigger numbers, but bare in mind that arithmetic operations on this libraries are orders of magnitude more expensive than regular ones.

One developed by one of Lua’s developers: http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lmapm

Manuel [import]uid: 61899 topic_id: 23397 reply_id: 93747[/import]

Thanks Manuel! [import]uid: 101883 topic_id: 23397 reply_id: 93758[/import]