Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to use numbers greater than 2^53
Started by xDRock Jun 26 2018 12:45 AM

- - - - -
29 replies to this topic
[TOPIC CONTROLS]
Page 1 of 2 1 2
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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??
 



[TOPIC: post.html]
#2

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 298 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 725 posts
  • Corona SDK

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.



[TOPIC: post.html]
#4

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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. 



[TOPIC: post.html]
#5

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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 tried using bignum but it throws multiple errors and when it comes to lbc I am not able to implement it. 



[TOPIC: post.html]
#6

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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.



[TOPIC: post.html]
#7

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,748 posts
  • Corona SDK

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/



[TOPIC: post.html]
#8

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 298 posts
  • Corona SDK

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()


[TOPIC: post.html]
#9

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,119 posts
  • Corona Staff

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



[TOPIC: post.html]
#10

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,458 posts
  • Corona SDK

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.

[TOPIC: post.html]
#11

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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/

 

hehe :) noted



[TOPIC: post.html]
#12

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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.



[TOPIC: post.html]
#13

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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.

 

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



[TOPIC: post.html]
#14

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 725 posts
  • Corona SDK

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.



[TOPIC: post.html]
#15

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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

 

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.



[TOPIC: post.html]
#16

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 298 posts
  • Corona SDK

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.

 

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



[TOPIC: post.html]
#17

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,458 posts
  • Corona SDK

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

 

Don't use string format, just do:

 

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

 

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



[TOPIC: post.html]
#18

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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.

 

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



[TOPIC: post.html]
#19

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,458 posts
  • Corona SDK

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.



[TOPIC: post.html]
#20

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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

 

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



[TOPIC: post.html]
#21

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

Items will also have their values stored like this. Add/Subtract the values from each unit to 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.

 

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


[TOPIC: post.html]
#22

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

Thank you guys, I will try implementing both  nick_sherman and

remiduchalard's  approaches. 



[TOPIC: post.html]
#23

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,748 posts
  • Corona SDK

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.



[TOPIC: post.html]
#24

xDRock

[GLOBAL: userInfoPane.html]
xDRock
  • Observer

  • 16 posts
  • Corona SDK

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.



[TOPIC: post.html]
#25

naveen_pcs

[GLOBAL: userInfoPane.html]
naveen_pcs
  • Contributor

  • 251 posts
  • Corona SDK

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!




[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]