CS 3723
 Programming Languages 
   Tiny® Testing ^ Operator  
(with doubles)


Tiny® Programs Testing Exponentiation (^): Here are two programs tor test the hat (^) operator:

  1. First is a program which inputs three numbers a, r, and n and calculates the sum of a geometric series with initial term a, ratio r, and number of terms n. The program calculates the sum by adding the terms, and compares the answer with the formula:

    a + ar + ar2 + ar3 + . . . + arn-1 = a(1 - rn)/(1 - r)
    

    Sample 7 Output
    > a; > r; > n; < N;
    i = 0; s = 0;
    { n - i ?
       t = a*r^i;
       s = s + t;
       < i; < B;
       < t; < T;
       < s; < N;
       i = i + 1;
    }
    < N; < s; < T;
    < a*(1 - r^n)/(1 - r);
    < N; $
    % javac Tiny.java
    % java Tiny < g.t > g.s
    % spim -file g.s
    2
    3
    10
    
    0 2     2
    1 6     8
    2 18    26
    3 54    80
    4 162   242
    5 486   728
    6 1458  2186
    7 4374  6560
    8 13122 19682
    9 39366 59048
    
    59048   59048
    
    % javac Tiny.java
    % java Tiny < g.t > g.s
    % spim -file g.s
    5
    7
    10
    
    0 5          5
    1 35         40
    2 245        285
    3 1715       2000
    4 12005      14005
    5 84035      98040
    6 588245     686285
    7 4117715    4804000
    8 28824005   33628005
    9 201768035  235396040
    
    235396040    235396040
    

  2. The next program compares the values of n^i^j,   n^(i^j), and (n^i)^j. (The output was cleaned up slightly by hand.)

    Sample 8 Output
    > i; > j; 
    < i; < B; 
    < j; < N; < N;
    n = 1;
    { 2*5 - n ?
         b = 1;
         < n; < B;
         e = n ^ i ^ j;
         f = n ^ (i ^ j);
         g = (n ^ i) ^ j;
         < e; < T; 
         < f; < T; 
         < g; < N;
         n = n + 1;
    }$
    % javac Tiny.java
    % java Tiny < e.t > e.s
    % spim -file e.s
    3
    2
    3 2
    
    1 1          1          1
    2 512        512        64
    3 19683      19683      729
    4 262144     262144     4096
    5 1953125    1953125    15625
    6 10077696   10077696   46656
    7 40353607   40353607   117649
    8 134217728  134217728  262144
    9 387420489  387420489  531441
    

(Revision date: 2014-11-06. Please use ISO 8601, the International Standard.)