Babylonian doubling and halving algorithm for reciprocals , eTCL demo example calculator, numerical analysis

Babylonian doubling and halving algorithm for reciprocals , eTCL demo example calculator, numerical analysis

This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Aside from your courtesy, your wiki MONIKER and date as a signature and minimal good faith of any internet post are the rules of this TCL-WIKI. Its very hard to reply reasonably without some background of the correspondent on his WIKI bio page. Thanks, gold 12Dec2018


gold Here is some eTCL starter code for Babylonian doubling and halving algorithm for reciprocals in calculator shell. For the Babylonian scribes, the standard table gave reciprocals of regular numbers between 2 and 1_21 (decimal 81) as reciprocal pairs. There were a number of ways the reciprocals could be calculated, but the easiest way is called doubling and halving. An eTCL calculator was adapted to output the decimal equivalent of reciprocal pairs.with contraints, mainly to study target number and factor selection on reciprocal algorithms. Additional console programs below are used to check or improve subroutine.

In the cuneiform math problems and coefficient lists on clay tablets, there are coefficient numbers which were used in determining the amount of materials and the daily work rates of the workers. In most cases, the math problem is how the coefficient was used in estimating materials, work rates, and math problems. One difficulty is determining the effective magnitude or power of the number coefficient in the base 60 notation. In cuneiform, numbers in base 60 are written using a relative notation. For example, 20 could represent either 20*3600,20,20/60, 20/3600, or even 1/20. The basic dimensions and final tallies were presented in the cuneiform accounts on clay tablets, but some calculations and some units were left off the tablet. On the particular algorithms that use factoring, the relative value or uncertain magnitude makes a big difference because the factors of 20 and 1/20 are not the same. At least one approach for the modern reader and using modern terminology is to develop the implied algebraic equations and decimal equivalents from the cuneiform numbers. Then the eTCL calculator can be run over a number of testcases to validate the algebraic equations.

The doubling and halving algorithm for reciprocals was loaded into a console program below. Taking a known reciprocal pair of n and 1/n, the double target number has a (1/2)*reciprocal of the original in proportion. From modern notation, the new pair of reciprocals are 2*n and 1/(2*n). Taking a known reciprocal pair of n and 1/n, the triple target number has a (1/3)*reciprocal of the original in proportion. Using modern notation, the new pair of reciprocals are 3*n and 1/(3*n). Generally, the notation is a*n and 1/(a*n), for a as a constant. The resulting reciprocals from doubling and halving procedure in eTCL code seemed to work as a robust solution for positive integers, floating point numbers, and decimal fractions.

         # take a known reciprocal pair of 2 and .5, set result from proportions
             set target_a 2.
             set reciprocal .5
             set doubling_halving_reciprocal [* [/ $target_a $target_number  ] $reciprocal  ]


Pseudocode Section

           # using pseudocode for Babylonian doubling and halving algorithm
           # for reciprocals 
           # irregular defined as not in standard B. table of reciprocals
       supplied values 
         lower limit
         upper limit
         start number
         factor
         standard table  N <  81
         1,2,4,8,16,32,64
         3,6,12,24,48
         5,10,20,40,80
         9,18,36,72 
        doubling and halving algorithm
        reciprocal pairs starting with 1
                                  1   1
                                  2   0.5
                                  4   0.25
                                  8   0.125 
       reciprocal pairs starting with 3                               
                                  3   0.333
                                  6   0.1666
                                 12   0.08333
                                 24   0.04166             
      Late Babylonian interest 1> N > 2
      need to compare result with TCL calculation
      check in error subroutine

Testcases Section

In planning any software, it is advisable to gather a number of testcases to check the results of the program. The math for the testcases can be checked by pasting statements in the TCL console. Aside from the TCL calculator display, when one presses the report button on the calculator, one will have console show access to the capacity functions (subroutines).

Testcase 1

table 1printed in tcl wiki format
quantity value comment, if any
1:testcase_number
1.0 :lower limit
81.0 :upper limit
2.0 :doubling factor (usually 2. )
0.5 :optional: halving factor (usually 0.5 ):
4.0 :answers: trial
1.0 :function
1.0 :percentage error
0.0 :initial reciprocal

doubling_halving_algorithmprinted in tcl wiki format
quantity value factor comment, if any
line number target number factor reciprocal solution
1 2.0 0.5 answer for reciprocal 0.5 1/n comparison with tcl math
2 4.0 0.25 answer for reciprocal 0.25 1/n comparison with tcl math
3 8.0 0.125 answer for reciprocal 0.125 1/n comparison with tcl math
4 16.0 0.0625 answer for reciprocal 0.0625 1/n comparison with tcl math
5 32.0 0.03125 answer for reciprocal 0.03125 1/n comparison with tcl math
6 64.0 0.015625 answer for reciprocal 0.015625 1/n comparison with tcl math
7 128.0 0.0078125 answer for reciprocal 0.0078125 1/n comparison with tcl math

Testcase 2

Testcase 3


Screenshots Section

figure 1.

Babylonian doubling and halving algorithm for reciprocals png


References:


Appendix Code

appendix TCL programs and scripts

        # pretty print from autoindent and ased editor
        # Babylonian doubling and halving algorithm for reciprocals  calculator
        # written on Windows XP on eTCL
        # working under TCL version 8.5.6 and eTCL 1.0.1
        # gold on TCL WIKI, 2nov2017
        package require Tk
        package require math::numtheory
        namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
        set tcl_precision 17
        frame .frame -relief flat -bg aquamarine4
        pack .frame -side top -fill y -anchor center
        set names {{} {lower limit :} }
        lappend names {upper limit:}
        lappend names {doubling factor (usually 2. ) : }
        lappend names {optional: halving factor (usually 0.5 ): }
        lappend names {answers: trial  :}
        lappend names {function : }
        lappend names {percentage error: }
        lappend names {initial reciprocal :} 
        foreach i {1 2 3 4 5 6 7 8} {
            label .frame.label$i -text [lindex $names $i] -anchor e
            entry .frame.entry$i -width 35 -textvariable side$i
            grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1 }
        proc about {} {
            set msg "Calculator for doubling and halving algorithm for reciprocals
            from TCL WIKI,
            written on eTCL "
            tk_messageBox -title "About" -message $msg } 
       proc ::tcl::mathfunc::precision {precision float}  {
            #  tcl:wiki:Floating-point formatting, [AM]
            set x [ format "%#.5g" $float ]
            return $x
           }   
            #proc errorx always returns a positive error. 
            #Normally assume $aa is human estimate,
            #assume $bb is divinely exact.
       proc errorx  {aa bb} {expr { $aa > $bb ?   (($aa*1.)/$bb -1.)*100. : (($bb*1.)/$aa -1.)*100.}}
             proc doubling_halving_algorithm { target_number } {
             # initialize placeholder answer
             set doubling_halving_reciprocal 1.
             set target_a 2.
             set reciprocal .5
             set doubling_halving_reciprocal [* [/ $target_a $target_number  ] $reciprocal  ]
             return $doubling_halving_reciprocal
             }
       proc calculate {     } {
            global answer2
            global side1 side2 side3 side4 side5
            global side6 side7 side8 
            global lower_limit upper_limit doubling_factor
            global testcase_number
            global doubling_halving_reciprocal check_answer_product4
            incr testcase_number 
            set side1 [* $side1 1. ]
            set side2 [* $side2 1. ]
            set side3 [* $side3 1. ]
            set side4 [* $side4 1. ]
            set side5 [* $side5 1. ]
            set side6 [* $side6 1. ]
            set side7 [* $side7 1. ]
            set side8 [* $side8 1. ]
            # initialize placeholder answer
            set doubling_halving_reciprocal 1.
            set doubling_factor $side3
            # begin doubling_halving_algorithm
            set lower_limit $side1
            set upper_limit $side2
                    }
        proc fillup {aa bb cc dd ee ff gg hh} {
            .frame.entry1 insert 0 "$aa"
            .frame.entry2 insert 0 "$bb"
            .frame.entry3 insert 0 "$cc"
            .frame.entry4 insert 0 "$dd"
            .frame.entry5 insert 0 "$ee"
            .frame.entry6 insert 0 "$ff" 
            .frame.entry7 insert 0 "$gg"
            .frame.entry8 insert 0 "$hh" 
             }
        proc clearx {} {
            foreach i {1 2 3 4 5 6 7 8 } {
                .frame.entry$i delete 0 end } }
        proc reportx {} {
            global answer2
            global side1 side2 side3 side4 side5
            global side6 side7 side8 
            global lower_limit upper_limit doubling_factor
            global testcase_number
            console show;
            puts "%|table $testcase_number|printed in| tcl wiki format|% "
            puts "&| quantity| value| comment, if any|& "
            puts "&| $testcase_number:|testcase_number | |&"
            puts "&| $side1 :|lower limit  |   |&"
            puts "&| $side2 :|upper limit | |& "  
            puts "&| $side3 :|doubling factor (usually 2. ) | |& "
            puts "&| $side4 :|optional: halving factor (usually 0.5 ): | |&"
            puts "&| $side5 :|answers: trial | |&"
            puts "&| $side6 :|function |  |&"
            puts "&| $side7 :|percentage error |  |&"
            puts "&| $side8 :|initial reciprocal |  |&" 
            puts " reciprocal [ doubling_halving_algorithm 10 ] "
             set counter 1
             set keeper 1
             set reciprocal 1.
             puts "%|doubling_halving_algorithm|printed in| tcl wiki format| || |% "
             puts "&| quantity| value| factor |comment, if any|||&"
             puts "&| line number | target number | factor |  reciprocal solution |||&"
             set target_number 1.
             while { $keeper < $upper_limit  } {    
             set keeper [* $keeper $doubling_factor ] 
             set reciprocal [* $reciprocal [/ 1. $doubling_factor ] ] 
             puts "&| $counter | $keeper  | $reciprocal  | answer for reciprocal |
[/ 1. $keeper ]  | 1/n comparison with tcl math |&   "      
             incr counter   
             } 

            }
        frame .buttons -bg aquamarine4
        ::ttk::button .calculator -text "Solve" -command { set side8 0 ; calculate   }
        ::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 1.  81.0 2.0 0.5  4.0   1.0   1.0    0.5}
        ::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 1.0 81.0 2.0 0.5  9.0   1.0  0.138   2.1}
        ::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 1.0 81.0 2.0 0.5  81.0  1.0  0.5     1.1}
        ::ttk::button .clearallx -text clear -command {clearx }
        ::ttk::button .about -text about -command {about}
        ::ttk::button .cons -text report -command { reportx }
        ::ttk::button .exit -text exit -command {exit}
        pack .calculator  -in .buttons -side top -padx 10 -pady 5
        pack  .clearallx .cons .about .exit .test4 .test3 .test2   -side bottom -in .buttons
        grid .frame .buttons -sticky ns -pady {0 10}
               . configure -background aquamarine4 -highlightcolor brown -relief raised -border 30
        wm title . "Babylonian Doubling and Halving Algorithm for Reciprocals Calculator"     

Pushbutton Operation

For the push buttons, the recommended procedure is push testcase and fill frame, change first three entries etc, push solve, and then push report. Report allows copy and paste from console.

For testcases in a computer session, the eTCL calculator increments a new testcase number internally, eg. TC(1), TC(2) , TC(3) , TC(N). The testcase number is internal to the calculator and will not be printed until the report button is pushed for the current result numbers. The current result numbers will be cleared on the next solve button. The command { calculate; reportx } or { calculate ; reportx; clearx } can be added or changed to report automatically. Another wrinkle would be to print out the current text, delimiters, and numbers in a TCL wiki style table as

  puts " %| testcase $testcase_number | value| units |comment |%"
  puts " &| volume| $volume| cubic meters |based on length $side1 and width $side2   |&"  

Console program for doubling_halving reciprocal algorithm

                  # autoindent from ased editor
                  # console program for doubling_halving reciprocal algorithm 
                  # written on Windows XP on eTCL
                  # working under TCL version 8.5.6 and eTCL 1.0.1
                  # TCL WIKI , 30jan2017
             console show
             package require math::numtheory
             namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory } 
             proc doubling_halving_algorithm { target_number } {
             global side1 side2 side3 side4 side5
             global side6 side7 side8 
             global doubling_halving_reciprocal check_answer_product4
             # initialize placeholder answer
             set doubling_halving_reciprocal 1.
             set target_a 2.
             set reciprocal .5
             set doubling_halving_reciprocal [* [/ $target_a $target_number  ] $reciprocal  ]
             #puts "doubling_halving $doubling_halving_reciprocal "
             return $doubling_halving_reciprocal
             }
            # initialize placeholder answer
            set doubling_halving_reciprocal 1.
            # begin doubling_halving_algorithm
            # set doubling_halving_reciprocal [ doubling_halving_algorithm $target_number ]
             set counter 1
             puts "%|doubling_halving_algorithm|printed in| tcl wiki format| || |% "
             puts "&| quantity| value| factor |comment, if any|||&"
             puts "&| line number | target number | factor |  reciprocal solution |||&"
             set target_number 1.
             while { $counter < 50.  } {           
             set target_number [* 2. $target_number ]
             puts "&| $counter |$target_number  | [ doubling_halving_algorithm $target_number ] | answer for reciprocal |[/ 1. 

$target_number ] | 1/n comparison with tcl math |&   "      
             incr counter   
             } 
doubling_halving_algorithmprinted in tcl wiki format 1/n tcl math
quantity value factor comment, if any
line number target number factor reciprocal solution
1 2.0 0.5 answer for reciprocal 0.5 comparison with tcl math
2 4.0 0.25 answer for reciprocal 0.25 comparison with tcl math
3 8.0 0.125 answer for reciprocal 0.125 comparison with tcl math
4 16.0 0.0625 answer for reciprocal 0.0625 comparison with tcl math
5 32.0 0.03125 answer for reciprocal 0.03125 comparison with tcl math
6 64.0 0.015625 answer for reciprocal 0.015625 comparison with tcl math
7 128.0 0.0078125 answer for reciprocal 0.0078125 comparison with tcl math
8 256.0 0.00390625 answer for reciprocal 0.00390625 comparison with tcl math
9 512.0 0.001953125 answer for reciprocal 0.001953125 comparison with tcl math
10 1024.0 0.0009765625 answer for reciprocal 0.0009765625 comparison with tcl math

doubling_halving_algorithmprinted in tcl wiki format 1/n tcl math
quantity value factor comment, if any
line number target number factor reciprocal solution
1 0.02 50.0 answer for reciprocal 50.0 comparison with tcl math
2 0.04 25.0 answer for reciprocal 25.0 comparison with tcl math
3 0.08 12.5 answer for reciprocal 12.5 comparison with tcl math
4 0.16 6.25 answer for reciprocal 6.25 comparison with tcl math
5 0.32 3.125 answer for reciprocal 3.125 comparison with tcl math
6 0.64 1.5625 answer for reciprocal 1.5625 comparison with tcl math
7 1.28 0.78125 answer for reciprocal 0.78125 comparison with tcl math
8 2.56 0.390625 answer for reciprocal 0.390625 comparison with tcl math
9 5.12 0.1953125 answer for reciprocal 0.1953125 comparison with tcl math
10 10.24 0.09765625 answer for reciprocal 0.09765625 comparison with tcl math

tripling_taking 1/3 printed in tcl wiki format 1/n tcl math
quantity value factor comment, if any
line number target number factor reciprocal solution
1 3.0 0.3333333333333333 answer for reciprocal 0.3333333333333333 1/n comparison with tcl math
2 9.0 0.1111111111111111 answer for reciprocal 0.1111111111111111 1/n comparison with tcl math
3 27.0 0.037037037037037035 answer for reciprocal 0.037037037037037035 1/n comparison with tcl math
4 81.0 0.012345679012345678 answer for reciprocal 0.012345679012345678 1/n comparison with tcl math
5 243.0 0.00411522633744856 answer for reciprocal 0.00411522633744856 1/n comparison with tcl math
6 729.0 0.0013717421124828531 answer for reciprocal 0.0013717421124828531 1/n comparison with tcl math
7 2187.0 0.0004572473708276177 answer for reciprocal 0.0004572473708276177 1/n comparison with tcl math
8 6561.0 0.00015241579027587258 answer for reciprocal 0.00015241579027587258 1/n comparison with tcl math
9 19683.0 5.080526342529086e-5 answer for reciprocal 5.080526342529086e-5 1/n comparison with tcl math
10 59049.0 1.6935087808430286e-5 answer for reciprocal 1.6935087808430286e-5 1/n comparison with tcl math

Hidden Comments Section



Please place any comments here with your wiki MONIKER and date, Thanks.gold12Dec2018