Multiplying a value sorta breaks

I was just adding a cost feature, so if you were to buy an item it would increase in cost. It’s very simple but for some reason after buying enough of the item it would then result in $0 as the price.

gui.Cost.Value = (basecost * 1.15^(plr:WaitForChild("PlrStats").Upgrades[gui.Name].Value + 2))

So getting the new cost amount is just getting the base cost of the item (it can be whatever, but in this it is 20 million) and then multiplying it with how many you have of the item plus two (so this ‘plr:WaitForChild(“PlrStats”).Upgrades[gui.Name].Value’ is just the item owned amount).

I’m not very sure why this is happening since I printed the same value as what would be the cost of the item and it worked just fine. In this example having the base cost of 20m and having, I believe, 194 of the item it breaks it, but printing the same thing (and even higher amount of items) works just fine.

1 Like

It could go to +Infinity if you keep making it bigger, which could get turned to zero by a later operation.

I just updated the topic thing so maybe it’ll give more context and stuff on it. But how big does a number get for it to be +infinity?

The last finite number that can be stored is about 17976931348623157000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000. If you add anything to this which rounds any higher it will “flush” to +Infinity.

if you are using an int value then it wont work, ive had this problem before handling big numbers with valuys and it just turned to 0, you should change to using a number value, those can handle more digits

3 Likes

So it will reach infinity when your Value is about 5080

Yup this was the problem, thank you. I just changed it to a numbervalue and it now is going into the octillions instead of dying on the quintillions. And to the person that has been replying for 20min, thank you for your time.

With the following calculations in mind:

local cost = 2*10^7 --20M
local i = 194 --repetitions

local y = cost*1.15^(i+2)
print(y) --1.57690927E19
print(2^63 < y and y < 2^64) --true

I was able to figure out that the number y calculated above, is more than 2^63 and less than 2^64. So I assume the problem here is that the instance gui.Cost stores integers as 64-bit numbers in the background, and the issue is caused as the limit is being approached.

A possible fix is to use a NumberValue or store the number as a string in a StringValue(so it’s stored using the e notation, and in code is converted to a float instead of an int). The first option is self-explanatory, and for the second one you have to do this:

--When writing to cost:
local cost = (basecost * 1.15^(plr:WaitForChild("PlrStats").Upgrades[gui.Name].Value + 2))
gui.Cost.Value = tostring(cost) --gui.Cost here is a StringValue

--When reading:
local cost = tonumber(gui.Cost.Value)
print(cost)

If you want to convert that to an integer in code for other calculations, you can use math.floor or math.round depending on what you’re trying to achieve.

After testing, I found out that the possible range of values for an IntValue is:

-9223372036854776832 <= x <= 9223372036854775295

If the x is out of bounds, the value is set to 0.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.