Skip to content
Tcl Source Code View Ticket
Login

View Ticket

expr.n should document integer division rounding

Closed / Fixed2931407fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 15 years ago
Descriptionplain2010-01-13 15:25:14
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.
dkfplain2010-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 Ticket [2931407fff]: 1 change [History diff]2010-01-14 00:30:38
dkfplain2012-05-29 16:49:15
allow_comments - 1
Log in (anonymous is fine) to add a comment.