Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 995e2537b169c3c03d2bc31df9c61aecf98cd98d
Ticket: 218045ffffffffffffffffffffffffffffffffff
% operator gives incorrect answer.
User & Date: nobody on 2000-10-26 00:52:32
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 1384 Bug
    Version: 8.0.3
    SubmitDate: '1999-03-02'
    LastModified: '1999-08-07'
    Severity: MED
    Status: Closed
    Submitter: pat
    ChangedBy: hobbs
    OS: HP-UX
    OSVersion: 10.20
    Machine: Other
    FixedDate: '2000-10-25'
    ClosedDate: '1999-08-07'
    
    
    Name:
    Bob Fletcher
    
    Extensions:
    expect
    
    CustomShell:
    None
    
    ReproducibleScript:
    #!/bin/sh
    # the next line restarts using wish  exec wish "$0" "$@"
    
    for {set i -10} {$i <= 10} {incr i} {
            set x $i
            puts "i $i [expr {$i%10}] $x [expr {fmod($x,10.)}]"
    }       
    exit
    
    ObservedBehavior:
    This is the cl8.03 output for this expr % test.
    i -10 0 -10 0.0
    i -9 1 -9 -9.0
    i -8 2 -8 -8.0
    i -7 3 -7 -7.0
    i -6 4 -6 -6.0
    i -5 5 -5 -5.0
    i -4 6 -4 -4.0
    i -3 7 -3 -3.0
    i -2 8 -2 -2.0
    i -1 9 -1 -1.0
    i 0 0 0 0.0
    i 1 1 1 1.0
    i 2 2 2 2.0
    i 3 3 3 3.0
    i 4 4 4 4.0
    i 5 5 5 5.0
    i 6 6 6 6.0
    i 7 7 7 7.0
    i 8 8 8 8.0
    i 9 9 9 9.0
    i 10 0 10 0.0
    
    This is the output from a similar f77 code.
    i -10 0 -10.0 .0
     i -9 -9 -9.0 -9.0
     i -8 -8 -8.0 -8.0
     i -7 -7 -7.0 -7.0
     i -6 -6 -6.0 -6.0
     i -5 -5 -5.0 -5.0
     i -4 -4 -4.0 -4.0
     i -3 -3 -3.0 -3.0
     i -2 -2 -2.0 -2.0
     i -1 -1 -1.0 -1.0
     i 0 0 .0 .0
     i 1 1 1.0 1.0
     i 2 2 2.0 2.0
     i 3 3 3.0 3.0
     i 4 4 4.0 4.0
     i 5 5 5.0 5.0
     i 6 6 6.0 6.0
     i 7 7 7.0 7.0
     i 8 8 8.0 8.0
     i 9 9 9.0 9.0
     i 10 0 10.0 .0
    
    The tcl expr command gives, for negative numbers, gives different answers than the  tcl floating point  fmod command, and different answers from f77/C.
    We would expect % to give the same answer as fmod, when the floating point is equivalent to an int. 
    
    DesiredBehavior:
    We would expect % to give the same answer as fmod, when the floating point is equivalent to an int. 
    
    
    
    Note the comments in tclExecute.c:
    
    case INST_MOD:
        /*
         * This code is tricky: C doesn't guarantee much about
         * the quotient or remainder, but Tcl does. The
         * remainder always has the same sign as the divisor and
         * a smaller absolute value.
         */
    
    K&R states specfically (p41, 2nd ed) that the sign of the
    result for % is machine-dependent for negative operands.
    Thus Tcl only formalizes this with a more strict definition. 
    -- 08/07/1999 hobbs
    
  4. foundin changed to: "obsolete: 8.0.3"
  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: "% operator gives incorrect answer."
  11. type changed to: "Bug"