## tan

A function in the expr command that computes the tangent of an angle. The angle is expressed in radians.

2007-11-01 Harm Olthof: I was just wondering how to get tan return +/-Inf (and what choice it would make for the sign).

``` 59 % expr tan(acos(0))
16331778728383844.0
60 % expr tan(acos(0.0))
16331778728383844.0
61 % set pi 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
62 % expr tan(double(\$pi/2.0))
16331778728383844.0```

DGP Somewhat more direct demonstration of the same thing:

``` % expr tan(atan(Inf))
16331778728383844.0```

The arctan of infinity should be pi/2 radians. However, the value pi/2 is transcendental, with no accurate representation in a finite number of digits.

Computers by their nature represent numbers with only a finite number of digits, so atan(Inf) returns the best approximation it can:

``` % expr atan(Inf)
1.5707963267948966```

Taking the tangent of that value which is near, but not exactly pi/2, yields an answer which is large, but not Inf.

This is all a natural expected consequence of the limited precision of floating point number representations in computers. It is not particular to Tcl. Calling the equivalent C functions shows the same thing.

2007-11-04 Harm Olthof: Still, from an engineering point of view, shouldn't it be possible to specify an approximation of pi/2 with enough digits such that the tangent will become so large as too return Inf? Because what I understand from the man pages is that Inf is just a flag for over- or underflow. Is that not possible in Tcl to specify pi/2 with the necessary amount of precision or is there a limitation in the implementation of tan? Or to go back to my original question "how to get tan return Inf?". Sorry to see from your example, the expr parser doesn't seem to pick up the tan/atan combination and just return Inf.

DKF: In theory maybe, but Tcl just uses the standard C math library for this; doesn't seem sensible to change either (super-high precision floating point is non-trivial).

 Category Function Category Mathematics