Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Large Numbers?

in Questions Posts: 28

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.

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 8,390

    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
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    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.

  • dave1707dave1707 Mod
    Posts: 8,390

    @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
    
  • Posts: 28

    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?

  • dave1707dave1707 Mod
    edited July 2016 Posts: 8,390

    @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
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    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.

  • Posts: 28

    @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.

  • dave1707dave1707 Mod
    Posts: 8,390

    @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.

  • Posts: 28

    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.

  • dave1707dave1707 Mod
    edited July 2016 Posts: 8,390

    @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
    
  • Posts: 28

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

  • dave1707dave1707 Mod
    edited July 2016 Posts: 8,390

    @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
    
  • edited August 2016 Posts: 28

    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.

  • dave1707dave1707 Mod
    Posts: 8,390

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

  • Posts: 28

    @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.

  • dave1707dave1707 Mod
    Posts: 8,390

    @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.

  • AnatolyAnatoly Mod
    Posts: 889

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

  • dave1707dave1707 Mod
    edited August 2016 Posts: 8,390

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

    function setup()
        print(1e100)
        print(10^100)
    end
    
  • Posts: 28

    @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.

  • edited August 2016 Posts: 160

    @Valgo Do you mean you want to use tables as numbers (for example 405 is {4,0,5}) and you want to be able to add two of these tables together like {3,0,8} + {4,0,5} == {7,1,3}? If so you can add tables together with metatables so this is possible to make.
    Edit: I mean that tables can be added, multiplied, compared, divided, subtracted, ect. with metatables, not just added.

  • dave1707dave1707 Mod
    Posts: 8,390

    @Valgo Here's a routine to multiple 2 strings of numbers. The strings are converted to tables and then multiplied together. I tried it with 2 strings of 2600 digits each. I'm not sure if there's a limit other than time.

    function setup()
        a="2112357909875357864211356809766864234689976"
        b="4664458987522345790875422312234680097656777"
        str=mult(a,b)
        print(str)
    end   
    
    function mult(str1,str2)
        local r,n1,n2={},{},{} 
        for z=1,string.len(str1) do
            table.insert(n1,tonumber(string.sub(str1,z,z)))
            table.insert(r,0)
        end    
        for z=1,string.len(str2) do
            table.insert(n2,tonumber(string.sub(str2,z,z)))
            table.insert(r,0)
        end
        for x=#n2,1,-1 do
            local carry=0
            for y=#n1,1,-1 do
                local v=r[x+y]+n2[x]*n1[y]+carry
                r[x+y]=v%10
                carry=math.floor(v/10)
            end
            r[x]=carry
        end
        if r[1]==0 then
            table.remove(r,1)
        end
        return(table.concat(r))
    end  
    
Sign In or Register to comment.