Topic: Expressions
Online Help
Functions
Library (built-in) functions
Calcpad includes a library with common math functions, ready to use:
- Trigonometric:
sin(x)
- sine;cos(x)
- cosine;tan(x)
- tangent =sin(x)/cos(x)
, for each x ≠ kπ, k=1, 2, 3…;csc(x)
- cosecant =1/sin(x)
, for each x ≠ kπ, k=1, 2, 3…;sec(x)
- secant =1/cos(x)
, for each x ≠ π/2 + kπ, k=1, 2, 3…;cot(x)
- cotangent =cos(x)/sin(x)
, for each x ≠ π/2 + kπ, k=1, 2, 3…;
- Hyperbolic:
sinh(x)
- hyperbolic sine =(ex - e-x)/2
;cosh(x)
- hyperbolic cosine =(ex + e-x)/2
;tanh(x)
- hyperbolic tangent =(ex - e-x)/(ex + e-x)
;csch(x)
- hyperbolic cosecant =1/sinh(x)
;sech(x)
- hyperbolic secant =1/cosh(x)
;coth(x)
- hyperbolic cotangent =(ex + e-x)/(ex - e-x), for x ≠ 0
;
- Inverse trigonometric:
asin(x)
- inverted sine, defined for -1 ≤ x ≤ 1;acos(x)
- inverted cosine, defined for -1 ≤ x ≤ 1;atan(x)
- inverted tangent;atan2(x; y)
- the angle whose tangent is the quotient of y and x;acsc(x)
- inverted cosecant =asin(1/x)
;asec(x)
- inverted secant =acos(1/x)
;acot(x)
- inverted cotangent;
- Inverse hyperbolic:
asinh (x)
- inverted hyperbolic sine =ln(x + √x2 + 1)
, defined for -∞ ≤ x ≤ +∞;acosh(x)
- inverted hyperbolic cosine =ln(x + √x + 1·√x - 1)
, defined for x ≥ 1;atanh(x)
- inverted hyperbolic tangent =1/2·ln[(1 + x)/(1 - x)]
, for -1 < x < 1;acsch(x)
- inverted hyperbolic cosecant =atanh(1/x)
;asech(x)
- inverted hyperbolic secant =acosh(1/x)
;acoth(x)
- inverted hyperbolic cotangent =1/2·ln[(x + 1)/(x - 1)]
, for |x| > 1;
- Logarithmic, exponential and roots:
log(x)
- decimal logarithm (with base 10), for each x > 0;ln(x)
- natural logarithm (with base e ≈ 2.7183), for each x > 0;log_2(x)
- binary logarithm (with base 2), for each x > 0;exp(x)
- exponential function = e ˣ;sqr(x)
orsqrt(x)
- square root (√x ), defined for each x ≥ 0;cbrt(x)
- cubic root ( 3√x );root(x; n)
- n-th root ( n√x );
- Rounding:
round(x)
- rounds to the nearest integer;floor(x)
- rounds to the smaller integer;ceiling(x)
- rounds to the greater integer;trunc(x)
- rounds to the nearest integer towards zero;
- Integer:
mod(x; y)
- the reminder of an integer division;gcd(x; y; z…)
- the greatest common divisor of several integers;lcm(x; y; z…)
- the least common multiple of several integers;
- Complex:
abs(x)
- absolute value (modulus) of a real number | x |;abs(a + bi)
- complex modulus = sqrt(a2 + b2);re(a + bi)
- returns the real part only, re(a + bi) = a;im(a + bi)
- returns the imaginary part as a real number, im(a + bi) = b;phase(a + bi)
- complex number phase (argument) = atan2(a; b).
- Aggregate and interpolation:
min(x; y; z…)
- the smallest of multiple values;max(x; y; z…)
- the greatest of of multiple values;sum(x; y; z…)
- sum of multiple values= x + y + z…
;sumsq(x; y; z…)
- sum of squares= x² + y² + z²…
;srss(x; y; z…)
- square root of sum of squares= sqrt(x² + y² + z²…)
;average(x; y; z…)
- average of multiple values= (x + y + z…)/n
;product(x; y; z…)
- product of multiple values= x·y·z…;
mean(x; y; z…)
- geometric mean= n-th root(x·y·z…)
;take(n; a; b; c…)
- returns the n-th element from the list;line(x; a; b; c…)
- performs linear interpolation among the specified values for parameter x;spline(x; a; b; c…)
- performs Hermite spline interpolation;
- Conditional and logical:
if(<cond>; <value-if-true>; <value-if-false>)
- if the condition cond is satisfied, the function returns the first value, otherwise it returns the second value. The condition is satisfied when it evaluates to any non-zero number;switch(<cond1>; <value1>; <cond2>; <value2>;…; <default-value>)
- returns the value for which the respective condition is satisfied. Conditions are checked from left to right. If none is satisfied, it returns the default value in the end;not(x)
- logical "not";and(x; y; z…)
- logical "AND";or(x; y; z…)
- logical "OR";xor(x; y; z…)
- logical "XOR";
- Other:
sign(x)
- sign of a number = -1, if x < 0; 1, if x > 0, 0 if x = 0;random(x)
- a random number between 0 and x.
Arguments must be enclosed in round brackets. They can be constants, variables or any valid expression. Multiple arguments must be separated by semicolons ";". When arguments are out of range, the function returns "Undefined". Exceptions from this rule are "cot(0)" and "coth(0)", which return "+∞".
Arguments of trigonometric functions can be in degrees, radians or grades. The units for angles can be specified in three different ways:
1. By the radio buttons above the output window (🔘D, 🔘R, 🔘G).
2. By compiler switches inside the code. You have to insert a separate line containing: #deg for degrees, #rad for radians or #gra for grades. This will affect all expressions after the current line to the end or until an alternative directive is found.
3. By attaching native units to the value itself: deg, °, ′, ″, rad, grad, rev (see the “Units” section, further in this manual).
Native units are of highest priority, followed by compiler switches in source code. Both override radio buttons settings, which are of lowest priority.
All functions are also defined in the complex domain, except for mod(), gcd(), lcm(), min() and max(). Rounding functions affect both real and imaginary part.
Logical functions accept numerical values and return “0” for “false” and “1” for “true”. The results for two arguments (one for “not”) are presented in the following tables:
|
|
|
|
Any numerical value, different from 0, is treated as 1 (true). Multiple arguments are evaluated sequentially from left to right, according to the above tables. We start with the first and the second. Then, the obtained result and the next value are evaluated in turn, and so on.
Rounding of midpoint values with round() evaluates to the nearest integer away from zero. The floor() function rounds to the smaller value (towards -∞). The ceiling() function rounds in the opposite direction to the larger value (towards +∞). Unlike floor(), trunc() rounds towards zero, which is equivalent to simply truncating the fractional part. Some examples for rounding of negative and positive numbers are provided in the table bellow:
Positive
|
Negative
|
Custom (user defined) functions
You can define your own functions, and use them further in the calculations. Custom functions can have unlimited number of parameters. They are specified after the function name, enclosed in brackets "(" ... ")" and separated by semicolons ";". Each function is defined, using the following format: "f ( x; y; z; ... ) = expression", where "f" is the function name and "x", "y" and "z" are function parameters. On the right side you can have any valid expression including constants, operators, variables and even other functions, e.g.:
f(x) = x^2 + 2*x*sin(x)
g(x; y) = f(x)/(y - 4)
Once defined, you can use a function in any expression by inserting a function call. Just write the function name and then specify the arguments in brackets, e. g. b = g(a + 2; 3) + 3. Function names must conform to the same rules as variable names. Arguments can be any valid expressions. You have to provide as many arguments as the number of function parameters. The life cycle of a function is from the place of definition to the end of the code. If you define a new function with the same name, the old one will be replaced. You cannot redefine a library function. For example, sin(x) = x^2 will return an error.
It is not necessary to pre-define the variables that are used for parameters. However, if other variables are used inside the function body, they must be defined before the first call to the function. Parameters work as local level variables inside the function body. If a variable with the same name exists outside the function, a call to that function will not rewrite the value of the global variable. For example:
- If you have a variable "x = 4"
- and a function "f(x) = x2".
- When you call "f(2)", it will evaluate to x2 = 22 = 4, because local x = 2
- If you call "x2" after that, it will return x2 = 42 = 16, because global x remains 4.
User defined functions support both real and complex numbers.
Table of contents
-
+
About Calcpad
-
+
Writing code
-
+
Coding aids
-
−
Expressions
-
+
Reporting
-
+
Programming
-
+
Results
-
+
Working with files