How high can an integer go? (Solved)
Nakita Block
27 Aug 2018, 11:41I want to make a "tycoon" type of game but it's no use if a variable is badly limited on how high its number can get (cough BASIC) so that is pretty much my question. Does anyone know the range?
mrangel
27 Aug 2018, 12:28Looks like the range of integers is -2147483648 to 2147483647.
It shouldn't be that hard to implement a bignumber system, though. Have separate attributes for units, millions, billions, and so on. Use changescripts to automatically update the higher values, so that as soon as your units value goes over a million, it subtracts a million from itself and adds one to the millions attribute. Then you only need to worry about individual transactions that are over the limit (which is unlikely, because if you have something that costs over a billion, you could just make the price apply directly to the millions counter.
mrangel
27 Aug 2018, 13:14Or, as you're likely to be adding and subtracting numbers but not multiplying them or doing other arithmetic (outside of very special circumstances) you could go string-as-bignum. Make your attribute a string like "1238959859885" or whatever, and make a couple of functions like AddBignum
and similar. If you can't figure out how to do this, there's plenty of people who'd be happy to help.
Nakita Block
28 Aug 2018, 04:10Thank you for your replies.
mrangel
28 Aug 2018, 17:37Just out of curiosity, here's one method I might use for handing "integers" bigger than a couple of billion. This stores your numbers as strings of digits, so there's no theoretical maximum length. It then converts numbers to lists of numbers, 3 digits at a time, in order to do arithmetic.
<function name="AddBignum" type="string" parameters="numA, numB"><![CDATA[
result = ""
partsA = NumberParts(numA)
partsB = NumberParts(numB)
negative = false
if (partsA[0] = partsB[0]) {
if (partsA[0] = -1) {
negative = true
}
}
else {
inverted = NewList()
list add (inverted, 1)
if (partsA[0] = -1) {
list remove (partsA, -1)
return (SubtractBignum (partsB, ListCombine(inverted, partsA)))
}
else {
list remove (partsB, -1)
return (SubtractBignum (partsA, ListCombine(inverted, partsB)))
}
}
remainder = 0
while (ListCount(partsA) * ListCount(partsB) > 1) {
result_part = remainder
if (ListCount (partsA) > 1) {
part = partsA[1]
list remove (partsA, part)
result_part = result_part + part
}
if (ListCount (partsB) > 1) {
part = partsB[1]
list remove (partsB, part)
result_part = result_part + part
}
remainder = result_part / 1000
result = Right( "000"+ToString (result_part), 3) + result
}
if (negative) {
result = "-" + result
}
return (result)
]]></function>
<function name="NumberParts" type="list" parameters="input"><![CDATA[
switch (TypeOf (input)) {
case ("list") {
return (input)
}
case ("string") {
result = NewList()
if (Left (input, 1) = "-") {
list add (result, -1)
input = Mid (input, 2)
}
else {
list add (result, 1)
}
while (true) {
list add (result, ToInt (Right (input, 3)))
if (LengthOf (input) > 3) {
input = Left (input, LengthOf (input) - 3)
}
else {
if (ListCount (result) = 1) {
list add (result, 0)
}
return (result)
}
}
}
case ("int") {
result = NewList()
if (input < 0) {
input = 0 - input
list add (result, -1)
}
else {
list add (result, 1)
}
while (input > 0) {
list add (result, input % 1000)
input = input / 1000
}
if (ListCount (result) = 1) {
list add (result, 0)
}
return (result)
}
default {
error ("ERROR: Can't convert "+input+" ("+TypeOf(input)+") to a bignum.")
}
}
]]></function>
Pertex
29 Aug 2018, 06:42Can't you use Double for larger numbers?
mrangel
29 Aug 2018, 07:53Using floats sacrifices accuracy.