Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2010-02-23
07:00 • Closed ticket [2954959fff]: expr abs(0.0) is -0.0 plus 6 other changes artifact: d04afddc44 user: nijtmans
2010-02-22
22:10 • Ticket [2954959fff]: 4 changes artifact: b7d72bd9b4 user: nijtmans
22:06 • Ticket [2954959fff]: 4 changes artifact: 93fe7b7ea7 user: nijtmans
21:50 • Ticket [2954959fff]: 4 changes artifact: c4ffc3d912 user: dkf
17:45 • Ticket [2954959fff]: 5 changes artifact: 7309fa5240 user: nijtmans
2010-02-21
16:21 • Ticket [2954959fff]: 3 changes artifact: 7711fb6cb8 user: dkf
15:59 • Ticket [2954959fff]: 4 changes artifact: f09bc8a449 user: nijtmans
2010-02-20
06:39 • Ticket [2954959fff]: 4 changes artifact: d4034beb2f user: nijtmans
2010-02-19
22:52 • Ticket [2954959fff]: 5 changes artifact: 90b5af6ca4 user: dgp
21:09 • Ticket [2954959fff]: 4 changes artifact: 7b4f9b6338 user: nijtmans
13:43 • New ticket [2954959fff]. artifact: 84de7b02fe user: fridolin

expr abs(0.0) is -0.0

Closed / Fixed2954959fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 17 years ago
Descriptionplain2010-02-19 13:43:08
% expr abs(0.0)
-0.0

Tcl version is 8.5.5, but it guess it's also in CVS (core-8-5-branch):

tclBasic.c (abs function)
 7437 if (d <= 0.0) {
 7438     Tcl_SetObjResult(interp, Tcl_NewDoubleObj(-d));
 7439 } else {
 7440     Tcl_SetObjResult(interp, objv[1]);
 7441 }

should be

7437 if (d < 0.0) {
 7438     Tcl_SetObjResult(interp, Tcl_NewDoubleObj(-d));
 7439 } else {
 7440     Tcl_SetObjResult(interp, objv[1]);
 7441 }
nijtmansplain2010-02-19 21:09:45
Yes, this problem is in head as well.

And the same for TCL_NUMBER_LONG. There
the "<=" should be "<" as well. The effect is only
the unnecessary creation of an object, but still.....
dgpplain2010-02-19 22:52:42
Please fix.  Thanks!
nijtmansplain2010-02-20 06:39:06
Turns out not that trivial to fix. See: also [Bug 1893815]
tcl::mathfunc commands and non-canonical string reps
nijtmansplain2010-02-21 15:59:05
Fixed in HEAD, to be backported to 8.5 and 8.4
dkf Ticket [2954959fff]: 3 changes [History diff]2010-02-21 16:21:14
nijtmansplain2010-02-22 17:45:35
Added some more tests, just to prove that the TCL_NUMBER_LONG was not quite right either. The problem
is that in ieee floating point math, -0.0 == 0.0 gives true! This means that you cannot really distinguish
-0.0 and 0.0 in floating point math.

Looking at the test cases, the rule seems to be:
- If the input looks negative, take the negative of the output and take that as a result.
- otherwise, take the unchanged input.

No, what looks negative? For example, -0.0 and -0 look negative, but really are
not: they are both forms of zero! Still, we want to output resp. 0.0 and 0 in those
cases. So, we need to distinguish between -0.0 and 0.0 (in the float case) and
between 0 and -0 (in the long case). How do we do that.

In the float case, we can use memcmp(). In the long case, I see no other way to look
for ´-´ characters (if there is a string representation). Anyway, this way I could
make all test cases pass.

For 8.4 the story is a little bit different. The only 'mistake' was that abs(-0.0) returns
-0.0, which is in fact correct, but not expected regarding the above rules. Numbers
like 1e-324 result in an underflow immediately in 8.4, it seems not worth to
fix that. But the abs(-0.0) case still can be fixed, therefore leaving this issue open.
dkfplain2010-02-22 21:50:55
In the 'long' case, if you've got -0 then you're on a ones-complement machine and we no longer guarantee Tcl to work right. :-)
nijtmansplain2010-02-22 22:06:23
>In the 'long' case, if you've got -0 then you're on a ones-complement
>machine and we no longer guarantee Tcl to work right. :-)

What happened, is that:
expr {abs(0x0)}    -->    0
expr {abs(0x1)}    -->    0x1
I would at least expect the two answers to
have the same form.... That's what this fix
is about in the "long" case
nijtmansplain2010-02-22 22:10:50
Oh no, I meant
::tcl::mathfunc::abs 0x0  --> 0   (old output)   0x0 (new output)
::tcl::mathfunc::abs 0x1  --> 0x1
That's what this fix is about regarding longs in Tcl 8.5/8.6. In
Tcl 8.4 there is no ::tcl::mathfunc::abs, so this is no issue.
nijtmansplain2010-02-23 07:00:21
allow_comments - 1

Relevant test cases backported to Tcl 8.4 as well, and minor
fix applied to tclExecute.c to make them all pass.
DONE
Log in (anonymous is fine) to add a comment.