Tcl Source Code

View Ticket
Login
Ticket UUID: 1273713
Title: Expr with acos
Type: Bug Version: obsolete: 8.4.11
Submitter: alvinpleung Created on: 2005-08-26 00:38:28
Subsystem: 16. Commands A-H Assigned To: msofer
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2005-09-11 09:20:10
Resolution: Invalid Closed By: sf-robot
    Closed on: 2005-09-11 02:20:10
Description:
set factor2 [expr ($x0x1 - $y1_sqrt_factor1) *
$denominator]
[expr acos($factor2)]
works
set factor2 [expr acos(($x0x1 - $y1_sqrt_factor1) *
$denominator)]
failed with error message
domain error: argument not in valid range
User Comments: sf-robot added on 2005-09-11 09:20:10:
Logged In: YES 
user_id=1312539

This Tracker item was closed automatically by the system. It was
previously set to a Pending status, and the original submitter
did not respond within 14 days (the time period specified by
the administrator of this Tracker).

dkf added on 2005-08-28 00:51:18:
Logged In: YES 
user_id=79902

Note for people reading this in the future: in 8.5, the
right thing to do will be to leave tcl_precision set to its
default value. The advice on setting it to 17 is only
correct up to 8.4 (or rather some version from the alpha
cycle of 8.5, but that's an internal matter).

msofer added on 2005-08-27 00:58:58:
Logged In: YES 
user_id=148712

Oops - sorry, had not seen the attached file. I did take a
look at the new "Full test case" (it is missing a paren in
line 51, but this is irrelevant for this bug report)

The problem is caused by the fact that your expressions in
line 50 and 51 are not {}'quoted. This causes the values of
the variables to be re-read from their string
representation, with some loss in precision if tcl_precision
is less than 17 (it is 12 by default). This is not really a bug.

So there are two possible fixes (and I recommend using both):

(1) do {}'quote your expressions: it is much faster, it does
not produce these effects, and is almost always the right
thing to do. There are few cases where you may want two
substitution rounds (one by the tcl interpreter, one by the
[expr]) interpreter). If that is what you're after, it will
be clear to you.

(2) do 'set ::tcl_precision 17' when working with doubles
(but see http://www.tcl.tk/man/tcl8.4/TclCmd/tclvars.htm#M39
for some gotchas)

alvinpleung added on 2005-08-27 00:33:01:

File Added - 147105: bug.tcl

alvinpleung added on 2005-08-27 00:33:00:
Logged In: YES 
user_id=958624

I already attached the complete test case in the original
bug report.  It is currently shown in the "Attached Files"
section with description "test case."
The problem case is when ($a - $b) * $c eval to 1.0.
I will attach the test case again.

dkf added on 2005-08-26 15:46:48:
Logged In: YES 
user_id=79902

Sounds as if your code is working at very close to the
boundary values, which is usually problematic.

FWIW, when I'm working out angles, I try to atan2() which
has far fewer annoying cases than the rest of the inverse
trig functions.

msofer added on 2005-08-26 08:13:32:
Logged In: YES 
user_id=148712

Can you provide a complete session log, including the values
of your variables? Are you sure that they have the same
values in both cases? The error message indicates that (Tcl
thinks that) the argument to 'acos' is not between -1 and 1 

% set a 2
2
% set b 1
1
% set c 0.5
0.5
% set f [expr acos(($a-$b)*$c)]
1.0471975512
% set c 2
2
% set f [expr acos(($a-$b)*$c)]
domain error: argument not in valid range

alvinpleung added on 2005-08-26 07:38:29:

File Added - 147014: bug.tcl

Attachments: