## Babylonian Square Side Rule & Diagonal Procedure Algorithm and eTCL demo example calculator, numerical analysis

gold Here is some eTCL starter code for Babylonian Square Side Rule & Diagonal Algorithm in calculator shell.

The Babylonian Square Side Rule from clay tablets was loaded into an eTCL calculator shell. The Babylonian Square Side Rule is of historical interest, but the rule is not very accurate and dependent on an initial input or guess. The square side rule with a single trial input becomes less accurate with increasing N. In some math problems, the Babylonians used the Square Side Rule for calculating the diagonals of squares and rectangles. The Babylonians did not use algebra notation, so the reader will have to bear some anachronisms in the eTCL pseudocode. Succesive or iterated math solutions are called algorithms and the Square Side Rule procedure is one of the earliest algorithms documented. The TCL procedures are descendants of this idea.

In the Babylonian problem texts on clay tablets, there are numbers and coefficients which were used in determining geometric dimensions, the amount of materials, and the daily work rates of the workers. One difficulty is determining the effective power of the numbers in base 60. For example, 20 could represent either 20*3600, 20*60, 20, 20/60, 20/3600, or even 1/20. The basic dimensions of geometric figures and final tallies were presented in the Babylonian accounts on clay tablets, but sometimes the calculations were left off the tablet, broken off, or garbled. At least one approach for the modern reader and using modern terminology is to develop the implied algebraic equations from the Babylonian numbers. Then the eTCL calculator can be run over a number of testcases to validate the algebraic equations. In the eTCL code, extra significant digits are retained to check the function errors, but not to imply greater precision in the algorithm. The Babylonians did not use percent estimates, but that seemed the most understandable error rating for the eTCL calculator.

For the first testcase of sqrt 2, the initial trial_square_root entry of 2 gave an error of 6.06 percent. For the second testcase of sqrt 10, the initial trial_square_root entry of 3 gave an error of 0.138 percent. For the third testcase, the initial trial_square_root entry of 9 gave an error of 0.555 percent. An iterative solution with successive trial_square_root entries loaded as 9, 10.05555, and 10.000153437 showed 1.177E-8 percent error in three interations. The formula factor is usually integer 2 in the texts, rarely three. A console program was added below for picking trial_square_root entries.

## Pseudocode Section

```    # using  pseudocode for Babylonian procedure algorithm.
3 quantities needed
target number
trial_square_root,   w.a. guess
formula factor , usually 2 or 3
set approximate_root [* [/ 1. \$formula_factor ] [+ [* [/ 1. \$trial_square_root ] \$target_number] \$trial_square_root ]   ]
set side5  \$trial_square_root
set side6 [+ [* [/ 1. \$trial_square_root ] \$target_number] \$trial_square_root ]
set side7 [* [/ 1. \$formula_factor ]]
check approx. root from square side rule with sqrt function in TCL
ref. errorx procedure
check_answer   new area =? desired goal , desired goal reached (yes/no)
set answers and printout with resulting values```

### 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
2.0 :target number N
2.0 :trial square root
2.0 :function factor
3.0 :answers: intermediate term in formula
4.0 :trial square
1.414 :square root from TCL sqrt function
6.066 :percentage error
1.5 :approximate square root from square side rule

#### Testcase 2

table 2printed in tcl wiki format
quantity value comment, if any
2:testcase_number
10.0 :target number N
3.0 :trial square root
2.0 :function factor
6.333 :answers: intermediate term in formula
9.0 :trial square
3.162 :square root from TCL sqrt function
0.138 :percentage error
3.166 :approximate square root from square side rule

#### Testcase 3

table 3printed in tcl wiki format
quantity value comment, if any
3:testcase_number
100.0 :target number N
9.0 :trial square root
2.0 :function factor
20.111 :answers: intermediate term in formula
81.0 :trial square
10.0 :square root from TCL sqrt function
0.555 :percentage error
10.0555 :approximate square root from square side rule

### Screenshots Section

#### figure 1. ## Appendix Code

### appendix TCL programs and scripts

```        # pretty print from autoindent and ased editor
# Babylonian Square Side Rule calculator
# written on Windows XP on eTCL
# working under TCL version 8.5.6 and eTCL 1.0.1
# gold on TCL WIKI, 2oct2017
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 {{} {target number N :} }
lappend names {trial square root:}
lappend names {formula factor (usually 2 ) : }
lappend names {answers: intermediate term in formula }
lappend names {trial square :}
lappend names {square root from TCL sqrt function : }
lappend names {percentage error: }
lappend names {approximate square root from square side rule :}
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
set msg "Calculator for Square Side Rule
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 calculate {     } {
global side1 side2 side3 side4 side5
global side6 side7 side8
global testcase_number
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. ]
set target_number \$side1
set trial_square_root \$side2
set formula_factor \$side3
set approximate_root [* [/ 1. \$formula_factor ] [+ [* [/ 1. \$trial_square_root ] \$target_number] \$trial_square_root ]   ]
set side4 [+ [* [/ 1. \$trial_square_root ] \$target_number] \$trial_square_root ]
set side5 [* \$trial_square_root \$trial_square_root]
set side6 [sqrt \$target_number ]
set side7 [ errorx \$approximate_root [sqrt  \$target_number ]   ]
set side8 \$approximate_root
}
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 side1 side2 side3 side4 side5
global side6 side7 side8
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 :|target number N  |   |&"
puts "&| \$side2 :|trial square root | |& "
puts "&| \$side3 :|formula factor (usually 2 )| |& "
puts "&| \$side4 :|answers: intermediate term in formula| |&"
puts "&| \$side5 :|trial square | |&"
puts "&| \$side6 :|square root from TCL sqrt function |  |&"
puts "&| \$side7 :|percentage error |  |&"
puts "&| \$side8 :|approximate square root from square side rule |  |&"
}
frame .buttons -bg aquamarine4
::ttk::button .calculator -text "Solve" -command { set side8 0 ; calculate   }
::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 2.  2.0  2.0 3.0  4.0   1.414    6.066  1.5}
::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 10.0 3.0 2.0 6.333 9.0   3.162 0.138     3.166}
::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 100.0 9.0 2.0  20.111  81.0    10.0  0.5555  10.0555}
::ttk::button .clearallx -text clear -command {clearx }
::ttk::button .cons -text report -command { reportx }
::ttk::button .exit -text exit -command {exit}
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 Square Side Rule 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 Newton's Method

see SQUARE ROOT on wiki

```        # gold on TCL WIKI, 2oct2017
# console program for square root
package require Tk
package require math::numtheory
set tcl_precision 17
console show
namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
proc square_root_function { number_for_root  } {
set counter 0
set epsilon .0001
while { \$counter < 50.  } {
if { [* \$counter \$counter 1. ]   > [* \$number_for_root 1.] } {break}
incr counter
}
set  square_root_estimate \$counter
while {1} {
set keeper \$square_root_estimate
set starter \$square_root_estimate
set remainder   [* \$starter \$starter  1. ]
set remainder [- \$number_for_root [* \$starter \$starter  1. ] ]
set  square_root_estimate  [+ \$starter [/ \$remainder [* 2. \$starter ]]]
if {abs(\$keeper - \$square_root_estimate) < \$epsilon} break
}
return \$square_root_estimate
}
puts " [ square_root_function 10. ] "```

### console program for brackets of root

```                # pretty print from autoindent and ased editor
# console program for token multiplication and square root
# working under TCL version 8.5.6 and eTCL 1.0.1
# program written on Windows XP on eTCL
# gold on TCL WIKI, 10Mar2017
package require Tk
package require math::numtheory
namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
set tcl_precision 17
console show
global keeper target_number keep_under
set keeper 0
proc square_root_functionx { number_for_root  } {
global keeper target_number keep_under
set counter 0
set epsilon .0001
while { \$counter < 1000.  } {
set keeper [* \$counter \$counter 1. ]
set target_number \$number_for_root
if { [* \$counter \$counter 1. ]   > [* \$number_for_root 1.] } {break}
set keep_under \$counter
incr counter
}
return \$counter}
puts " function gives [ square_root_functionx 10. ] ,  root for \$target_number between \$keep_under and  [ square_root_functionx 10. ] , max square \$keeper "
#returns positive integers under and over root.
# function gives 4 ,  root for 10. between 3 and  4 , max square 16.0 ```