Mathematical Functions
From Flowcode Help
Jump to navigationJump to searchOld Versions
This page is current for Flowcode v11 and later. Earlier versions can be found below:
| Flowcode v10 |
| Flowcode v9 |
| Flowcode v8 |
Introduction
As well as the basic mathematical operators (+, -, *, /), the following mathematical functions can be used in calculations. All of these functions are now usable on each chip type.
Functions
Flowcode includes an additional set of mathematical functions:
| Function Prototype | Description | Example |
|---|---|---|
| Floating Point Arithmetic | ||
| float = fadd(float, float) | Add two floating point numbers together | fadd(3.14, 2.86) = 6.0 |
| float = fsub(float, float) | Subtract two floating point numbers | fsub(10.5, 3.2) = 7.3 |
| float = fmul(float, float) | Multiply two floating point numbers | fmul(4.5, 2.0) = 9.0 |
| float = fdiv(float, float) | Divide two floating point numbers | fdiv(15.0, 3.0) = 5.0 |
| float = fmod(float, float) | MOD function for floating point numbers | fmod(7.5, 2.0) = 1.5 |
| byte = isinf(float) | Checks to see if the floating point number is infinite | isinf(1.0/0.0) = 1 |
| byte = isnan(float) | Checks to see if the floating point is not a number | isnan(0.0/0.0) = 1 |
| byte = float_eq(float, float) | Compares two floating point numbers to see if they are equal | float_eq(3.14, 3.14) = 1 |
| byte = float_ge(float, float) | Compares two floating point numbers to see if they are greater then or equal | float_ge(5.0, 3.0) = 1 |
| byte = float_gt(float, float) | Compares two floating point numbers to see if they are greater then | float_gt(5.0, 3.0) = 1 |
| byte = float_le(float, float) | Compares two floating point numbers to see if they are less then or equal | float_le(3.0, 5.0) = 1 |
| byte = float_lt(float, float) | Compares two floating point numbers to see if they are less then | float_lt(3.0, 5.0) = 1 |
| int = random() | Generates a random number (0-65535) | (random() MOD 10) + 1 = 7 (range 1-10) |
| Mathematical Functions | ||
| fabs( x ), floor( x ), ceil( x ) | Absolute value, floor and ceiling functions | fabs(-5.7) = 5.7 floor(4.9) = 4 ceil(4.1) = 5 |
| fmod( x , y ) | Floating point modulus (remainder of x divided by y) | fmod(25.5, 7.0) = 4.5 |
| sqrt( x ), cbrt( x ) | Square and cube roots | sqrt(25) = 5.0 cbrt(27) = 3.0 |
| log( x ), log10( x ) | Logarithms (base e and base 10) | log(2.718) ~ 1.0 log10(100) = 2.0 |
| exp( x ), pow( x , y ) | Exponential and power functions (x to the power of y) | exp(1) ~ 2.718 pow(2, 8) = 256 |
| sin( x ), cos( x ), tan( x ) | Trigonometric functions where x is in radians | sin(1.57) ~ 1.0 cos(0) = 1.0 tan(0.785) ~ 1.0 |
| asin( x ), acos( x ), atan( x ) | Inverse trigonometric functions where x is in radians | asin(0.5) ~ 0.524 acos(0.707) ~ 0.785 atan(1) ~ 0.785 |
| atan2( y , x ) | Four-quadrant inverse tangent | atan2(1, 1) = 0.785 |
| sinh( x ), cosh( x ), tanh( x ) | Hyperbolic functions | sinh(1) ~ 1.175 cosh(0) = 1.0 tanh(1) ~ 0.762 |
| isnan( x ), isinf( x ) | Tests for not-a-number and infinity | isnan(5.0) = 0 isinf(5.0) = 0 |
| round( x ) | Decimal rounding (x rounded to the nearest integer) | round(3.7) = 4 |
| fround( x , y ) | Floating point rounding (x rounded to y decimal places) | fround(3.14159, 2) = 3.14 |
| asinh( x ), acosh( x ), atanh( x ) | Inverse hyperbolic functions | asinh(1) ~ 0.881 acosh(1) = 0 atanh(0.5) ~ 0.549 |
| fact( x ) | Factorial (multiply all numbers down to 1) | fact(5) = 120 (5×4×3×2×1) |
| Integer Operations | ||
| MOD | Returns the remainder after division | 17 MOD 5 = 2 (17÷5 = 3 remainder 2) |
| Bitwise Operations | ||
| AND | Bitwise AND operation | 0b1100 AND 0b1010 = 0b1000 |
| OR | Bitwise OR operation | 0b1100 OR 0b1010 = 0b1110 |
| XOR | Bitwise XOR (exclusive OR) operation | 0b1100 XOR 0b1010 = 0b0110 |
| NOT | Bitwise NOT operation | NOT 0b1100 = 0b0011 |
| << | Left shift operation (multiply by 2^n) | 5 << 2 = 20 (5 × 4) |
| >> | Right shift operation (divide by 2^n) | 20 >> 2 = 5 (20 ÷ 4) |
| Logical Operations (usually used in decision branches) | ||
| == (also =) |
Equal to operation | Value1 = 26, Value2 = 26 If Value1 == Value2 then True If Value1 = Value2 then True |
| != (also <>) |
Not equal to operation | Value1 = 10, Value2 = 20 If Value1 != Value2 then True If Value1 <> Value2 then True |
| && | Logical AND operation | Value = 10 If (Value > 2) && (Value < 15) then True |
| || | Logical OR operation | Value = 20 If (Value < 5) || (Value > 15) then True |
| ! | Logical NOT operation | Flag = 1 If ! Flag then False (inverts the value) |
| Array Functions | ||
| arraydims( array ) | Get the number of dimensions in an array | arraydims(data[5][3]) = 2 |
| arraysize( array ) | Get the total size of an array | arraysize(data[5][3]) = 15 |
| Hardware Functions | ||
| SetPinNoDDR( PortPin, Output ) | Set output pin quickly without changing data direction state. | SetPinNoDDR(PORTA.0, 1) = pin high |
| Input = GetPinNoDDR( PortPin ) | Read input pin quickly without changing data direction state. | GetPinNoDDR(PORTB.1) = 0 or 1 |
| Type Conversion | ||
| float2int( x ) | Convert floating point number to integer | float2int(5.7) = 5 (truncates) |
| int2float( x ) | Convert integer to floating point number | int2float(42) = 42.0 |
| Type Casting Operators (For more information see Typecasting) | ||
| STRING | Convert numeric value to string type | STRING 123 = "123" |
| FLOAT | Convert value to floating point type | FLOAT 5 = 5.0 |
| SIGNED | Convert value to signed integer type | SIGNED -5 = -5 (as signed) |
| UNSIGNED | Convert value to unsigned integer type | UNSIGNED -5 = 65531 (as unsigned) |