| |||||||||||
TCL - Comparison between numerals Posted by chaitanya_tcs (chaitanya_tcs), 17 December 2007 Hi,I'm trying to write a script to compare a few numerals, but it seems to be acting very strange. At times the comparison is Ok and at times it is not. I have not been able to identify any pattern. Is it possible to declare numerals in TCL? Can someone help me on this? A small prototype of my code: set x 0 set h 0 set ReturnCode 16 set jobtime "292.16 403.62 144.92 0.20 0.13 0.13 0.13 0.13 0.13 0.13 0.13 0.13 155.22 47.79 57.86 18.56" set acctime "00000292.16 00000695.78 00000840.70 00000840.90 00000841.03 00000841.16 00000841.29 00000841.42 00000841.55 00000841.68 00000841.81 00000841.94 00000997.16 00001044.95 00001102.81 00001121.37" for { set n 0;set i 1 } {$i < $ReturnCode} { incr n;incr i} { set z [expr "[lindex $acctime $n]+ [lindex $jobtime $i]"] set y [lindex $acctime $i] puts "$z" puts "$y" if { $y == $z } { set x [expr $x + 1] puts "x is $x" } else { set h [expr $h + 1] puts "h is $h" } } puts "$n" puts "$x" puts "$h" Thanks, Chaitanya Posted by admin (Graham Ellis), 17 December 2007 Yes - ran that as you described for me, but when I compared the numbers in a straightford way the problem was not exhibited.I think your problem is in the addition, and in rounding. If you add one third and two thirds - 0.3333333333 and 0.6666666 you get 0.9999999 whereas you should get 1.00000000 - and Tcl's comparision shows those as different values. Your addition of values from $acctime and $jobtime may have the same problem, and rather than testing for equality (which is dangerous on a float in ANY language!) you should test whether the difference is greater that a ver small value. if {abs($y -$z) < 0.001} { .... This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |