12.7  MATH FUNCTIONS

Tcl supports the following mathematical functions in expressions:

Operators      Description

+                     Addition

-                      Subtraction

*                      Multiplication

/                      Division

sin( )              Sine function; format is sin(A) where A is in radians

cos( )             Cosine function; format is cos(A) where A is in radians

tan( )             Tangent function. Converts radians to slope. Format is tan(A) where A is in radians. Returns a value less than 1.
example:

set x [expr [GETVAL degrees]*3.14159265/180]

set y [expr tan($x)]

SETVAL radians=[expr [GETVAL degrees]*3.14159265/180]

SETVAL slope=[expr $y]

 

atan( )           Arctangent function. Converts slope to radians. Format is atan(A) where A is the slope.  Atan returns a value in radians.
Example:

set x [expr [GETVAL slope]]

set y [expr atan($x)]

SETVAL radians=$y

SETVAL degrees=[expr $y*180/3.14159265]

 

sqrt( )            Square root; format is sqrt(A) where A is in radians

pow(A ,B)     Power; format is pow(A,B) for A to the power of B

log( )              Natural logarithm; format is log(A)

log10( )          Logarithm base 10; format is log10(A)

( )                    Precedence operator. Perform calculation inside parentheses before any operators outside parentheses

ceil(A)         Smallest integer >= A

floor(A)       Largest integer <= A

int(A)          Integer part of A

fmod(A,n)    Remainder A/n

 

acos 

asin

atan2

sinh

cosh 

hypot 

tanh

 

Note - the Trigonometric functions (sin, cos, tan, etc) use RADIANS as the output or expected input (not degrees).

 

To convert radians to degrees, multiply by 57.2957795

   or  multiply by 180/3.14159265.

 

To convert degrees to radians, multiply by 0.0175

   or  multiply by 3.14159265/180.

Example:

set x [expr [GETVAL slope]/100]

set y [expr atan($x)]

SETVAL radians=$y

SETVAL degrees=[expr $y*180/3.14159265]