How to use numbers greater than 2^53

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

Thank you guys, I will try implementing both  nick_sherman and

remiduchalard’s  approaches. 

Hey guys.  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 nick_sherman method might be better because it would be much easier to implement.

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!

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

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.  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”.  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:  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!  Few of your players will live that long.

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

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.

If only someone had already suggested that :wink:

Whoops sorry. You did already suggest that.  

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