# How to use numbers greater than 2^53

**URL:** <https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945>\
**Category:** Lua Language\
**Created:** [June 26, 2018, 8:45am UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945 "2018-06-26T08:45:36Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![xDRock](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/x/d6d6ee/32.png) [@xDRock](https://forums.solar2d.com/u/xDRock)\
**Post date:** [June 26, 2018, 4:50pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/21 "2018-06-26T16:50:04Z")

</div>

> [@nick\\\_sherman](#):
>
> Items will also have their values stored like this. Add/Subtract the values from each unit to each other.&nbsp;
> 
> 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.

Yes I could do that.

Btw Lua has an example function called comma\_value to add commas after thousands. I would need to modify that function.

The function is -\>

```
function comma\_value(amount) local formatted = string.format('%.f',amount) while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if (k==0) then break end end return formatted end

```

---

<div class="post-metadata">

**Author:** ![xDRock](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/x/d6d6ee/32.png) [@xDRock](https://forums.solar2d.com/u/xDRock)\
**Post date:** [June 26, 2018, 5:02pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/22 "2018-06-26T17:02:38Z")

</div>

Thank you guys, I will try implementing both&nbsp;&nbsp;[nick\_sherman](https://forums.coronalabs.com/user/308271-nick-sherman/)&nbsp;and

[remiduchalard](https://forums.coronalabs.com/user/485069-remiduchalard/)’s&nbsp;&nbsp;approaches.&nbsp;

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [June 26, 2018, 5:09pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/23 "2018-06-26T17:09:05Z")

</div>

Hey guys.&nbsp; This is a fascinating discussion.

When an answer is arrived at that works, would someone please post the code back here?

I think a working solution that folks could study and run would really be the cherry on top of this sweet discussion.

---

<div class="post-metadata">

**Author:** ![xDRock](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/x/d6d6ee/32.png) [@xDRock](https://forums.solar2d.com/u/xDRock)\
**Post date:** [June 26, 2018, 5:12pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/24 "2018-06-26T17:12:49Z")

</div>

> [@roaminggamer](#):
>
> Hey guys.&nbsp; This is a fascinating discussion.
> 
> When an answer is arrived at that works, would someone please post the code back here?
> 
> I think a working solution that folks could study and run would really be the cherry on top of this sweet discussion.

Yes I will post the code when I find the best solution.

But just off my head I think&nbsp;[nick\_sherman](https://forums.coronalabs.com/user/308271-nick-sherman/)&nbsp;method might be better because it would be much easier to implement.

---

<div class="post-metadata">

**Author:** ![naveen\_pcs](https://forums.solar2d.com/user_avatar/forums.solar2d.com/naveen_pcs/32/224_2.png) [@naveen\_pcs](https://forums.solar2d.com/u/naveen_pcs)\
**Post date:** [July 5, 2018, 2:57pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/25 "2018-07-05T14:57:51Z")

</div>

Any update on this? I currently have a clicker/incremental game that’s been out for a while, and I’m slowly approaching the number limit. Looking for a drop in solution to extend the current way numbers are handled and “just work” like numbers usually would.

If you think you can do this, I’m willing to pay $$$ for a drop in solution. You can PM me if you’d like to talk in private.

Thanks!

---

<div class="post-metadata">

**Author:** ![remiduchalard](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@remiduchalard](https://forums.solar2d.com/u/remiduchalard)\
**Post date:** [July 6, 2018, 12:43pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/26 "2018-07-06T12:43:01Z")

</div>

Hello @naveen_pcs

There is many solution, I have write down one but without testing.

```
local myMonney="10000000000" -- I assume all number began by not a 0 local function myStringNumbertoNumber(myNumber) local theNumber={} local i=0 while(i\*30\<string.len(myNumber)) do theNumber[i]=tonumber(string.sub(myNumber,i\*30,(i+1)\*30)) i=i+1 end return theNumber end local function myNumberToMyString(myNumber) local theNumber="" for(i=0,#myNumber)do theNumber=theNumber..myNumber[i] end return theNumber end local function compareLargeNumber(myNumber,myMonney) local theMyNumber=myStringNumbertoNumber(myNumber) local theMyMonney=myStringNumbertoNumber(myMonney) local result=0 -- 0 for equal, 1 for \>,2 for \< if(#theMyNumber\>#theMyMonney) then result=1 else if(#theMyNumber\<#theMyMonney) then result=2 else local i=0 while(i\<#theMyMonney) do if(theMyNumber[i]\>theMyMonney[i])then result=1 i=100 -- large number else if(theMyNumber[i]\<theMyMonney[i])then result=2 i=100 --large number else i=i+1 end end end return result end local function addOrMinus(myNumber,myMonney,addOrMinus)--addOrMinus is true for add and false to minus local theMyNumber=myStringNumbertoNumber(myNumber) local theMyMonney=myStringNumbertoNumber(myMonney) -- continue like this, I think you have understood the logic behind end
```

---

<div class="post-metadata">

**Author:** ![davebollinger](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/d/e79b87/32.png) [@davebollinger](https://forums.solar2d.com/u/davebollinger)\
**Post date:** [July 6, 2018, 1:48pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/27 "2018-07-06T13:48:34Z")

</div>

> In my game. But lua looses precision after numbers greater than 2^53.

It’s not Lua at fault, it’s IEEE754 doubles.

But what’s the real issue - just output fomatting or actual internal precision?

I’ll tell you that the vast majority of incremental games out there (including ones that I’ve worked on) just use IEEE754 doubles and get away with it just fine.&nbsp; Because they’re NOT trying to add 1 to 1E33, for example.

That is, it’s the nature of the game that in order to get a ridiculously large “bank”, you need correspondingly ridiculously large “producers”.&nbsp; So all the math is always happening “within range” of what double precision is capable of.

Yes, you’d start losing “pennies” eventually, as you run out of bits to represent them, but who cares?! Most of these games, once beyond a certain point (usually about 1e9) start using some sort of modified scientific notation and maybe give you four or five decimals at most (fe 1.2345De for decillions, rarely would you see all 33 digits printed)

Consider:&nbsp; If your balance is in the 1e33 range, and you THINK you need to be worried about some “producer” making $1/second, … it would take something like 1e12 years before it even affected the 15th decimal place!&nbsp; Few of your players will live that long.

---

<div class="post-metadata">

**Author:** ![sidermaniac](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sidermaniac](https://forums.solar2d.com/u/sidermaniac)\
**Post date:** [July 7, 2018, 6:06am UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/28 "2018-07-07T06:06:03Z")

</div>

(As nick sherman said) How about using a table like so:&nbsp;

```
local money = {ones=0, hundreds=0, thousands=0, millions=0, billions=0, trillions=0, quadrillions=0}

```

Then you only need a string building function to parse the table and print the proper 0’s and commas.

---

<div class="post-metadata">

**Author:** ![nick\_sherman](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/n/958977/32.png) [@nick\_sherman](https://forums.solar2d.com/u/nick_sherman)\
**Post date:** [July 7, 2018, 6:47am UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/29 "2018-07-07T06:47:36Z")

</div>

If only someone had already suggested that 😉

---

<div class="post-metadata">

**Author:** ![sidermaniac](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sidermaniac](https://forums.solar2d.com/u/sidermaniac)\
**Post date:** [July 7, 2018, 7:37am UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/30 "2018-07-07T07:37:53Z")

</div>

Whoops sorry. You did already suggest that.&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![remiduchalard](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@remiduchalard](https://forums.solar2d.com/u/remiduchalard)\
**Post date:** [June 26, 2018, 3:01pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/31 "2018-06-26T15:01:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [June 26, 2018, 3:29pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/32 "2018-06-26T15:29:58Z")

</div>

If you just need to do something like absurdly high scores&nbsp;à la Giga Wing and friends, then just build them up piecewise, along the lines of what&nbsp; **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&nbsp;[here](http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/) and offers some commentary both&nbsp;[here](http://lua-users.org/lists/lua-l/2013-12/msg00277.html) and [here](https://stackoverflow.com/questions/288707/what-is-the-standard-or-best-supported-big-number-arbitrary-precision-librar).

As far as pure Lua goes,&nbsp;[this](https://github.com/user-none/lua-nums) sounds like it requires a newer Lua (5.3), but maybe I’m wrong. I also saw [this one](http://lua-users.org/lists/lua-l/2010-04/msg00607.html), which seems to be early enough to be 5.1.

I don’t know anything about [these](http://oss.digirati.com.br/luabignum/) but also discovered them while searching.

---

<div class="post-metadata">

**Author:** ![xDRock](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/x/d6d6ee/32.png) [@xDRock](https://forums.solar2d.com/u/xDRock)\
**Post date:** [June 26, 2018, 3:39pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/33 "2018-06-26T15:39:58Z")

</div>

> [@remiduchalard](#):
>
> 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.

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.&nbsp;

---

<div class="post-metadata">

**Author:** ![xDRock](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/x/d6d6ee/32.png) [@xDRock](https://forums.solar2d.com/u/xDRock)\
**Post date:** [June 26, 2018, 3:44pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/34 "2018-06-26T15:44:18Z")

</div>

> [@StarCrunch](#):
>
> If you just need to do something like absurdly high scores&nbsp;à la Giga Wing and friends, then just build them up piecewise, along the lines of what&nbsp; **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&nbsp;[here](http://webserver2.tecgraf.puc-rio.br/~lhf/ftp/lua/) and offers some commentary both&nbsp;[here](http://lua-users.org/lists/lua-l/2013-12/msg00277.html) and [here](https://stackoverflow.com/questions/288707/what-is-the-standard-or-best-supported-big-number-arbitrary-precision-librar).
> 
> As far as pure Lua goes,&nbsp;[this](https://github.com/user-none/lua-nums) sounds like it requires a newer Lua (5.3), but maybe I’m wrong. I also saw [this one](http://lua-users.org/lists/lua-l/2010-04/msg00607.html), which seems to be early enough to be 5.1.
> 
> I don’t know anything about [these](http://oss.digirati.com.br/luabignum/) but also discovered them while searching.

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

---

<div class="post-metadata">

**Author:** ![xDRock](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/x/d6d6ee/32.png) [@xDRock](https://forums.solar2d.com/u/xDRock)\
**Post date:** [June 26, 2018, 3:48pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/35 "2018-06-26T15:48:24Z")

</div>

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)&nbsp;

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

---

<div class="post-metadata">

**Author:** ![roaminggamer](https://forums.solar2d.com/user_avatar/forums.solar2d.com/roaminggamer/32/134_2.png) [@roaminggamer](https://forums.solar2d.com/u/roaminggamer)\
**Post date:** [June 26, 2018, 4:02pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/36 "2018-06-26T16:02:46Z")

</div>

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

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

[https://www.grammarly.com/blog/loose-lose/](https://www.grammarly.com/blog/loose-lose/)

---

<div class="post-metadata">

**Author:** ![remiduchalard](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/d2c977/32.png) [@remiduchalard](https://forums.solar2d.com/u/remiduchalard)\
**Post date:** [June 26, 2018, 4:04pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/37 "2018-06-26T16:04:42Z")

</div>

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 (&nbsp;http://lua-users.org/wiki/StringLibraryTutorial) mySecondPart=... How to compare: YourOwnFunction()
```

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [June 26, 2018, 4:17pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/38 "2018-06-26T16:17:38Z")

</div>

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.”&nbsp; 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

---

<div class="post-metadata">

**Author:** ![nick\_sherman](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/n/958977/32.png) [@nick\_sherman](https://forums.solar2d.com/u/nick_sherman)\
**Post date:** [June 26, 2018, 4:28pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/39 "2018-06-26T16:28:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xDRock](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/x/d6d6ee/32.png) [@xDRock](https://forums.solar2d.com/u/xDRock)\
**Post date:** [June 26, 2018, 4:29pm UTC](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945/40 "2018-06-26T16:29:01Z")

</div>

> [@roaminggamer](#):
>
> I was going to guess this is for a ‘clicker’, but the OP already answered.&nbsp; Dang… Too slow.
> 
> PS - You keep spelling the word **lose** incorrectly as loose.&nbsp; Just a small nit, but worth knowing.
> 
> [https://www.grammarly.com/blog/loose-lose/](https://www.grammarly.com/blog/loose-lose/)

hehe 🙂 noted

[Previous page](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945.md?page=1)

[Next page](https://forums.solar2d.com/t/how-to-use-numbers-greater-than-2-53/347945.md?page=3)
