Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 33841e7bf8df4b332f1842564e6f1eba025a788a
Ticket: 218443ffffffffffffffffffffffffffffffffff
an if expression comparing a computed floating point value w
User & Date: nobody on 2000-10-26 00:57:39
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 6295 Bug
    Version: 8.3.2
    SubmitDate: '2000-09-21'
    LastModified: '2000-10-18'
    Severity: MED
    Status: Closed
    Submitter: techsupp
    ChangedBy: ericm
    OS: All Windows
    OSVersion: Windows NT 4.0
    Machine: Dell Dimension P200
    FixedDate: '2000-10-25'
    ClosedDate: '2000-10-18'
    
    
    Name:
    Jon Hilbert
    
    ReproducibleScript:
    for {set i 0} {$i < 10} {incr i} {set a($i) "0.10"}
    set total 0
    for {set i 0} {$i < 10} {incr i} {set total [expr {$total + $a($i)}]}
    if {$total != 1.0} {puts "what? why doesn't $total == 1.0 ?"}
    if {[expr $total] == 1.0} {puts "but \[expr $total] == 1.0 ?"}
    info patchlevel
    
    ObservedBehavior:
    as the script shows, the results of "if {$total != 1.0}" are not the same as "if {[expr $total] != 1.0}". I think they should be.
    
    
    
    Floating-point comparisons are notoriously unreliable.  Likely what you are hitting here is an artifact of the limitations of floating-point arithmetic inherent in all computer platforms.
    
    If you must perform floating-point equality comparisons, you are better off with something like:
    
    set tolerance 0.001
    if { 1.0 - $tolerance <= $total && $total <= 1.0 + $tolerance } {
    ...
    }
    
    That is, allow for a "slush factor" in the comparison by testing that the value is "tolerably close" to the expected value.
    
       Eric Melski                            The Other Tcl Guy
       ericm at ajubasolutions.com            Ajuba Solutions
    
    -- 10/18/2000 ericm
    
    
    
  4. foundin changed to: "obsolete: 8.3.2"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "69. Other"
  10. title changed to:
    an if expression comparing a computed floating point value w
    
  11. type changed to: "Bug"