Difference between revisions of "Mathematical Functions"

From Flowcode Help
Jump to navigationJump to search
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<sidebar>Sidebar: Taking Flowcode Further</sidebar>
 
 
:''See [[Calculation Icon Properties]]''
 
:''See [[Calculation Icon Properties]]''
  
Line 9: Line 8:
 
Flowcode includes an additional set of mathematical functions:
 
Flowcode includes an additional set of mathematical functions:
  
{| class="wikitable" style="margin:auto; width:700px;"
+
{| class="wikitable" style="margin:auto; width:900px;"
 
|-
 
|-
! scope="col" width="30%" | Function Prototype
+
! scope="col" width="25%" | Function Prototype
! scope="col" width="70%" | Description
+
! scope="col" width="45%" | Description
 +
! scope="col" width="30%" | Example
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Floating Point Arithmetic
 
|-
 
|-
 
| align="center" | float = fadd(float, float)
 
| align="center" | float = fadd(float, float)
 
| Add two floating point numbers together
 
| Add two floating point numbers together
 +
| fadd(3.14, 2.86) = 6.0
 
|-
 
|-
 
| align="center" | float = fsub(float, float)
 
| align="center" | float = fsub(float, float)
 
| Subtract two floating point numbers
 
| Subtract two floating point numbers
 +
| fsub(10.5, 3.2) = 7.3
 
|-
 
|-
 
| align="center" | float = fmul(float, float)
 
| align="center" | float = fmul(float, float)
 
| Multiply two floating point numbers
 
| Multiply two floating point numbers
 +
| fmul(4.5, 2.0) = 9.0
 
|-
 
|-
 
| align="center" | float = fdiv(float, float)
 
| align="center" | float = fdiv(float, float)
 
| Divide two floating point numbers
 
| Divide two floating point numbers
 +
| fdiv(15.0, 3.0) = 5.0
 
|-
 
|-
 
| align="center" | float = fmod(float, float)
 
| align="center" | float = fmod(float, float)
 
| MOD function for floating point numbers
 
| MOD function for floating point numbers
 +
| fmod(7.5, 2.0) = 1.5
 
|-
 
|-
 
| align="center" | byte = isinf(float)
 
| align="center" | byte = isinf(float)
 
| Checks to see if the floating point number is infinite
 
| Checks to see if the floating point number is infinite
 +
| isinf(1.0/0.0) = 1
 
|-
 
|-
 
| align="center" | byte = isnan(float)
 
| align="center" | byte = isnan(float)
 
| Checks to see if the floating point is not a number
 
| Checks to see if the floating point is not a number
 +
| isnan(0.0/0.0) = 1
 
|-
 
|-
 
| align="center" | byte = float_eq(float, float)
 
| align="center" | byte = float_eq(float, float)
 
| Compares two floating point numbers to see if they are equal
 
| Compares two floating point numbers to see if they are equal
 +
| float_eq(3.14, 3.14) = 1
 
|-
 
|-
 
| align="center" | byte = float_ge(float, float)
 
| align="center" | byte = float_ge(float, float)
 
| Compares two floating point numbers to see if they are greater then or equal&nbsp;&nbsp;&nbsp;
 
| Compares two floating point numbers to see if they are greater then or equal&nbsp;&nbsp;&nbsp;
 +
| float_ge(5.0, 3.0) = 1
 
|-
 
|-
 
| align="center" | byte = float_gt(float, float)
 
| align="center" | byte = float_gt(float, float)
 
| Compares two floating point numbers to see if they are greater then
 
| Compares two floating point numbers to see if they are greater then
 +
| float_gt(5.0, 3.0) = 1
 
|-
 
|-
 
| align="center" | byte = float_le(float, float)
 
| align="center" | byte = float_le(float, float)
 
| Compares two floating point numbers to see if they are less then or equal
 
| Compares two floating point numbers to see if they are less then or equal
 +
| float_le(3.0, 5.0) = 1
 
|-
 
|-
 
| align="center" | byte = float_lt(float, float)
 
| align="center" | byte = float_lt(float, float)
 
| Compares two floating point numbers to see if they are less then
 
| Compares two floating point numbers to see if they are less then
 +
| float_lt(3.0, 5.0) = 1
 
|-
 
|-
 
| align="center" | int = random()
 
| align="center" | int = random()
| Generates a random number -32768 <=> 32767
+
| Generates a random number (0-65535)
 +
| (random() MOD 10) + 1 = 7 (range 1-10)
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Mathematical Functions
 
|-
 
|-
 
| align="center" | fabs( x ), floor( x ), ceil( x )
 
| align="center" | fabs( x ), floor( x ), ceil( x )
 
| Absolute value, floor and ceiling functions
 
| Absolute value, floor and ceiling functions
 +
| fabs(-5.7) = 5.7<br>floor(4.9) = 4<br>ceil(4.1) = 5
 
|-
 
|-
 
| align="center" | fmod( x , y )
 
| align="center" | fmod( x , y )
 
| Floating point modulus (remainder of x divided by y)
 
| Floating point modulus (remainder of x divided by y)
 +
| fmod(25.5, 7.0) = 4.5
 
|-
 
|-
 
| align="center" | sqrt( x ), cbrt( x )
 
| align="center" | sqrt( x ), cbrt( x )
 
| Square and cube roots
 
| Square and cube roots
 +
| sqrt(25) = 5.0<br>cbrt(27) = 3.0
 
|-
 
|-
 
| align="center" | log( x ), log10( x )
 
| align="center" | log( x ), log10( x )
 
| Logarithms (base e and base 10)
 
| Logarithms (base e and base 10)
 +
| log(2.718) ~ 1.0<br>log10(100) = 2.0
 
|-
 
|-
 
| align="center" | exp( x ), pow( x , y )
 
| align="center" | exp( x ), pow( x , y )
 
| Exponential and power functions (x to the power of y)
 
| Exponential and power functions (x to the power of y)
 +
| exp(1) ~ 2.718<br>pow(2, 8) = 256
 
|-
 
|-
 
| align="center" | sin( x ), cos( x ), tan( x )
 
| align="center" | sin( x ), cos( x ), tan( x )
| Trigonometric functions
+
| Trigonometric functions where x is in radians
 +
| sin(1.57) ~ 1.0<br>cos(0) = 1.0<br>tan(0.785) ~ 1.0
 
|-
 
|-
 
| align="center" | asin( x ), acos( x ), atan( x )
 
| align="center" | asin( x ), acos( x ), atan( x )
| Inverse trigonometric functions          
+
| Inverse trigonometric functions where x is in radians         
 +
| asin(0.5) ~ 0.524<br>acos(0.707) ~ 0.785<br>atan(1) ~ 0.785
 
|-
 
|-
 
| align="center" | atan2( y , x )
 
| align="center" | atan2( y , x )
 
| Four-quadrant inverse tangent
 
| Four-quadrant inverse tangent
 +
| atan2(1, 1) = 0.785
 
|-
 
|-
 
| align="center" | sinh( x ), cosh( x ), tanh( x )
 
| align="center" | sinh( x ), cosh( x ), tanh( x )
 
| Hyperbolic functions
 
| Hyperbolic functions
 +
| sinh(1) ~ 1.175<br>cosh(0) = 1.0<br>tanh(1) ~ 0.762
 
|-
 
|-
 
| align="center" | isnan( x ), isinf( x )
 
| align="center" | isnan( x ), isinf( x )
 
| Tests for not-a-number and infinity
 
| Tests for not-a-number and infinity
 +
| isnan(5.0) = 0<br>isinf(5.0) = 0
 
|-
 
|-
 
| align="center" | round( x )
 
| align="center" | round( x )
 
| Decimal rounding (x rounded to the nearest integer)
 
| Decimal rounding (x rounded to the nearest integer)
 +
| round(3.7) = 4
 
|-
 
|-
 
| align="center" | fround( x , y )
 
| align="center" | fround( x , y )
 
| Floating point rounding (x rounded to y decimal places)
 
| Floating point rounding (x rounded to y decimal places)
 +
| fround(3.14159, 2) = 3.14
 
|-
 
|-
 
| align="center" | asinh( x ), acosh( x ), atanh( x )
 
| align="center" | asinh( x ), acosh( x ), atanh( x )
 
| Inverse hyperbolic functions
 
| Inverse hyperbolic functions
 +
| asinh(1) ~ 0.881<br>acosh(1) = 0<br>atanh(0.5) ~ 0.549
 
|-
 
|-
 
| align="center" | fact( x )
 
| align="center" | fact( x )
| Factorial
+
| Factorial (multiply all numbers down to 1)
 +
| fact(5) = 120 (5×4×3×2×1)
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Integer Operations
 +
|-
 +
| align="center" | MOD
 +
| Returns the remainder after division
 +
| 17 MOD 5 = 2 (17÷5 = 3 remainder 2)
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Bitwise Operations
 +
|-
 +
| align="center" | AND
 +
| Bitwise AND operation
 +
| 0b1100 AND 0b1010 = 0b1000
 +
|-
 +
| align="center" | OR
 +
| Bitwise OR operation
 +
| 0b1100 OR 0b1010 = 0b1110
 +
|-
 +
| align="center" | XOR
 +
| Bitwise XOR (exclusive OR) operation
 +
| 0b1100 XOR 0b1010 = 0b0110
 +
|-
 +
| align="center" | NOT
 +
| Bitwise NOT operation
 +
| NOT 0b1100 = 0b0011
 +
|-
 +
| align="center" | <<
 +
| Left shift operation (multiply by 2^n)
 +
| 5 << 2 = 20 (5 × 4)
 +
|-
 +
| align="center" | >>
 +
| Right shift operation (divide by 2^n)
 +
| 20 >> 2 = 5 (20 ÷ 4)
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Logical Operations (usually used in decision branches)
 +
|-
 +
| align="center" | &&
 +
| Logical AND operation
 +
| Value = 10<br>If (Value > 2) && (Value < 15) then True
 +
|-
 +
| align="center" | &#124;&#124;
 +
| Logical OR operation
 +
| Value = 20<br>If (Value < 5) &#124;&#124; (Value > 15) then True
 +
|-
 +
| align="center" | !
 +
| Logical NOT operation
 +
| Flag = 1<br>If ! Flag then False (inverts the value)
 +
|-
 +
| align="center" | != also <>
 +
| Not equal to operation
 +
| Value1 = 10, Value2 = 20<br>If Value1 != Value2 then True<br>If Value1 <> Value2 then True
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Array Functions
 +
|-
 +
| align="center" | arraydims( array )
 +
| Get the number of dimensions in an array
 +
| arraydims(data[5][3]) = 2
 +
|-
 +
| align="center" | arraysize( array )
 +
| Get the total size of an array
 +
| arraysize(data[5][3]) = 15
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Hardware Functions
 +
|-
 +
| align="center" | SetPinNoDDR( PortPin, Output )
 +
| Set output pin quickly without changing data direction state.
 +
| SetPinNoDDR(PORTA.0, 1) = pin high
 +
|-
 +
| align="center" | Input = GetPinNoDDR( PortPin )
 +
| Read input pin quickly without changing data direction state.
 +
| GetPinNoDDR(PORTB.1) = 0 or 1
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Type Conversion
 +
|-
 +
| align="center" | float2int( x )
 +
| Convert floating point number to integer
 +
| float2int(5.7) = 5 (truncates)
 +
|-
 +
| align="center" | int2float( x )
 +
| Convert integer to floating point number
 +
| int2float(42) = 42.0
 +
|-
 +
! colspan="3" style="background-color: #f0f0f0;" | Type Casting Operators (For more information see [[Typecasting]])
 +
|-
 +
| align="center" | STRING
 +
| Convert numeric value to string type
 +
| STRING 123 = "123"
 +
|-
 +
| align="center" | FLOAT
 +
| Convert value to floating point type
 +
| FLOAT 5 = 5.0
 +
|-
 +
| align="center" | SIGNED
 +
| Convert value to signed integer type
 +
| SIGNED -5 = -5 (as signed)
 
|-
 
|-
| align="center" | random( )
+
| align="center" | UNSIGNED
| Random number between 0 and 32767
+
| Convert value to unsigned integer type
 +
| UNSIGNED -5 = 65531 (as unsigned)
 
|}
 
|}

Latest revision as of 07:38, 17 October 2025

See Calculation Icon Properties

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)
&& 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)
!= also <> Not equal to operation Value1 = 10, Value2 = 20
If Value1 != Value2 then True
If Value1 <> Value2 then True
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)