How to use numbers greater than 2^53

I need to implement big numbers (greater than 2^53)

in my game. But lua looses precision after numbers greater

than 2^53. I cannot seem to find any solution to this.

So how do I use greater than this in my game??
 

If you want to have bigger numbers, you can split the value. The way you use your huge value depend of what you need.

For example:

firstHalf=math.floor(myNumber/2^54) mySecondOne=myNumber-firstHalf\*2^54

Can you explain why you need this very big number?

If you have only big value you can simply save in you value 10^-6 of the real value.

If you just need to do something like absurdly high scores à la Giga Wing and friends, then just build them up piecewise, along the lines of what  remiduchalard recommends.

There are a few big number libraries, but in large part they’ll require compiling C and have licenses like GPL. One of Lua’s authors has a few bindings here and offers some commentary both here and here.

As far as pure Lua goes, this sounds like it requires a newer Lua (5.3), but maybe I’m wrong. I also saw this one, which seems to be early enough to be 5.1.

I don’t know anything about these but also discovered them while searching.

I am trying to make a game like AdVenture Capitalist which uses money values much bigger than 100 trillion. I need money to take values upwards of 100 trillion. After that lua uses the “e” to display numbers like 1.2e14. But I need these values to display in normal notation not scientific. I used string.format(’%.f’) to do this but it looses its precision after values greater than I think 100 trillion. 

I tried using bignum but it throws multiple errors and when it comes to lbc I am not able to implement it. 

Moreover, I came to know that lua stores numbers as only

floating point in 5.1(version of Lua which is the version Corona sdk uses) 

as doubles which loose precision after values greater than 2^53.

I was going to guess this is for a ‘clicker’, but the OP already answered.  Dang… Too slow.

PS - You keep spelling the word lose incorrectly as loose.  Just a small nit, but worth knowing.

https://www.grammarly.com/blog/loose-lose/

For your type of game, do you need to go for 1 to 100 trillion?

If you go from 1K to 100 trillion, it’s more reachable. What is your smaller value?

If you want easily huge number you can do something like I mention before and if you want to save it entirely in one value, save it in a string a not a number. In a string you can have number totally insane. And you split the string.

Take the last ten char for the ten first digit (1 to 9 billion) and then the 10 next char will be you ten next digit. And to compare huge value you will find an easy way (have a look on how in ASM compare some number)

Example:

myHugeNumber="123 456 789 012 345 678"-- with no space, I put space to be more readable myFirstPart=take the first 10 digit with some lua function ( http://lua-users.org/wiki/StringLibraryTutorial) mySecondPart=... How to compare: YourOwnFunction()

I’m curious just how much precision you really need. There is a saying:

“Measure with a micrometer. Mark with chalk. Cut with an Axe.”  Which basically says if you’re going to be cutting with an axe, do you really need to measure it with something on the scale of sub-millimeters?

Translated to this if you’re dealing with trillions of dollars, does 12 cents really matter? Does $12 really matter?

Rob

You could store values in terms of units.

Quadrillions - 64
Trillions - 987
Billions - 364
Millions - 912
Balance - 912988

Add values to balance, if that overflows 1m, add 1m to millions, reduce balance by 1m. When millions goes past 1000, add one to billion and so.

hehe :slight_smile: noted

money = 123456789012345678 --Read 123 456 789 012 345 6 78 (no spaces) print(string.format('%.f',money)) --\> Displays 1234567890123456 80 -- notice last two digits

The problem is after 16 digits. The stored value of money would be different than the one displayed

on screen to the player.

Actually this can be done but the problem would come when displaying those values to the player.

The way floating point works, the range spreads out more and more as you go up past successive powers of 2. After 2^53, you can only represent 1 / 2^53 of the range from 2^53 to 2^54, which is only enough to hit even integers (and no fractional values in between), but you’d already be down to 64ths of an integer around 2^45. (Some of this might be off by one, but the point stands.) This is also why you have such fantastic accuracy from -1 to 1, along with denormals.

This actually is important in financial transactions and various formats like binary-coded decimal have been introduced as alternatives to the more scientist- and engineer-oriented floating points.

I think you’d be fine adopting something like nick_sherman suggests. If you need to multiply just break it down like you’d do it by hand. Displaying shouldn’t be an issue; just build the string up.

I need to use values much greater than trillions.

My game is a clicker, so I have a few autoclickers in my game which buy/sell items and deduct/add changes to the player money variable which is then displayed on screen. 

The only problem is that the money variable would differ from what is presented to the player and some other issues while doing

arithmetic.

I was talking of this kind of string function: 

> = string.sub(“Hello Lua user”, 7) – from character 7 until the end
Lua user
> = string.sub(“Hello Lua user”, 7, 9) – from character 7 until and including 9
Lua
> = string.sub(“Hello Lua user”, -8) – 8 from the end until the end
Lua user
> = string.sub(“Hello Lua user”, -8, 9) – 8 from the end until 9 from the start
Lua
> = string.sub(“Hello Lua user”, -8, -6) – 8 from the end until 6 from the end 

Like this you have your number in one unique value and you need a little operation to update it or compare it

Don’t use string format, just do:

[lua]

print (Quadrillions…","…Trillions…","…Billions…","…Millions…","…Balance)

[/lua]

Although you’d need a function to add a comma to Balance.

Yeah displaying would not be an issue but I am not sure about arithmetic like when buying items or selling items etc.

Items will also have their values stored like this. Add/Subtract the values from each unit to/from each other. 

If adding, work from Balance to Quadrillions, checking if there is an overflow and if so adding to the next unit and subtracting from the current one. If subtracting, work the opposite way round.

Ohh… this can work I didnt came across it earlier. Will surely try it. :slight_smile: