Navigation: Processing Concepts >

Post-Fix Math

 

 

 

 

Math Operations
String Operations
Stack Operations

Post-Fix Math, referred to in these documents often as PF Math, and called RPN by many, are used in MakerPlot-J to add math, string and other operations not supported in Standard Math, or provide higher speed alternatives to basic operations.
 

PF Math uses a Stack to store data. Data is placed on the stack, like stacking plates, first in is the last out. As operations and functions are called, MakerPlot-J will pull the needed data off the stack, process them, and place the result on the stack or return the value for use.

Compared to standard math, which requires a lot of processing time to interpret the equation, PF Math operates very quickly having simply to store data, retrieve data, and perform the operation on the data.

PF Math is indicated in code with {::   }

Feel free to use the DEBUG/CLI to test operations such as ? {:: 4 9 + 2 * }

For example, the operation of: 4 + 9 * 2
 ? {:: 4 9 + 2 * }
 The sequence of operations is:
 4 is pushed on the stack
 9 is pushed on the stack
 + operator is processed by:
                Popping 9 off the stack
                Popping 4 off the stack
                Adding 4 and 9 for a value of 13
                Pushing 13 on the stack
 2 is pushed on the stack
 * operator is processed by:
                Popping 13 off the stack
                Popping 2 off the stack
                Multiplying 13 by 2 for a value of 26
                Pushing 26 on the stack
 } closes the operation
                18 is popped off the stack and returned to be used in the string

Writing the equation in another format:
 {:: 2 4 9 + * }
 Addition will use the last 2 value, 4 and 9, store 13, then * will use 13 and 2

PF Math may be mixed with standard math operations:
 ? { 2 * {:: 3.4 int}}  // returns 6

To test operations, used the DEBUG/CLI window. Enter some analog values first, such as:
 10,20,30

Post-Fix Math Operations

Instruction

Purpose

Discussion & Examples

+, - , *, /

Basic math operators

Value1 Value2 operator
 Basic math operation in post-fix math
 ? {:: 10 5 * 2 / }    // Returns 25
 ? {:: [ain0] [ain1] + }  // adds 2 analog input values

%

Modulus

Value1 Value2 %
 Returns the remainder of a value after removing all whole defined values:
 ? {:: 7 2 %}  // Returns 2 ( 7 -2 -2 -2 ) = 1

^

Power

Base power ^
 Raises the base to a power
 ? {:: 2 3 ^ }

rad

To Radians

Degrees rad
 Converts degrees to radians. All trig functions are in degrees.
 ? {::  90 rad}

deg

To Degrees

Radians deg
 Converts radians to degrees. All trig functions are in degrees
 ? {:: 1.5 deg sin }

sin, cos, tan

Trigonometric functions in degrees.

Degrees function
 Returns the trig function value using degrees.
 ? {:: 90 sin }

asin, acos, atan

Inverse or Arc Trigonometric functions in degrees.

Value function
 Returns the value in degrees
 ? {:: 1 atan}

int

Integer value

Value int
 Returns the integer part of a value
 ? {:: 12.345 int }                              // Returns 12

frac

Fractional part

Value frac
 Returns the fractional part of a value without the decimal point
 ? {:: 12.345 frac }                            // Returns 345

fracWPoint

Fractional part with decimal point

Value fracWPoint
 Returns the fractional part of a value without the decimal point
 ? {:: 12.345 fracWPoint }              // Returns .345

map

Maps 2 ranges for scaling

value fromStart fromEnd toStart toEnd map
 Maps a value from ‘from’ range to the ‘to’ range to scale data. The result is NOT constrained, in that the result can exceed the boundaries of the ‘to’ set of values.
 ? {:: 78 32 212 0 100 map }          // convert 78 from Fahrenheit range to Celsius range

map

Maps 2 ranges for scaling

value fromStart fromEnd toStart toEnd mapConstrain
Maps a value from the ‘from’ range to the ‘to’ range to scale data. The result IS constrained, in that the result CANNOT exceed the boundaries of the ‘to’ set of values.
 ? {:: 250 32 212 0 100 mapConstrain }    // returns 100

 This is beneficial when using data to draw graphics converting from a value to a drawing coordinate range without exceeding the desired bounds of your graphics.

dec2Bin

Convert decimal to binary

Decimal Places bin
 Coverts and returns a binary value from decimal with the number of places defined:
 ? {:: 95 8 dec2Bin}  // returns 01011111

bin2Dec

Converts binary 2 decimal

Binary bin2Dec
 Converts a binary value to decimal.
 ? {:: 1111 bin2Dec } // returns 15

dec2Hex

Convert decimal to Hex

Decimal dec2Hex
 Coverts and returns a Hexadecimal value from decimal.
 ? {:: 255 dec2Hex}  // returns FF

Hex2Dec

Converts Hex 2 decimal

Hex hex2Dec
 Converts a hexadecimal value to decimal.
 ? {:: FF Hex2Dec } // returns 255

dec2Oct

Convert decimal to octal

Decimal dec2Oct
 Coverts and returns an octal value from decimal.
 ? {:: 8 dec2Oct}  // returns 10

Oct2Dec

Converts octal 2 decimal

Octal oct2Dec
 Converts an octal value to decimal.
 ? {:: 10 Oct2Dec } // returns 8

 

String Operations

+
 or
 concat

Adds (concatenates) 2 Strings

String1 String2 concat
 Adds 2 strings, special string values may be used:
 [\s] space
 [\n] new line
 [\r] carriage return
 [\t] tab
 ? {:: Hello[\s] world! +}
 ? {:: Hello, there World!,+}
 ? {:: Hello, there World!,concat}

if any portion has spaces, double-colons must be used between parameters.
 ? {:: Hello there:: ,how are you?:: concat}
 Note: In many cases you can simply combine values through the use of string structure:
 ? [ain0] volts
 lblAvg= {:: [ain0] + [ain1] } Vavg

-
 or
 remove

Removes part of a String

String1 String 2 remove
 Removes String2 from String1.
 ? {:: abcdefgh def - } // returns abcgh
 ? {:: abcdefgh def remove} // returns abcgh
 If any string has spaces, double-colons must be used to separate parameters.
 ? {:: hello there ::th:: remove }

csvNumber

Returns the defined number of comma separated values

csvString csvNumber
 Given a CSV string such as 10,20,30,40 csvNumber will return the number of specified comma separated values from the END of the string. If the string contains spaces, a double-colon is required to separate parameters.
 ? {:: 10, 20, 30, 40 :: 2 :: csvNumber}

Certain standard math operations use csv values to perform their operation, such as std for standard deviation:
 ? { std(10, 10, 10, 10, 11)}   // returns 0.447…

Comma-separated values can be automatically 2 ways in MakerPlot-J, from the analog plot for each of the 10 channels of data currently plotted using [plot.csvCh0] for example, and pulling the data from the Stack using stackView.
 ? {std ( [plot.csvCh0] ) }
 ? {std ( [stackView] ) }

cvsNumber can be used to reduce the number returned by specifying how many to return. If the data set holds less than the number specified, the full data set will be returned.
 ? {std ( {:: [plot.csvCh0]::100::csvNumber} ) } // standard deviation of last 100

equals

Compares 2 strings

String1 String2 equals
 Compares 2 strings, return 1 if equal 0 if not. This is a case-sensitive comparison.
 ? {:: [txtValue] power equals }
 If any strings contain spaces, a double-colon must be used to separate parameters:
 ? {:: [txtValue]::power:: equals }

equalsIgnoreCase

Compares 2 strings

String1 String2 equalsIgnoreCase
 Compares 2 strings, return 1 if equal 0 if not. This is a case-insensitive comparison.
 ? {:: Power power equalsIgnoreCase }
 If any strings contain spaces, a double-colon must be used to separate parameters:
 ? {:: [txtValue]::power:: equalsIgnoreCase }

format

Formats a value

Value Formatter format
 Formats a single value as shown below using the formatters:
%.2, number of decimals or places
x,X to hex, o to octal
 %number padding align right
%-number padding align left 
d integer
 f float value formatter format

? {:: 12.345667 %.2f format }      // 12.34"
? {:: 123456 %x format }              // hex - 1e240
? {:: 123456 %X format }              // hex - 1E240
? {:: 123456 %o format }              // octal – 361100
? {:: 123456 %e format }              // 1.234560e+05
? {:: 123456 %,d format }             // 123,456
? {:: 1 %03d format }                     // 001"
? {:: 123456 %,.2f format }          // 123,456.00
? {:: Hello %.2s format }               // He
? {:: 100 |%30s| format }             // |                           100|
? {:: 100 |%-30s| format }           // |100                           |
? {:: 100 |%-30.2f| format }        // |100.00

indexOf

Returns the location in a string

String1 String2 indexOf
 Returns the starting index of a String2 in string1. The first character index is 0. -1 is returned if it is not contained in the string. If string contain spaces, :: must be used to separate parameters. String are not trimmed for leading/training spaces.
 ? {:: Hello World!::World::indexOf }        //Returns 6

replace

Replace part of a string

Sting1 String2 String 3 replace
 Replaces the matching contents of string1 identified by string2 with string3. This is case sensitive. If string contain spaces, :: must be used to separate parameters.
 ? {:: Hello lo ppp! replace}   // returns Helppp!

subString

Returns a portion of a string

String1 Start Length substring
 Returns portion of String1 starting at character number start (0 based), with the number of characters defined by length. If string contain spaces, :: must be used to separate parameters.
 ? {:: Hello 2 3 subString }   // returns llo

toLower

Converts to Lower Case

String toLower
 Converts a string to lower case. If string contain spaces, :: must be used to separate parameters.
 ? {:: Hello toLower}   // Returns hello

toUpper

Converts to Upper Case

String toUpper
 Converts a string to Upper case. If string contain spaces, :: must be used to separate parameters.
 ? {:: Hello toUpper}   // Returns HELLO

trim

Remove leading/trailing spaces

String trim
 Removes leading/trailing spaces from a string. Many functions trim automatically. If string contain spaces, :: must be used to separate parameters.

 

Stack Operations

stackClear

Empties the Stack

!stackClear

stackPop

Pop top element off the stack

? [stackPop]

  stackPeek

  Returns the top of the stack

  without popping it off

  ? [stackPeek]

stackPush

Push a value onto the stack

!stackPush 20

stackSize

Returns size of the stack

[stackSize]

stackView

Returns stack as CSV values

[stackView]

 

 

 

 

 

 

Copyright © 2024 SelmaWare Solutions, LLC