Reverse Polish Notation (math package)

This commit is contained in:
2025-09-19 19:37:43 +10:00
parent 5f65c1b42b
commit 340fb67e98
5 changed files with 180 additions and 47 deletions

View File

@@ -10,11 +10,31 @@ fun -string !ltos -list &inlist
add $ans $store &ans
add $idx 1 &idx
equal $idx $listlen &store
if $store %trueend
if $store %end
add $ans ", " &ans
jump %stdoutloop
@trueend
@end
add $ans "]" &ans
return $ans
endfun
fun -list !listremove -list &input
set &idx 0
getlistsize *input &endidx
subtract $endidx 1 &endidx
setlist *ans
@loop
equal $idx $endidx &store
if $store %return
getlistat *input $idx &store
listappend *ans $store
add $idx 1 &idx
jump %loop
@return
return *ans
endfun

View File

@@ -9,3 +9,7 @@ Please note that you cannot copy a single function from the package, as some fun
### fun -string !ltos -list &inlist
Converts a list to a string, in the format [$item1, $item2, $item3, ...] (no prerequisites).
### fun -list !listremove -list &input
Removes the last item from a list.

View File

@@ -454,3 +454,131 @@ fun -double !ln -double &in
@error
error "Cannot find ln of a non-positive double" "mathError"
endfun
fun -double !rpn -list &rpn
use "lists"
# List format: [3, 2, 4, '+', '-'] returns -3
setlist *stack
getlistsize *rpn &len
set &idx 0
@loop
equal $idx $len &store
if $store %return
# If type is a number, add it to the stack
getlistat *rpn $idx &store
gettype $store &store
equal $store "double" &bool
if $bool %pushstack
equal $store "int" &bool
if $bool %pushstack
getlistat *rpn $idx &store
equal $store '+' &bool
if $bool %add
equal $store '-' &bool
if $bool %subtract
equal $store '*' &bool
if $bool %multiply
equal $store '/' &bool
if $bool %divide
jump %increment
@add
getlistsize *stack &store
subtract $store 2 &store
getlistat *stack $store &list1
add $store 1 &store
getlistat *stack $store &list2
add $list1 $list2 &list1
subtract $store 1 &store
setlistat *stack $store $list1
pusharg *stack
call !packages/lists/lists:listremove &stack
jump %increment
@subtract
getlistsize *stack &store
subtract $store 2 &store
getlistat *stack $store &list1
add $store 1 &store
getlistat *stack $store &list2
subtract $list1 $list2 &list1
subtract $store 1 &store
setlistat *stack $store $list1
pusharg *stack
call !packages/lists/lists:listremove &stack
jump %increment
@multiply
getlistsize *stack &store
subtract $store 2 &store
getlistat *stack $store &list1
add $store 1 &store
getlistat *stack $store &list2
multiply $list1 $list2 &list1
subtract $store 1 &store
setlistat *stack $store $list1
pusharg *stack
call !packages/lists/lists:listremove &stack
jump %increment
@divide
getlistsize *stack &store
subtract $store 2 &store
getlistat *stack $store &list1
add $store 1 &store
getlistat *stack $store &list2
divide $list1 $list2 &list1
subtract $store 1 &store
setlistat *stack $store $list1
pusharg *stack
call !packages/lists/lists:listremove &stack
jump %increment
@pushstack
getlistat *rpn $idx &store
listappend *stack $store
jump %increment
@increment
add $idx 1 &idx
jump %loop
@return
getlistsize *stack &store
greater $store 1 &store
if $store %error
getlistat *stack 0 &store
return $store
@error
error "Stack for !fpn has multiple contents" "rangeError"
endfun

View File

@@ -6,70 +6,52 @@ Please note that you cannot copy a single function from the package, as some fun
## Functions
### fun -double !sin -double &input
# Trigonometry
Gets the sin of input.
fun -double !sin -double &input: Gets the sin of input.
### fun -double !cos -double &input
fun -double !cos -double &input: Gets the cos of input.
Gets the cos of input.
fun -double !tan -double &input: Gets the tan of input.
### fun -double !tan -double &input
fun -double !asin -double &input: Finds the angle *x* in the range [-pi/2, pi/2] such that sin(x) equals the input.
Gets the tan of input.
fun -double !acos -double &input: Finds the angle *x* in the range [0, pi] such that cos(x) equals the input.
### fun -double !asin -double &input
fun -double !atan -double &input: Finds the angle *x* in the range [-pi/2, pi/2] such that tan(x) equals the input.
Finds the angle *x* in the range [-pi/2, pi/2] such that sin(x) equals the input.
fun -double !atan2 -double &y -double &x: Finds the angle in the range (-pi, pi] between the positive x axis and the point (x,y). This is equivalent to atan(y/x) for positive values of x.
### fun -double !acos -double &input
# Exponents and Logarithms
Finds the angle *x* in the range [0, pi] such that cos(x) equals the input.
fun -double !intexp -double &base -int &power: Returns base^power, for positive integer values of power. Returns 1 if power is non-positive (and thus is inaccurate for negative values of power).
### fun -double !atan -double &input
fun -double !exp -double &input: Finds exp($input), where exp(x)=e^x; e is Euler's number = 2.718281828...
Finds the angle *x* in the range [-pi/2, pi/2] such that tan(x) equals the input.
fun -double !ln -double &in: Finds the natural log (log_e) of $input, i.e. finds x such that e^x = $input; e is Euler's number = 2.718281828...
### fun -double !atan2 -double &y -double &x
fun -double !sqrt -double &input: Returns the positive value *x* that satisfies x^2 = input. Throws an error if input is negative.
Finds the angle in the range (-pi, pi] between the positive x axis and the point (x,y). This is equivalent to atan(y/x) for positive values of x.
# Integer Operations
### fun -double !intexp -double &base -int &power
fun -double !mod -double &in -double &modulus: Returns *n* such that *n* is congruent to the input modulo $modulus, where *n* is at least 0 and less than $modulus.
Returns base^power, for positive integer values of power. Returns 1 if power is non-positive (and thus is inaccurate for negative values of power).
fun -double !abs -double &in: Returns the absolute value of the input.
### fun -double !exp -double &input
fun -int !factorial -int &in: Returns the factorial of the input.
Finds exp($input), where exp(x)=e^x, e is Euler's number = 2.718281828...
# Constants
### fun -double !ln -double &in
fun -double !pi: Returns pi to the maximum accuracy that doubles can store.
Finds the natural log (log_e) of $input, i.e. finds x such that e^x = $input, e is Euler's number = 2.718281828...
fun -double !e: Returns e to the maximum accuracy that doubles can store.
### fun -double !sqrt -double &input
# Misc
Returns the positive value *x* that satisfies x^2 = input. Throws an error if input is negative.
fun -double !random -int &seed: Gets a random double in the domain [0,1), using a LCG. A seed is required.
### fun -double !mod -double &in -double &modulus
fun -double !rpn -list &rpn: Calculates a function in reverse polish notation. The list should have a form similar to `[3, 5, '+', 2, '-']`, i.e. only numbers and the `+`, `-`, `*`, `/` characters. **Requires the lists package to use**.
Returns *n* such that *n* is congruent to the input modulo $modulus, where *n* is at least 0 and less than $modulus.
fun -double !itod -int &input: Returns the input as a double.
### fun -double !abs -double &in
Returns the absolute value of *n*.
### fun -double !random -int &seed
Gets a random double in the domain [0,1), using a LCG. A seed is required.
### fun -double !pi
Returns pi to the maximum accuracy that doubles can store.
### fun -double !itod -int &input
Returns the input as a double.
### fun -int !dtoi -double &input
Returns the input as an integer.
fun -int !dtoi -double &input: Returns the input as an integer.

View File

@@ -1 +0,0 @@
extern "file"