|
2008-07-11
| ||
| 06:01 | • Closed ticket [2015452fff]: Negative integer division is wrong if there is a rem... artifact: 4b9ceae5fc user: karll | |
| 05:44 | • Pending ticket [2015452fff]. artifact: 16298620ca user: msofer | |
| 05:33 | • Ticket [2015452fff]: 4 changes artifact: 0871dda0a4 user: karll | |
|
2008-07-10
| ||
| 22:27 | • New ticket [2015452fff]. artifact: c27113cc10 user: karll | |
Negative integer division is wrong if there is a remainder
Closed / Invalid2015452fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2008-07-10 22:27:41
If I divide a negative integer by an integer and there is a remainder, Tcl will round the result downward. This has been replicated on Mac and FreeBSD.
tclsh8.4
% expr -501/500
-2
% expr -1000001/1000000
-2
Let's try a C program to see if I am crazy.
$ cat foo.c
main()
{
int i = -86401 / 86400;
printf("%d\n", i);
}
$ cc foo.c
$ ./a.out
-1
It works as expected.
karllplain2008-07-11 05:33:19
Logged In: YES user_id=64603 Originator: YES % expr -1/3 -1 % expr -1/1000000 -1
msoferplain2008-07-11 05:44:29
Logged In: YES user_id=148712 Originator: NO The problem is with the expectation! This is the behaviour by design. From the expr man page: "Multiply, divide, remainder. None of these operators may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor." The observed behaviour is implied by this and general identity for integers a = (a/b)*b + (a%b) Note that the C standard makes no such promises about the sign of the remainder, different implementations may give different results. Note also that there is no "rounding", neither up nor down: this is just the way that integer division is defined.
karll Closed ticket [2015452fff].
[History diff]2008-07-11 06:01:54
Log in (anonymous is fine) to add a comment.