Incremental game high numbers?

I wonder how I can implement high numbers in code, like shown in most of the idle clicker incremental games.

How can I work with huge numbers and increase those, like for example 1.65E11 which should be updated every second by adding another 300.050 thousands for example?

Can this be done with Lua in Corona?

I’m not sure exactly what you’re asking, so this is spun a few ways.  :slight_smile:

2^54 = 18,014,398,509,481,984‬ is the end of the line.

Internally, Lua uses numbers that are interpolations between powers of 2, namely 1 / 2^53 of the distance from one to the other. As you can imagine, this gives you a pretty huge swath between 1 / 4, 1 / 2, 1, 2, 4, 8, 16, … etc.

Once you hit 2^53, though, it means you land exactly on all the integers and can’t represent any values in between.

Once you hit 2^54, you can’t even land on odd integers (then only multiples of 4, then 8, and so on), thus the problem. In fact, if you increment by 1, you won’t move at all. This is exactly the same phenomenon as the “I gave it THESE decimal values but it’s printing THESE slightly different ones” issue that sometimes shows up on the forums: the numbers aren’t exactly representable.

That’s me interpreting your question “How can I work with huge numbers” one way, to say you actually have more than you might think.

You actually can use 1.65e11 directly in Lua, in case you were wondering that.

With the “e” and “E” specifiers to string.format() you can get the scientific format:

print("!!!", string.format("%e", 3323473.4534532)) print("???", string.format("%E", 343439054398509435))

One of my favorite sayings (I think it came from a Murphy’s Law Book, probably #2).  “Measure with a micrometer, mark with chalk, cut with an axe”.

Why is that relevant? It’s about precision. If someone has $57 billion in their account, are you really concerned about it being $57,000,000,001 or $57,000,000,002?

Your items you’re adding to someone who has a billion in their game account probably isn’t going to be adding $10 or $5 to the account in the game, the values you will be adding are perhaps thousands or millions to their balance. So you don’t need really big numbers to handle this. Once their balance hits 1,000,000,000, don’t store that, store 1,000,000 and don’t add $10,000 , instead add $10. and then just append G’s B’, and M’s to the amounts that you show. 

Rob

Thanks for the tips guys! This helps a lot!

I have a game that has BIG numbers.  Sharing some code for formatting them

 

function comma\_value(amount) local function roundToNthDecimal(num, n) local mult = 10^(n or 0) return mfloor(num \* mult + 0.5) / mult end local suffix, formatted = "", 0 if amount == nil or amount == "" then amount = 0 else amount = tonumber(amount) end amount = roundToNthDecimal(amount, 2) if amount \> 1000 then amount = mfloor(amount) end if amount \>= 1000000000000000000000000000000000 then amount = amount / 1000000000000000000000000000000000 suffix = "d" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000000000000 then amount = amount / 1000000000000000000000000000000 suffix = "n" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000000000 then amount = amount / 1000000000000000000000000000 suffix = "o" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000000 then amount = amount / 1000000000000000000000000 suffix = "sp" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000 then amount = amount / 1000000000000000000000 suffix = "sx" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000 then amount = amount / 1000000000000000000 suffix = "qi" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000 then amount = amount / 1000000000000000 suffix = "qa" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000 then amount = amount / 1000000000000 suffix = "t" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000 then amount = amount / 1000000000 suffix = "b" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000 then amount = amount / 1000000 suffix = "m" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 100000 then amount = amount / 1000 suffix = "k" formatted = tonumber(string.format("%.1f", amount)) else formatted = amount end while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if (k==0) then break end end return formatted..suffix end

Pass a number and get a formatted string returned.

This reminds me of an old joke:

“Joiners measure to the nearest millimetre. Carpenters measure to the nearest centimetre.  Bricklayers just hope they turned up at the right site.”

I’m not sure exactly what you’re asking, so this is spun a few ways.  :slight_smile:

2^54 = 18,014,398,509,481,984‬ is the end of the line.

Internally, Lua uses numbers that are interpolations between powers of 2, namely 1 / 2^53 of the distance from one to the other. As you can imagine, this gives you a pretty huge swath between 1 / 4, 1 / 2, 1, 2, 4, 8, 16, … etc.

Once you hit 2^53, though, it means you land exactly on all the integers and can’t represent any values in between.

Once you hit 2^54, you can’t even land on odd integers (then only multiples of 4, then 8, and so on), thus the problem. In fact, if you increment by 1, you won’t move at all. This is exactly the same phenomenon as the “I gave it THESE decimal values but it’s printing THESE slightly different ones” issue that sometimes shows up on the forums: the numbers aren’t exactly representable.

That’s me interpreting your question “How can I work with huge numbers” one way, to say you actually have more than you might think.

You actually can use 1.65e11 directly in Lua, in case you were wondering that.

With the “e” and “E” specifiers to string.format() you can get the scientific format:

print("!!!", string.format("%e", 3323473.4534532)) print("???", string.format("%E", 343439054398509435))

One of my favorite sayings (I think it came from a Murphy’s Law Book, probably #2).  “Measure with a micrometer, mark with chalk, cut with an axe”.

Why is that relevant? It’s about precision. If someone has $57 billion in their account, are you really concerned about it being $57,000,000,001 or $57,000,000,002?

Your items you’re adding to someone who has a billion in their game account probably isn’t going to be adding $10 or $5 to the account in the game, the values you will be adding are perhaps thousands or millions to their balance. So you don’t need really big numbers to handle this. Once their balance hits 1,000,000,000, don’t store that, store 1,000,000 and don’t add $10,000 , instead add $10. and then just append G’s B’, and M’s to the amounts that you show. 

Rob

Thanks for the tips guys! This helps a lot!

I have a game that has BIG numbers.  Sharing some code for formatting them

 

function comma\_value(amount) local function roundToNthDecimal(num, n) local mult = 10^(n or 0) return mfloor(num \* mult + 0.5) / mult end local suffix, formatted = "", 0 if amount == nil or amount == "" then amount = 0 else amount = tonumber(amount) end amount = roundToNthDecimal(amount, 2) if amount \> 1000 then amount = mfloor(amount) end if amount \>= 1000000000000000000000000000000000 then amount = amount / 1000000000000000000000000000000000 suffix = "d" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000000000000 then amount = amount / 1000000000000000000000000000000 suffix = "n" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000000000 then amount = amount / 1000000000000000000000000000 suffix = "o" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000000 then amount = amount / 1000000000000000000000000 suffix = "sp" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000000 then amount = amount / 1000000000000000000000 suffix = "sx" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000000 then amount = amount / 1000000000000000000 suffix = "qi" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000000 then amount = amount / 1000000000000000 suffix = "qa" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000000 then amount = amount / 1000000000000 suffix = "t" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000000 then amount = amount / 1000000000 suffix = "b" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 1000000 then amount = amount / 1000000 suffix = "m" formatted = tonumber(string.format("%.1f", amount)) elseif amount \>= 100000 then amount = amount / 1000 suffix = "k" formatted = tonumber(string.format("%.1f", amount)) else formatted = amount end while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if (k==0) then break end end return formatted..suffix end

Pass a number and get a formatted string returned.

This reminds me of an old joke:

“Joiners measure to the nearest millimetre. Carpenters measure to the nearest centimetre.  Bricklayers just hope they turned up at the right site.”