Vaibhav Kumar <
vk53...@gmail.com> wrote:
> proc CircleFit { X Y } {
> set n [llength $X]
> catch {
> unset XX ;
> unset YY ;
> unset XY ;
> unset XXY ;
> unset YYY ;
> unset XXX ;
> unset XYY
> }
Since you are using a proc, none of these variables exist until you set
them, so 'unsetting' them here is redundant.
Additionally, there is no need for a catch, read about the -nocomplain
option to unset in the documentation. Also, note that unset can unset
plural names at once, so all of the above in the catch, plus the catch,
can become one single line.
> set sumX 0 ;
> set sumY 0 ;
> set sumXX 0 ;
> set sumYY 0 ;
> set sumXY 0 ;
> set sumXXY 0 ;
> set sumYYY 0 ;
> set sumXXX 0 ;
> set sumXYY 0 ;
> set sumXXplusYY 0 ;
> set sumXXYplusYYY 0 ;
> set sumXXXplusXYY 0;
This is a good use for a foreach look setting variables by name:
foreach var {sumX sumY sumXX sumYY sumXY sumXXY sumYYY sumXXX sumXYY
sumXXplusYY sumXXYplusYYY sumXXXplusXYY} {
set $var 0
}
> set nsumXXplusYY -[expr {$sumXX + $sumYY}]
> set nsumXXYplusYYY -[expr {$sumXXY + $sumYYY}]
> set nsumXXXplusXYY -[expr {$sumXXX + $sumXYY}]
Don't place your negation outside of the expr expansion, that causes
your numbers to shimmer between numeric types and string types on each
loop cycle (i.e., your code wastes cycles needlessly converting
numerics to strings and back to numerics later). Perform the negation
inside the expression, then the variables will remain numeric types.
> -------------------
> domain error: argument not in valid range
> while executing
> "expr {double($A($k,$i)) / double($A($i,$i))}"
> (procedure "CircleFit" line 64)
> invoked from within
> "CircleFit {10 20 30 40} {10 20 30 40}"
> invoked from within
> "puts [CircleFit {10 20 30 40} {10 20 30 40}]"
> (file "testTCL.tcl" line 86)
Two ways to debug this are:
1) place a temporary puts statement (best to use stderr as the file
descriptor) just prior to this command that prints the values contained
in A($k,$i) and A($i,$i), then you can see what values are inside that
is causing trouble.
2) place a temporary catch around the expr, test the result of the
catch with if to detect an error condition, and puts the values of
A($k,$i) and A($i,$i) when an error condition was detected (as well as
halting at that point).
When you see what values were going in that caused the problem, you'll
be better able to trace back to how they were created.