Tcl Source Code

View Ticket
Login
Ticket UUID: 2931407
Title: expr.n should document integer division rounding
Type: Bug Created on: 2010-01-13 15:25:14
Submitter: cjmcdonald Assigned to: msofer
Subsystem: 47. Bytecode Compiler Severity:
Priority: 5 Medium Last modified: 2012-05-29 16:49:15
Status: Closed Closed by: dkf
Resolution: Fixed Closed on: 2012-05-29 09:49:15
Version: obsolete: 8.5.8
Description:
The expr.n man pages refers to a C manual for a description of the behaviour of arithmetic operators.

But, in Tcl [expr { -57/10 }] gives -6, rounding towards minus infinity.  This was a surprise to me, as most C implementations round towards zero, and -57/10 returns -5.  

Googling, the latest C standard ISO 9899:1999 actually specifies rounding towards zero in this case.  Previously it appears to have been "implementation dependant".  So we are now at odds with standard C here.

I think that the expr.n man page should document this behaviour.
User Comments:
dkf added on 2010-01-14 00:30:37:
It has to do with the behaviour of %, where Tcl guarantees that the sign of the result will be the same as the sign of the right hand side (and smaller in magnitude). Add in the constraint that
   X - X%Y = (X/Y)*Y
(which is a pretty sensible constraint!) and you get the behaviour that Tcl requires as a natural consequence.

dkf added on 2012-05-29 16:49:15:

allow_comments - 1