12. MATH FUNCTION INSTRUCTIONS

12.1. Type of Math Functions

Usable instructions of math functions are as follows.

Table 7. Usable instruction of math function

Function Explanation Restriction of Argument
SQRT[x] Square root 0 <= x
SIN[x] Trigonometric Function Units are Degrees None
COS[x] None
TAN[x] Except 90,270±360n
ASIN[x] -1 <= x <= 1
ACOS[x] -1 <= x <= 1
ATAN[x] None
ATAN2[x,y] Except x=0,y=0
LN[x] Natural Logarithm 0 < x
EXP[x] Exponent None
ABS[x] Absolute None
TRUNC[x] Truncate -2.1*10^9 <= x <=2.1*10^9
ROUND[x] Round off -2.1*10^9 <= x <=2.1*10^9

  • Units of aforementioned functions are degrees.

  • Conversion functions are not supported because the user can achieve them by multiplying a constant value(57.29579 or 0.017453 based on requirement).

12.2. Instruction Format of Math Function

Instruction type of math functions is explained in the following example.

12.2.1. Instruction Format of Assignment Statements

  • The function which only has one argument(Except of ATAN2[ ])

  • The function which has two arguments(ATAN2[ ])

  • The argument of math functions is “Register “ and “Argument Register.”

  • Constant is not used directly.

12.2.2. Instruction Format of Relational Statements

  • A mixed logic instruction expression can be used in the conditional statement of a conditional branch command.

  • If the result of a conditional statement is ON, the executable statement of the conditional branch command is executed.

  • The argument of math functions is “Register “ and “Argument Register.”

  • A Constant is not used directly.

12.2.3. Instruction Format of Wait Command Statements

  • A mixed logic expression can be specified in the conditional statement of a wait command.

  • Wait until the specified condition is satisfied.

  • The argument of math functions is “Register “ and “Argument Register.”

  • A Constant is not used directly.

12.3. Function Specification of Math Functions

12.3.1. Square Root (SQRT)

Function name: SQRT[X] Argument: A positive integer or a positive real value to calculate square root. This function calculates the positive square root of the argument. Example:

Execute this TP program then,

If the argument is minus value, then the following error occurs.

12.3.2. Trigonometric Function (SIN)

Function name: SIN[X] Argument: An angle to calculate SIN (in degree). An integer or a real value. This function calculates sine of the argument. Example:

Execute this TP program then,

12.3.3. Trigonometric Function (COS)

Function name: COS[X] Argument: An angle to calculate COS (in degree). An integer or a real value. This function calculates cosine of the argument. Example:

Execute this TP program then,

12.3.4. Trigonometric Function (TAN)

Function name: TAN[X] Argument: An angle to calculate TAN (in degree). An integer or a real value. This function calculates tangent of the arguments. Example:

Execute this TP program then,

If the argument equals to 180*n+90 and execute, then the following Error will be posted.

12.3.5. Inverse Trigonometric Function (ASIN)

Function name: ASIN[x] Argument: An integer or a real value to calculate ASIN. (Return value is degree.) This function calculates arc sine of the argument. Example:

Execute this TP program then,

If the argument ranges are x>1 or x<-1 and execute program, then the following Error will be posted.

12.3.6. Inverse Trigonometric Function (ACOS)

Function name: ACOS[x] Argument: An integer or a real value to calculate ACOS. (Return value is degree.) This function calculates arc cosine of the argument. Example:

Execute this TP program then,

If the argument ranges are x>1 or x<-1 and execute program, then the following Error will be posted.

12.3.7. Inverse Trigonometric Function (ATAN2)

Function name: ATAN2[x,y] 1st argument: x coordinates of the point to calculate ATAN2. An integer or a real value. 2nd argument: y coordinates of the point to calculate ATAN2. An integer or a real value. This function calculates arc tangent of the 1st and 2nd arguments. (Return value is degree.) Example:

Execute this TP program then,

12.3.8. Inverse Trigonometric Function (ATAN)

Function name: ATAN [x] Argument: An integer or a real value to calculate ATAN. Example:

Execute this TP program then,

12.3.9. Exponent

Function name: EXP[x] Argument: An integer or a real value to calculate EXP. This function calculates a value equal to e (approximately 2.71828) raised to the power specified by the argument. Example:

Execute this TP program then,

12.3.10. Natural Logarithm

Function name: LN[x] Argument: An integer or a real value to calculate LN. This function calculates the natural logarithm of the argument. Example:

Execute this TP program then,

If the argument is x <= 0 and the program execute, then the following Error will be posted.

12.3.11. Absolute (ABS)

Function name: ABS[x] Argument: An integer or a real value to calculate ABS. This function calculates the absolute value of the argument. Example:

Execute this TP program then,

12.3.12. Truncate (TRUNC)

Function name: TRUNC[x] Argument: A real value to calculate TRUNC. This function converts the real argument to an integer by removing fractional part of the real value. Example:

Execute this TP program then,

12.3.13. Round Off (ROUND)

12.4. Background Operation of Math Function

Math function instructions are part of the mixed logic instruction. Then Math function instruction can be used in background operation.

Table 8. Background operation of math function

Mode Maximum Allowable Number of Items Scanning Time Available Data Available Operators
Standard mode No restriction (Number of items in standard mode / 300-Number of items in high-level mode)*ITP The number of items as used here refers to the total number of items in each mode background operation programs. One ITP is usually eight milliseconds. SIN, COS, TAN, ASIN, ACOS, ATAN, ATAN2, SQRT, LN, EXP, ABS, TRUNC, ROUND (,), =, <>, <, <=, >, >=, +, -, *, /, DIV, MOD
High-level mode 270 Eight milliseconds.

  • The scanning time of Math function is 2 or 3 times of normal items, because Math function instruction needs more time to calculate.

  • The scanning time of ATAN2 instruction is 3 times of normal items.

  • The other math function instructions of scanning time are 2 times of normal items.

12.5. Teach Math Function Instruction

Math function can be used in mixed logic instruction. As an example, the teaching process of following statement is as follows.

Procedure 8. Examples of Teaching Math Function

  1. Press F1 key(INST).

  2. Select Register.

  3. Select mixed logic instructions “…=(…)”.

  4. Select register “R[ ]”. Then the cursor moves in the square bracket.

  5. Input index 1.

  6. Then the cursor moves to the right side of equal sign.

  7. Select “SIN[ ]”.

  8. Then the cursor moves in the square bracket. And the default value R[…] is selected. Arguments are “Register “ and “Argument Register”. (Not Constant) The “Register” is default value of argument. So to use “Argument Register”, press F3(CHOICE) then select “Argument Register”.

  9. For example Register[2] is used here. Input value 2 then the cursor moves to the right parentheses.

  10. Move cursor to finish teaching.

  11. Teaching of SIN[R[2]] function completed.

12.6. Restriction of Teaching Math Function

  • The argument of math functions is “Register “ and “Argument Register”.

  • Constant is not used directly.

  • If the user wants to use constant, input constant to a register, and use the register.

  • You can put multiple math functions in a single line.

  • You cannot put math functions in a math function.

The following statements are supported.

The following statements are not supported.

  • Math function might have a negligible calculation error of 10-7 due to internal calculation error because math function follows the same specification of KAREL Built-Ins.

  • For example the result of COS[R[1]] (when R[1]=90), may be 10-8 that is not exactly zero. This behavior can be avoided by rounding off the result using ROUND function.

12.7. Exceptions and Restriction

Math function instruction posts the alarm in the following conditions. ・

  • When the argument exceeds the domain of function definition. Example :LN[R[1]] (R[1] = -1) :ASIN[R[2]] (R[2] = 10) :SQRT[R[3]] (R[3] = -4) :TAN[R[4]] (R[4] = 90)

  • The value overflows when executed. Example :EXP[R[1]] (R[1] = 100)

  • If the variables are divided by zero then, the following error will be occurred. Example :R[1] / SIN[R[2]] (R[2] = 0) :10/COS[R[3]] (R[3] = 90)