Large Numbers?

I am planning to make a clicker/tapper game in Codea. The only thing is, this game will involve numbers that go into 10 ^ 300 or more. Does anyone know of a good way to implement numbers like this into a game? If so, please let me know.

Here’s the max integer used by Codea. If you tapped the screen 10 times per second and added 1 to a counter each time, it would take you 29,247,120,867.754 years to reach that number. 10^300 is a little overkill.

function setup()
    print(math.maxinteger)
    --  9,223,372,036,854,775,807     max integer for Codea 
end

If you get beyond Codea’s largest number, you will have to start using strings and do the arithmetic in bits. This has been discussed on the forum before, try searching for it.

@Valgo Here’s an example to create a large number. This is setup for 10^150, half the size you want. Tap and hold the screen to add 1, 60 times per second to the number. Let me know when you reach all 9’s.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    fontSize(10)
    v=0
    size=150    -- change size for the 10^ number 
    tab={}
    for z=1,size do
        tab[z]=0
    end
end

function draw()
    background(40, 40, 50)
    fill(255)
    tab[1]=tab[1]+v
    for z=1,size-1 do
        if tab[z]>9 then
            tab[z]=0
            tab[z+1]=tab[z+1]+1
        end
    end
    text("Tap and hold screen to add 1 to the number",WIDTH/2,HEIGHT/2+100)
    text("10^"..size,WIDTH/2,HEIGHT/2+50)
    for z=size,1,-1 do
        text(tab[z],WIDTH-z*6-30,HEIGHT/2)
    end
end

function touched(t)
    if t.state==BEGAN then
        v=1
    end
    if t.state==ENDED then
        v=0
    end
end

I would like to bring this topic back. I still haven’t been able to find or make anything good. Anyone else got any solutions?

@Valgo Below you see the largest number that Codea can use. If you add another number (1e100) to it, it still doesn’t change because you need to add an even bigger number to it. So if you’re working with numbers on the scale of 10^300 as you stated in your first post, you’re not going to see anything change unless you add other numbers of that size. In my example above, I show a 150 digit number for you to work with. What exactly are you trying to do. Do you want to see a lot of digits or just 10 to some power number. Also above, I show you the largest integer you can use before it goes to powers of 10.

function setup()
    a=1.7976931348623E308   -- largest 64 bit floating point number
    print(a)
    a=a+1e100
    print(a)
end

If you want to show larger numbers in full, you need to use strings and when adding these numbers, break them into smaller chunks that can be added together, and then recombine them afterwards. This is a lot of work.

It would be much easier if you avoided numbers this large.

@dave1707 I need to be able to do all the basic operations on these numbers. This includes addition, subtraction, multiplication, division, and comparisons. The way I want these numbers to be represented is like 1,000 = 1K. 1,000,000 = 1M. 55,000 = 55K. Once you go beyond the set of defined suffixes, then the numbers would be represented in scientific notation.

@Valgo You should be able to use the normal math, then add if statements to print the 1K or 1M depending on the number. Would you display the number 1234, as 1K+234 or just 1K.

I would display the number as 1.234K (would round to 3 digits). For 1,234,567 it would display 1.234M. That’s how I would want it to work.

@Valgo Are you after something like this. Tap screen to double the number.

function setup()
    nbr=1.0
end

function draw()
    background(40, 40, 50)
    fill(255)
    if nbr<1000 then
        str=string.format("%3d",nbr)       
    elseif nbr<1000000 then
        str=string.format("%.3fK",nbr/1000)       
    elseif nbr<1000000000 then
        str=string.format("%.3fM",nbr/1000000)    
    elseif nbr<1000000000000 then
        str=string.format("%.3fB",nbr/1000000000)  
    elseif nbr<1000000000000000 then
        str=string.format("%.3fT",nbr/1000000000000)  
    elseif nbr<1000000000000000000 then
        str=string.format("%.3fQ",nbr/1000000000000000)  
    else
        str=string.format("%.3e",nbr)
    end
    fontSize(20)
    text("Tap screen to double the number",WIDTH/2,HEIGHT-100)
    text(nbr,WIDTH/2,HEIGHT/2+100)
    fontSize(40)
    text(str,WIDTH/2,HEIGHT/2)
end

function touched(t)
    if t.state==BEGAN then
        nbr=nbr*2
    end
end

@dave1707 that is pretty much what I am looking for. Of course, it will have way more suffixes, but that is basically it.

@Valgo If that’s what you want, then this will be easier to use. You just have to fill in the table (tab) with what you want. Again, tap the screen to double the number.

EDIT: Added more entries to the table.
EDIT: Changed code for displaying numbers under 1000.
EDIT: Added number names in tab1.

function setup()
    tab={" ","K","M","B","t","q","Q","s","S","o","n","d","U","D",
            "T","Qt","Qd","Sd","St","O","N","v","c","e69","e72","e75"}
    tab1={"Thousand","Million","Billion","trillion","quadrillion","quintillion",
    "sextillion","septillion","octillion","nonillion","decillion","undecillion",
    "duodecillion","tredecillion","quattuordecillion","quindecillion",
    "sexdecillion","septendecillion","octodecillion","novemdecillion",
    "vigintillion","unvigintillion"}
    nbr=2.0
    convert(nbr)   
end

function draw()
    background(40, 40, 50)
    fill(255)
    fontSize(20)
    text(nbr,WIDTH/2,HEIGHT/2+100)
    fontSize(40)
    text(str,WIDTH/2,HEIGHT/2)
    text(str1,WIDTH/2,HEIGHT/2-50)
end

function touched(t)
    if t.state==BEGAN then
        nbr=nbr*2
        convert(nbr)
    end
end

function convert(num)
    if num<1000 then
        str=string.format("%d",num)
        str1=""
    else
        exp=0
        while num>1000 do
            num=num/1000
            exp=exp+1
        end
        str=string.format("%.3f%s",num,tab[exp+1])
        str1=string.format("%s",tab1[exp])
    end
end

I still haven’t found a great solution to the problem that I need to solve. I need a class that will allow for numbers up to an arbitrarily large size. What I need to be able to do with these numbers is set them, add them, multiply them, subtract them, divide them (you can round), compare them, and get a suffix for them.
Suffixes:
1,000 → 1K

1,234 → 1.23K (round to either 2nd or 3rd decimal place)

1,212,121 → 1.21M

After a certain number of suffixes, it would simply go into scientific notation.

If you could make this for me, or at least guide me along the right path, I would really appreciate it. Thanks.

@Valgo What don’t you like about my code above. I think it does everything you described.

@dave1707 You did one part of it - the suffixes. What I really need though is a class that has a value of a number; that number needs to be able to assume the value of a really large number - say, 10 ^ 2000. I don’t think I can have Codea limiting the size of my numbers, the game will eventually need larger than that.

@Valgo I’m not sure where you’re going to find those kind of numbers. The limit of Codea and any other 64 bit math is around 10^308. I guess if you’re working with numbers 3 digits past the decimal point, you can work with those and then add or subtract the exponents seperately. You’ll probably have to write your own routines.

@dave1707, What does 1e100 mean? 12 posts above in the code.

@TokOut 1e100 is 1+e100 or 10^100

function setup()
    print(1e100)
    print(10^100)
end

@dave1707 What I meant is a class that holds a table for each digit of the number. For example, 345 = {3, 4, 5}. This will be obsolete for the first 10^308 or so numbers, but once we break the 64 bit barrier, it will become necessary. I also need to be able to do simple arithmetic on these numbers. For example, I could call num1:add(num2), where num1 and num2 are instances of the class that I have described.