Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho