PawScript Function Approximations
While PawScript is very powerful, it does not contain a full suite of math functions. This page provides both expression and script based approximations for many mathematical functions, from practical to absurdly niche. All entries include working drop-in replacements with both forms.
Logarithmic Functions[edit]
Natural Logarithm[edit]
Expression[edit]
For values between 0.5 and 2:
<<if($x <= 0, "NaN", 2 * (($z := ($x-1)/($x+1)) + $z^3/3 + $z^5/5 + $z^7/7))>>
For values between 0.125 and 8:
<<if($x <= 0, "NaN", if $x >= 8, 2.07944154168 + 2 * (($z := ($x/8-1)/($x/8+1)) + $z^3/3 + $z^5/5 + $z^7/7), if $x >= 4, 1.38629436112 + 2 * (($z := ($x/4-1)/($x/4+1)) + $z^3/3 + $z^5/5 + $z^7/7), if $x >= 2, 0.69314718056 + 2 * (($z := ($x/2-1)/($x/2+1)) + $z^3/3 + $z^5/5 + $z^7/7), if $x < 0.5, if $x <= 0.125, -2.07944154168 + 2 * (($z := (8/$x-1)/(8/$x+1)) + $z^3/3 + $z^5/5 + $z^7/7), if $x <= 0.25, -1.38629436112 + 2 * (($z := (4/$x-1)/(4/$x+1)) + $z^3/3 + $z^5/5 + $z^7/7), -0.69314718056 + 2 * (($z := (2/$x-1)/(2/$x+1)) + $z^3/3 + $z^5/5 + $z^7/7), 2 * (($z := ($x-1)/($x+1)) + $z^3/3 + $z^5/5 + $z^7/7) )>>
For larger values (works up to 16,384):
<<if($x <= 0, "NaN", if $x >= 8192, 9.01091334728 + 2*(($x/8192-1)/($x/8192+1) + (($x/8192-1)/($x/8192+1))^3/3 + (($x/8192-1)/($x/8192+1))^5/5 + (($x/8192-1)/($x/8192+1))^7/7), if $x >= 4096, 8.31776616672 + 2*(($x/4096-1)/($x/4096+1) + (($x/4096-1)/($x/4096+1))^3/3 + (($x/4096-1)/($x/4096+1))^5/5 + (($x/4096-1)/($x/4096+1))^7/7), if $x >= 2048, 7.62461898616 + 2*(($x/2048-1)/($x/2048+1) + (($x/2048-1)/($x/2048+1))^3/3 + (($x/2048-1)/($x/2048+1))^5/5 + (($x/2048-1)/($x/2048+1))^7/7), if $x >= 1024, 6.9314718056 + 2*(($x/1024-1)/($x/1024+1) + (($x/1024-1)/($x/1024+1))^3/3 + (($x/1024-1)/($x/1024+1))^5/5 + (($x/1024-1)/($x/1024+1))^7/7), if $x >= 512, 6.23832462504 + 2*(($x/512-1)/($x/512+1) + (($x/512-1)/($x/512+1))^3/3 + (($x/512-1)/($x/512+1))^5/5 + (($x/512-1)/($x/512+1))^7/7), if $x >= 256, 5.54517744448 + 2*(($x/256-1)/($x/256+1) + (($x/256-1)/($x/256+1))^3/3 + (($x/256-1)/($x/256+1))^5/5 + (($x/256-1)/($x/256+1))^7/7), if $x >= 128, 4.85203026392 + 2*(($x/128-1)/($x/128+1) + (($x/128-1)/($x/128+1))^3/3 + (($x/128-1)/($x/128+1))^5/5 + (($x/128-1)/($x/128+1))^7/7), if $x >= 64, 4.15888308336 + 2*(($x/64-1)/($x/64+1) + (($x/64-1)/($x/64+1))^3/3 + (($x/64-1)/($x/64+1))^5/5 + (($x/64-1)/($x/64+1))^7/7), if $x >= 32, 3.4657359028 + 2*(($x/32-1)/($x/32+1) + (($x/32-1)/($x/32+1))^3/3 + (($x/32-1)/($x/32+1))^5/5 + (($x/32-1)/($x/32+1))^7/7), if $x >= 16, 2.77258872224 + 2*(($x/16-1)/($x/16+1) + (($x/16-1)/($x/16+1))^3/3 + (($x/16-1)/($x/16+1))^5/5 + (($x/16-1)/($x/16+1))^7/7), if $x >= 8, 2.07944154168 + 2*(($x/8-1)/($x/8+1) + (($x/8-1)/($x/8+1))^3/3 + (($x/8-1)/($x/8+1))^5/5 + (($x/8-1)/($x/8+1))^7/7), if $x >= 4, 1.38629436112 + 2*(($x/4-1)/($x/4+1) + (($x/4-1)/($x/4+1))^3/3 + (($x/4-1)/($x/4+1))^5/5 + (($x/4-1)/($x/4+1))^7/7), if $x >= 2, 0.69314718056 + 2*(($x/2-1)/($x/2+1) + (($x/2-1)/($x/2+1))^3/3 + (($x/2-1)/($x/2+1))^5/5 + (($x/2-1)/($x/2+1))^7/7), if $x >= 1, 2*(($x-1)/($x+1) + (($x-1)/($x+1))^3/3 + (($x-1)/($x+1))^5/5 + (($x-1)/($x+1))^7/7), if $x >= 0.5, -0.69314718056 + 2*((2*$x-1)/(2*$x+1) + ((2*$x-1)/(2*$x+1))^3/3 + ((2*$x-1)/(2*$x+1))^5/5 + ((2*$x-1)/(2*$x+1))^7/7), if $x >= 0.25, -1.38629436112 + 2*((4*$x-1)/(4*$x+1) + ((4*$x-1)/(4*$x+1))^3/3 + ((4*$x-1)/(4*$x+1))^5/5 + ((4*$x-1)/(4*$x+1))^7/7), if $x >= 0.125, -2.07944154168 + 2*((8*$x-1)/(8*$x+1) + ((8*$x-1)/(8*$x+1))^3/3 + ((8*$x-1)/(8*$x+1))^5/5 + ((8*$x-1)/(8*$x+1))^7/7), -2.77258872224 + 2*((16*$x-1)/(16*$x+1) + ((16*$x-1)/(16*$x+1))^3/3 + ((16*$x-1)/(16*$x+1))^5/5 + ((16*$x-1)/(16*$x+1))^7/7) )>>
Script[edit]
# Natural Logarithm - Artanh Series with Range Reduction
set $ln2 = 0.6931471805599453
set $y = $x
set $n = 0
# Reduce y to [0.5, 2) using powers of 2
for each $i in range(40)
if $y >= 2
set $y = $y / 2
set $n = $n + 1
else if $y < 0.5
set $y = $y * 2
set $n = $n - 1
# Compute artanh((y-1)/(y+1)) with 10 terms
set $z = ($y - 1) / ($y + 1)
set $z2 = $z * $z
set $sum = $z
set $current = $z
for each $k in range(1, 10)
set $current = $current * $z2
set $sum = $sum + $current / (2 * $k + 1)
set $result = $n * $ln2 + 2 * $sum
Formula[edit]
This approximation is provided by the Artanh Series Approximation (also known as the Inverse Hyperbolic Tangent Series for Natural Logarithm)
Log Base N[edit]
Expression[edit]
For values between .5 and 2:
<<if($b <= 0 or $b = 1 or $x <= 0, "NaN", (2 * (($x-1)/($x+1) + (($x-1)/($x+1))^3/3 + (($x-1)/($x+1))^5/5 + (($x-1)/($x+1))^7/7)) / (2 * (($b-1)/($b+1) + (($b-1)/($b+1))^3/3 + (($b-1)/($b+1))^5/5 + (($b-1)/($b+1))^7/7)) )>>
For values between 0.125 and 16:
<<if($b <= 0 or $b = 1 or $x <= 0, "NaN", (if $x >= 8, 2.07944154168 + 2*(($x/8-1)/($x/8+1) + (($x/8-1)/($x/8+1))^3/3 + (($x/8-1)/($x/8+1))^5/5 + (($x/8-1)/($x/8+1))^7/7), if $x >= 4, 1.38629436112 + 2*(($x/4-1)/($x/4+1) + (($x/4-1)/($x/4+1))^3/3 + (($x/4-1)/($x/4+1))^5/5 + (($x/4-1)/($x/4+1))^7/7), if $x >= 2, 0.69314718056 + 2*(($x/2-1)/($x/2+1) + (($x/2-1)/($x/2+1))^3/3 + (($x/2-1)/($x/2+1))^5/5 + (($x/2-1)/($x/2+1))^7/7), if $x < 0.5, -0.69314718056 + 2*((2*$x-1)/(2*$x+1) + ((2*$x-1)/(2*$x+1))^3/3 + ((2*$x-1)/(2*$x+1))^5/5 + ((2*$x-1)/(2*$x+1))^7/7), if $x < 0.25, -1.38629436112 + 2*((4*$x-1)/(4*$x+1) + ((4*$x-1)/(4*$x+1))^3/3 + ((4*$x-1)/(4*$x+1))^5/5 + ((4*$x-1)/(4*$x+1))^7/7), 2*(($x-1)/($x+1) + (($x-1)/($x+1))^3/3 + (($x-1)/($x+1))^5/5 + (($x-1)/($x+1))^7/7)) / (if $b >= 8, 2.07944154168 + 2*(($b/8-1)/($b/8+1) + (($b/8-1)/($b/8+1))^3/3 + (($b/8-1)/($b/8+1))^5/5 + (($b/8-1)/($b/8+1))^7/7), if $b >= 4, 1.38629436112 + 2*(($b/4-1)/($b/4+1) + (($b/4-1)/($b/4+1))^3/3 + (($b/4-1)/($b/4+1))^5/5 + (($b/4-1)/($b/4+1))^7/7), if $b >= 2, 0.69314718056 + 2*(($b/2-1)/($b/2+1) + (($b/2-1)/($b/2+1))^3/3 + (($b/2-1)/($b/2+1))^5/5 + (($b/2-1)/($b/2+1))^7/7), if $b < 0.5, -0.69314718056 + 2*((2*$b-1)/(2*$b+1) + ((2*$b-1)/(2*$b+1))^3/3 + ((2*$b-1)/(2*$b+1))^5/5 + ((2*$b-1)/(2*$b+1))^7/7), if $b < 0.25, -1.38629436112 + 2*((4*$b-1)/(4*$b+1) + ((4*$b-1)/(4*$b+1))^3/3 + ((4*$b-1)/(4*$b+1))^5/5 + ((4*$b-1)/(4*$b+1))^7/7), 2*(($b-1)/($b+1) + (($b-1)/($b+1))^3/3 + (($b-1)/($b+1))^5/5 + (($b-1)/($b+1))^7/7)) )>>
Script[edit]
# Logarithm of any base: log_b(x) -> stores result in $result
# Approximation: Artanh Series with Range Reduction
# Input validation - set result to NaN if invalid
if $b <= 0 or $b = 1 or $x <= 0
set $result = "NaN"
else
set $ln2 = 0.6931471805599453
# --- Compute ln(x) ---
set $y = $x
set $n = 0
for each $i in range(40)
if $y >= 2
set $y = $y / 2
set $n = $n + 1
else if $y < 0.5
set $y = $y * 2
set $n = $n - 1
set $z = ($y - 1) / ($y + 1)
set $z2 = $z * $z
set $sum_x = $z
set $current = $z
for each $k in range(1, 10)
set $current = $current * $z2
set $sum_x = $sum_x + $current / (2 * $k + 1)
set $ln_x = $n * $ln2 + 2 * $sum_x
# --- Compute ln(b) ---
set $y = $b
set $n = 0
for each $i in range(40)
if $y >= 2
set $y = $y / 2
set $n = $n + 1
else if $y < 0.5
set $y = $y * 2
set $n = $n - 1
set $z = ($y - 1) / ($y + 1)
set $z2 = $z * $z
set $sum_b = $z
set $current = $z
for each $k in range(1, 10)
set $current = $current * $z2
set $sum_b = $sum_b + $current / (2 * $k + 1)
set $ln_b = $n * $ln2 + 2 * $sum_b
# --- Final result ---
set $result = $ln_x / $ln_b
Formula[edit]
This approximation is provided by the Artanh Series Approximation (also known as the Inverse Hyperbolic Tangent Series for Natural Logarithm)
Nth Root[edit]
Computes the nth root of x, handling negative numbers for odd roots.
Expression[edit]
<<if($x < 0 and $n % 2 = 0, "NaN",
if($n = 0, "NaN",
if($x = 0, 0,
power(abs($x), 1/$n) * if($x < 0 and $n % 2 = 1, -1, 1)
)
)
)>>
Script[edit]
# nth_root(x, n) -> $result
if $x < 0 and $n % 2 = 0
set $result = "NaN"
else if $n = 0
set $result = "NaN"
else if $x = 0
set $result = 0
else
set $result = power(abs($x), 1/$n)
if $x < 0 and $n % 2 = 1
set $result = -$result
Formula[edit]
x^(1/n) with sign preservation for odd n
Hyperbolic Functions[edit]
Hyperbolic Sine (sinh)[edit]
Expression[edit]
<<(power(2.718281828459045, $x) - power(2.718281828459045, -$x)) / 2>>
Script[edit]
# sinh(x) -> $result set $e = 2.718281828459045 set $result = (power($e, $x) - power($e, -$x)) / 2
Formula[edit]
(e^x - e^-x)/2
Hyperbolic Cosine (cosh)[edit]
Expression[edit]
<<(power(2.718281828459045, $x) + power(2.718281828459045, -$x)) / 2>>
Script[edit]
# cosh(x) -> $result set $e = 2.718281828459045 set $result = (power($e, $x) + power($e, -$x)) / 2
Formula[edit]
(e^x + e^-x)/2
Hyperbolic Tangent (tanh)[edit]
Expression[edit]
<<if($x = 0, 0, (power(2.718281828459045, 2*$x) - 1) / (power(2.718281828459045, 2*$x) + 1) )>>
Script[edit]
# tanh(x) -> $result set $e = 2.718281828459045 set $ex2 = power($e, 2*$x) set $result = ($ex2 - 1) / ($ex2 + 1)
Formula[edit]
(e^(2x) - 1)/(e^(2x) + 1)
Inverse Hyperbolic Sine (asinh)[edit]
Expression[edit]
<<if($x = 0, 0, ln_expression($x + power($x^2 + 1, 0.5)) )>>
- (Requires the ln_expression from the Natural Logarithm section above)*
Script[edit]
# asinh(x) -> $result
if $x = 0
set $result = 0
else
# Compute ln(x + sqrt(x^2 + 1)) using artanh series
set $temp = $x + power($x*$x + 1, 0.5)
set $ln2 = 0.6931471805599453
set $y = $temp
set $n = 0
for each $i in range(40)
if $y >= 2
set $y = $y / 2
set $n = $n + 1
else if $y < 0.5
set $y = $y * 2
set $n = $n - 1
set $z = ($y - 1) / ($y + 1)
set $z2 = $z * $z
set $sum = $z
set $current = $z
for each $k in range(1, 10)
set $current = $current * $z2
set $sum = $sum + $current / (2 * $k + 1)
set $result = $n * $ln2 + 2 * $sum
Formula[edit]
ln(x + √(x² + 1))
Inverse Hyperbolic Cosine (acosh)[edit]
Expression[edit]
<<if($x < 1, "NaN", ln_expression($x + power($x^2 - 1, 0.5)) )>>
Script[edit]
# acosh(x) -> $result
if $x < 1
set $result = "NaN"
else
set $temp = $x + power($x*$x - 1, 0.5)
set $ln2 = 0.6931471805599453
set $y = $temp
set $n = 0
for each $i in range(40)
if $y >= 2
set $y = $y / 2
set $n = $n + 1
else if $y < 0.5
set $y = $y * 2
set $n = $n - 1
set $z = ($y - 1) / ($y + 1)
set $z2 = $z * $z
set $sum = $z
set $current = $z
for each $k in range(1, 10)
set $current = $current * $z2
set $sum = $sum + $current / (2 * $k + 1)
set $result = $n * $ln2 + 2 * $sum
Formula[edit]
ln(x + √(x² - 1)) for x ≥ 1
Inverse Hyperbolic Tangent (atanh)[edit]
Expression[edit]
<<if(abs($x) >= 1, "NaN", ln_expression((1 + $x) / (1 - $x)) / 2 )>>
Script[edit]
# atanh(x) -> $result
if abs($x) >= 1
set $result = "NaN"
else
set $temp = (1 + $x) / (1 - $x)
set $ln2 = 0.6931471805599453
set $y = $temp
set $n = 0
for each $i in range(40)
if $y >= 2
set $y = $y / 2
set $n = $n + 1
else if $y < 0.5
set $y = $y * 2
set $n = $n - 1
set $z = ($y - 1) / ($y + 1)
set $z2 = $z * $z
set $sum = $z
set $current = $z
for each $k in range(1, 10)
set $current = $current * $z2
set $sum = $sum + $current / (2 * $k + 1)
set $result = ($n * $ln2 + 2 * $sum) / 2
Formula[edit]
0.5 * ln((1+x)/(1-x)) for |x| < 1
Statistical Functions[edit]
Median[edit]
Finds the middle value of a list.
Expression[edit]
<<if($list.count() = 0, "NaN", if($list.count() % 2 = 1, $list.sort().item(($list.count()+1)/2), ($list.sort().item($list.count()/2) + $list.sort().item($list.count()/2 + 1)) / 2 ) )>>
Script[edit]
# median(list) -> $result set $sorted = $list.sort() set $n = $sorted.count() if $n = 0 set $result = "NaN" else if $n % 2 = 1 set $result = $sorted.item(($n + 1) / 2) else set $result = ($sorted.item($n / 2) + $sorted.item($n / 2 + 1)) / 2
Formula[edit]
Middle value(s) of sorted list
Variance (Population)[edit]
Expression[edit]
<<if($list.count() = 0, "NaN", $list.sum(power($item - $list.average(), 2)) / $list.count() )>>
- (Note: The $item variable is handled by the sum function in PawScript)*
Script[edit]
# variance(list) -> $result
set $n = $list.count()
if $n = 0
set $result = "NaN"
else
set $mean = $list.average()
set $sum_sq = 0
for each $item in $list
set $sum_sq = $sum_sq + power($item - $mean, 2)
set $result = $sum_sq / $n
Formula[edit]
Σ(xᵢ - μ)² / N
Standard Deviation (Population)[edit]
Expression[edit]
<<power(variance_expression($list), 0.5)>>
- (Uses variance_expression above)*
Script[edit]
# std_dev(list) -> $result
set $n = $list.count()
if $n = 0
set $result = "NaN"
else
set $mean = $list.average()
set $sum_sq = 0
for each $item in $list
set $sum_sq = $sum_sq + power($item - $mean, 2)
set $result = power($sum_sq / $n, 0.5)
Formula[edit]
√(variance)
Combinatorics[edit]
Factorial[edit]
Expression[edit]
<<if($x < -1, "NaN",
if $x = round($x) and $x >= 0,
choose($x, 0, 1, 1, 1, 2, 2, 3, 6, 4, 24, 5, 120, 6, 720, 7, 5040, 8, 40320, 9, 362880, 10, 3628800, 11, 39916800, 12, 479001600, 13, 6227020800, 14, 87178291200, 15, 1307674368000, 16, 20922789888000, 17, 355687428096000, "NaN"),
power($x + 8.5, $x + 8.5) *
power(2.718281828459045, -($x + 8.5)) *
power(6.283185307179586, 0.5) *
(0.99999999999980993 +
676.5203681218851/($x+2) -
1259.1392167224028/($x+3) +
771.32342877765313/($x+4) -
176.61502916214059/($x+5) +
12.507343278686905/($x+6) -
0.13857109526572012/($x+7) +
9.9843695780195716e-6/($x+8) +
1.5056327351493116e-7/($x+9))
)>>
Script[edit]
# factorial(x) -> $result (works for any x > -1, except negative integers)
set $e = 2.718281828459045
set $pi = 3.141592653589793
if $x < -1
set $result = "NaN"
else if $x = round($x) and $x >= 0
# Direct computation for non-negative integers
set $result = 1
for each $i in range(1, $x + 1)
set $result = $result * $i
else
# Spouge's approximation for non-integers
set $z = $x + 1
set $sum = 0.99999999999980993
set $sum = $sum + 676.5203681218851 / ($z + 1)
set $sum = $sum - 1259.1392167224028 / ($z + 2)
set $sum = $sum + 771.32342877765313 / ($z + 3)
set $sum = $sum - 176.61502916214059 / ($z + 4)
set $sum = $sum + 12.507343278686905 / ($z + 5)
set $sum = $sum - 0.13857109526572012 / ($z + 6)
set $sum = $sum + 9.9843695780195716e-6 / ($z + 7)
set $sum = $sum + 1.5056327351493116e-7 / ($z + 8)
set $result = power($z + 7.5, $z + 0.5) * power($e, -($z + 7.5)) * power(2 * $pi, 0.5) * $sum
Formula[edit]
Directly computed for integers but using Spouge's Approximation for noninteger values.
Binomial Coefficient (n choose k)[edit]
Expression[edit]
<<if($k < 0 or $k > $n, 0, if($k = 0 or $k = $n, 1, factorial_expression($n) / (factorial_expression($k) * factorial_expression($n - $k)) ) )>>
- (Uses factorial_expression from the Factorial section above)*
Script[edit]
# binomial(n, k) -> $result
if $k < 0 or $k > $n
set $result = 0
else if $k = 0 or $k = $n
set $result = 1
else
# Compute factorial(n)
set $fact_n = 1
for each $i in range(1, $n + 1)
set $fact_n = $fact_n * $i
# Compute factorial(k)
set $fact_k = 1
for each $i in range(1, $k + 1)
set $fact_k = $fact_k * $i
# Compute factorial(n-k)
set $fact_nk = 1
for each $i in range(1, $n - $k + 1)
set $fact_nk = $fact_nk * $i
set $result = $fact_n / ($fact_k * $fact_nk)
Formula[edit]
n! / (k! * (n-k)!)
Permutations (nPr)[edit]
Expression[edit]
<<if($k < 0 or $k > $n, 0, factorial_expression($n) / factorial_expression($n - $k) )>>
Script[edit]
# permute(n, k) -> $result
if $k < 0 or $k > $n
set $result = 0
else
set $fact_n = 1
for each $i in range(1, $n + 1)
set $fact_n = $fact_n * $i
set $fact_nk = 1
for each $i in range(1, $n - $k + 1)
set $fact_nk = $fact_nk * $i
set $result = $fact_n / $fact_nk
Formula[edit]
n! / (n-k)!
Subfactorial (Derangement) 🤪[edit]
Number of permutations where no element appears in its original position.
Expression[edit]
<<if($n = 0, 1, if($n = 1, 0, round(factorial_expression($n) / 2.718281828459045) ) )>>
Script[edit]
# derangement(n) -> $result
if $n = 0
set $result = 1
else if $n = 1
set $result = 0
else
set $fact = 1
for each $i in range(1, $n + 1)
set $fact = $fact * $i
set $e = 2.718281828459045
set $result = round($fact / $e)
Formula[edit]
!n ≈ n!/e (approximation for n ≥ 2)
Superfactorial[edit]
Product of factorials: sf(n) = 1! × 2! × 3! × ... × n!
Expression[edit]
<<if($n < 0, "NaN", if($n = 0, 1, power(1, $n) * power(2, $n-1) * power(3, $n-2) * power(4, $n-3) * power(5, $n-4) ) )>>
- (Exact for n ≤ 5, approximation for n > 5)*
Script[edit]
# superfactorial(n) -> $result
if $n < 0
set $result = "NaN"
else if $n = 0
set $result = 1
else
set $result = 1
set $k = 1
while $k <= $n
set $fact_k = 1
set $i = 1
while $i <= $k
set $fact_k = $fact_k * $i
set $i = $i + 1
set $result = $result * $fact_k
set $k = $k + 1
Formula[edit]
∏ₖ=1ⁿ k!
Double Factorial[edit]
n!! = n × (n-2) × (n-4) × ... × 1 or 2
Expression[edit]
<<if($n < -1, "NaN",
if($n = -1 or $n = 0, 1,
if($n % 2 = 0,
power(2, $n/2) * factorial_expression($n/2),
power(2, ($n-1)/2) * factorial_expression(($n+1)/2)
)
)
)>>
Script[edit]
# double_factorial(n) -> $result
if $n < -1
set $result = "NaN"
else if $n = -1 or $n = 0
set $result = 1
else
set $result = 1
set $k = $n
while $k > 0
set $result = $result * $k
set $k = $k - 2
Formula[edit]
n × (n-2) × ... × 1 or 2
Calculus[edit]
Numerical Derivative[edit]
Approximates the first derivative of a function at a point using the central difference method.
Expression[edit]
<<(FUNCTION($x + 0.0001) - FUNCTION($x - 0.0001)) / 0.0002>>
Script[edit]
# derivative(f, x) -> $result set $h = 0.0001 set $f_x_plus_h = FUNCTION($x + $h) set $f_x_minus_h = FUNCTION($x - $h) set $result = ($f_x_plus_h - $f_x_minus_h) / (2 * $h)
Formula[edit]
Numerical differentiation: f'(x) ≈ (f(x + h) - f(x - h)) / (2h) where h is a small number (e.g., 1e-4).
Second Derivative[edit]
Approximates the second derivative of a function at a point.
Expression[edit]
<<(FUNCTION($x + 0.0001) - 2 * FUNCTION($x) + FUNCTION($x - 0.0001)) / (0.0001^2)>>
Script[edit]
# second_derivative(f, x) -> $result set $h = 0.0001 set $f_x_plus_h = FUNCTION($x + $h) set $f_x = FUNCTION($x) set $f_x_minus_h = FUNCTION($x - $h) set $result = ($f_x_plus_h - 2 * $f_x + $f_x_minus_h) / ($h * $h)
Formula[edit]
Numerical second derivative: f(x) ≈ (f(x + h) - 2f(x) + f(x - h)) / h²
Partial Derivatives (2D)[edit]
Approximates the partial derivative of a function of two variables, f(x, y), with respect to x or y.
Expression (∂f/∂x)[edit]
<<(FUNCTION($x + 0.0001, $y) - FUNCTION($x - 0.0001, $y)) / 0.0002>>
Script (∂f/∂x and ∂f/∂y)[edit]
# partial_derivative_x(f, x, y) -> $result set $h = 0.0001 set $f_x_plus_h = FUNCTION($x + $h, $y) set $f_x_minus_h = FUNCTION($x - $h, $y) set $result = ($f_x_plus_h - $f_x_minus_h) / (2 * $h) # partial_derivative_y(f, x, y) -> $result set $h = 0.0001 set $f_y_plus_h = FUNCTION($x, $y + $h) set $f_y_minus_h = FUNCTION($x, $y - $h) set $result = ($f_y_plus_h - $f_y_minus_h) / (2 * $h)
Formula[edit]
Numerical partial derivatives: ∂f/∂x ≈ (f(x + h, y) - f(x - h, y)) / (2h) ∂f/∂y ≈ (f(x, y + h) - f(x, y - h)) / (2h)
Gradient (2D)[edit]
Approximates the gradient vector of a function of two variables, f(x, y).
Expression[edit]
[ (FUNCTION($x + 0.0001, $y) - FUNCTION($x - 0.0001, $y)) / 0.0002, (FUNCTION($x, $y + 0.0001) - FUNCTION($x, $y - 0.0001)) / 0.0002 ]
Script[edit]
# gradient(f, x, y) -> $result (as a list) set $h = 0.0001 set $df_dx = (FUNCTION($x + $h, $y) - FUNCTION($x - $h, $y)) / (2 * $h) set $df_dy = (FUNCTION($x, $y + $h) - FUNCTION($x, $y - $h)) / (2 * $h) set $result = [$df_dx, $df_dy]
Formula[edit]
Numerical gradient: ∇f(x, y) ≈ [∂f/∂x, ∂f/∂y] using central differences for each component.
Directional Derivative[edit]
Approximates the directional derivative of f(x, y) in the direction of a unit vector [u₁, u₂].
Expression[edit]
<< (FUNCTION($x + 0.0001 * $u1, $y + 0.0001 * $u2) - FUNCTION($x, $y)) / 0.0001 >>
Script[edit]
# directional_derivative(f, x, y, u1, u2) -> $result set $h = 0.0001 set $f_x_plus_h = FUNCTION($x + $h * $u1, $y + $h * $u2) set $f_x = FUNCTION($x, $y) set $result = ($f_x_plus_h - $f_x) / $h
Formula[edit]
Numerical directional derivative: Dₐf(x, y) ≈ (f(x + h·a, y + h·a) - f(x, y)) / h where a = [u₁, u₂] is a unit vector.
Jacobian (2D → 2D)[edit]
Approximates the Jacobian matrix of a vector-valued function F(x, y) = [f(x, y), g(x, y)].
Script[edit]
# jacobian(f, g, x, y) -> $result (as a 2x2 list) set $h = 0.0001 # ∂f/∂x set $df_dx = (F1($x + $h, $y) - F1($x - $h, $y)) / (2 * $h) # ∂f/∂y set $df_dy = (F1($x, $y + $h) - F1($x, $y - $h)) / (2 * $h) # ∂g/∂x set $dg_dx = (F2($x + $h, $y) - F2($x - $h, $y)) / (2 * $h) # ∂g/∂y set $dg_dy = (F2($x, $y + $h) - F2($x, $y - $h)) / (2 * $h) set $result = [[$df_dx, $df_dy], [$dg_dx, $dg_dy]]
Formula[edit]
Numerical Jacobian: J ≈ [[∂f/∂x, ∂f/∂y], [∂g/∂x, ∂g/∂y]] using central differences for each partial derivative.
Laplacian (2D)[edit]
Approximates the Laplacian of a function f(x, y).
Expression[edit]
<< (FUNCTION($x + 0.0001, $y) + FUNCTION($x - 0.0001, $y) + FUNCTION($x, $y + 0.0001) + FUNCTION($x, $y - 0.0001) - 4 * FUNCTION($x, $y)) / (0.0001^2) >>
Script[edit]
# laplacian(f, x, y) -> $result set $h = 0.0001 set $f_x_plus_h = FUNCTION($x + $h, $y) set $f_x_minus_h = FUNCTION($x - $h, $y) set $f_y_plus_h = FUNCTION($x, $y + $h) set $f_y_minus_h = FUNCTION($x, $y - $h) set $f_xy = FUNCTION($x, $y) set $result = ($f_x_plus_h + $f_x_minus_h + $f_y_plus_h + $f_y_minus_h - 4 * $f_xy) / ($h * $h)
Formula[edit]
Numerical Laplacian: ∇²f(x, y) ≈ (f(x + h, y) + f(x - h, y) + f(x, y + h) + f(x, y - h) - 4f(x, y)) / h²
Numerical Limit[edit]
Approximates the limit of a function as x approaches a value.
Expression[edit]
<<if($x = $a, FUNCTION($a), FUNCTION($a + ($x - $a) * 0.000001))>>
For limit as x → a of f(x), using a small delta (1e-6).
Script[edit]
# limit(f, a) -> $result set $delta = 0.000001 set $x_left = $a - $delta set $x_right = $a + $delta set $f_left = FUNCTION($x_left) set $f_right = FUNCTION($x_right) set $result = ($f_left + $f_right) / 2
Formula[edit]
Numerical approximation: limₓ→ₐ f(x) ≈ f(a ± δ) where δ is a very small number (e.g., 1e-6).
Definite Integration[edit]
Approximates the definite integral of a function using Simpson's Rule.
Expression[edit]
Simpson's Rule (single interval):
<<($b - $a) / 6 * (FUNCTION($a) + 4 * FUNCTION(($a+$b)/2) + FUNCTION($b))>>
Script[edit]
# Definite integral of FUNCTION from $a to $b -> $result # Simpson's Rule: n must be even (here n=100) set $n = 100 set $h = ($b - $a) / $n set $sum = FUNCTION($a) + FUNCTION($b) # f(a) + f(b), both have weight 1 for each $i in range(1, $n) set $x = $a + $i * $h set $weight = if($i % 2 = 0, 2, 4) set $sum = $sum + $weight * FUNCTION($x) set $result = $sum * $h / 3
Formula[edit]
This uses Simpson's Rule.
Number Theory[edit]
Greatest Common Divisor (GCD)[edit]
Expression[edit]
<<if($a = 0 or $b = 0, max(abs($a), abs($b)), gcd_expression(abs($b), abs($a) % abs($b)) )>>
- (Recursive - may hit recursion limits for large numbers. Use script for reliability.)*
Script[edit]
# gcd(a, b) -> $result set $a = abs($a) set $b = abs($b) while $b != 0 set $temp = $b set $b = $a % $b set $a = $temp set $result = $a
Formula[edit]
Euclidean algorithm
Least Common Multiple (LCM)[edit]
Expression[edit]
<<if($a = 0 or $b = 0, 0, abs($a * $b) / gcd_expression($a, $b) )>>
Script[edit]
# lcm(a, b) -> $result
set $a = abs($a)
set $b = abs($b)
if $a = 0 or $b = 0
set $result = 0
else
# First compute gcd
set $temp_a = $a
set $temp_b = $b
while $temp_b != 0
set $temp = $temp_b
set $temp_b = $temp_a % $temp_b
set $temp_a = $temp
set $gcd = $temp_a
set $result = $a * $b / $gcd
Formula[edit]
|a×b| / gcd(a,b)
Prime Check[edit]
Expression[edit]
<<if($n <= 1, 0,
if($n <= 3, 1,
if($n % 2 = 0 or $n % 3 = 0, 0,
1
)
)
)>>
- (Note: This is a partial check. For full accuracy, use the script version.)*
Script[edit]
# is_prime(n) -> $result (1 if prime, 0 otherwise)
if $n <= 1
set $result = 0
else if $n <= 3
set $result = 1
else if $n % 2 = 0 or $n % 3 = 0
set $result = 0
else
set $result = 1
set $i = 5
while $i * $i <= $n
if $n % $i = 0 or $n % ($i + 2) = 0
set $result = 0
break
set $i = $i + 6
Formula[edit]
Trial division up to √n
Fibonacci Sequence[edit]
Expression[edit]
<<if($n < 0, "NaN",
if($n = 0, 0,
if($n = 1, 1,
round(power((1+power(5,0.5))/2, $n) / power(5, 0.5))
)
)
)>>
- (Binet's formula approximation)*
Script[edit]
# fibonacci(n) -> $result (0-indexed)
if $n < 0
set $result = "NaN"
else if $n = 0
set $result = 0
else if $n = 1
set $result = 1
else
set $a = 0
set $b = 1
set $i = 2
while $i <= $n
set $temp = $a + $b
set $a = $b
set $b = $temp
set $i = $i + 1
set $result = $b
Formula[edit]
Fₙ = Fₙ₋₁ + Fₙ₋₂
Special Functions[edit]
Gamma Function[edit]
Generalized factorial: Γ(n) = (n-1)!
Expression[edit]
<<if($x <= 0, "NaN",
if($x = 1, 1,
if($x = 2, 1,
if($x = 3, 2,
if($x = 4, 6,
if($x < 4.5,
factorial_expression($x - 1),
power($x + 5.5, $x + 0.5) * power(2.718281828459045, -($x + 5.5)) *
power(6.283185307179586, 0.5) *
(1.000000000190015 +
76.18009172947146/($x+1) -
86.50532032941677/($x+2) +
24.01409824083091/($x+3) -
1.231739572450155/($x+4) +
0.1208650973866179e-2/($x+5) -
0.5395239384953e-5/($x+6))
)
)
)
)
)
)>>
Script[edit]
# gamma(x) -> $result
set $e = 2.718281828459045
set $pi = 3.141592653589793
if $x <= 0
set $result = "NaN"
else if $x = round($x) and $x > 0
# Integer case: (n-1)!
set $result = 1
set $i = 1
while $i < $x
set $result = $result * $i
set $i = $i + 1
else
# Lanczos approximation
set $g = 7
set $p0 = 0.99999999999980993
set $p1 = 676.5203681218851
set $p2 = -1259.1392167224028
set $p3 = 771.32342877765313
set $p4 = -176.61502916214059
set $p5 = 12.507343278686905
set $p6 = -0.13857109526572012
set $p7 = 9.9843695780195716e-6
set $p8 = 1.5056327351493116e-7
if $x < 0.5
set $result = $pi / (sin($pi * $x) * gamma(1 - $x))
else
set $z = $x
set $sum = $p0
set $sum = $sum + $p1 / ($z + 1)
set $sum = $sum + $p2 / ($z + 2)
set $sum = $sum + $p3 / ($z + 3)
set $sum = $sum + $p4 / ($z + 4)
set $sum = $sum + $p5 / ($z + 5)
set $sum = $sum + $p6 / ($z + 6)
set $sum = $sum + $p7 / ($z + 7)
set $sum = $sum + $p8 / ($z + 8)
set $result = power($z + $g + 0.5, $z + 0.5) * power($e, -($z + $g + 0.5)) * power(2 * $pi, 0.5) * $sum
Formula[edit]
Lanczos approximation for non-integer values
Error Function (erf)[edit]
Expression[edit]
<<if(abs($x) > 6, if($x > 0, 1, -1), (1 - (a1*$t + a2*$t^2 + a3*$t^3 + a4*$t^4 + a5*$t^5) * power(2.718281828459045, -$x^2)) * if($x < 0, -1, 1) : $t:=1/(1+0.3275911*abs($x)), $a1:=0.254829592, $a2:=-0.284496736, $a3:=1.421413741, $a4:=-1.453152027, $a5:=1.061405429 )>>
- (Abramowitz & Stegun approximation)*
Script[edit]
# erf(x) -> $result set $sign = if($x < 0, -1, 1) set $x = abs($x) if $x > 6 set $result = $sign * 1 else set $t = 1 / (1 + 0.3275911 * $x) set $t2 = $t * $t set $t3 = $t2 * $t set $t4 = $t3 * $t set $t5 = $t4 * $t set $a1 = 0.254829592 set $a2 = -0.284496736 set $a3 = 1.421413741 set $a4 = -1.453152027 set $a5 = 1.061405429 set $poly = $a1*$t + $a2*$t2 + $a3*$t3 + $a4*$t4 + $a5*$t5 set $exp_term = power(2.718281828459045, -$x*$x) set $result = $sign * (1 - $poly * $exp_term)
Formula[edit]
1 - (a₁t + a₂t² + a₃t³ + a₄t⁴ + a₅t⁵)e^(-x²) where t = 1/(1 + ax)
Sigmoid / Logistic Function[edit]
Expression[edit]
<<1 / (1 + power(2.718281828459045, -$x))>>
Script[edit]
# sigmoid(x) -> $result set $e = 2.718281828459045 set $result = 1 / (1 + power($e, -$x))
Formula[edit]
1 / (1 + e^(-x))
Heaviside Step Function[edit]
Expression[edit]
<<if($x < 0, 0, if($x = 0, 0.5, 1))>>
Script[edit]
# heaviside(x) -> $result if $x < 0 set $result = 0 else if $x = 0 set $result = 0.5 else set $result = 1
Formula[edit]
H(x) = 0 for x < 0, 0.5 for x = 0, 1 for x > 0
Lambert W Function (Approximation) 🤪[edit]
Solves w e^w = x
Expression[edit]
<<if($x < -1/2.718281828459045, "NaN",
if($x = 0, 0,
if($x <= 1.5,
if($x < 0,
ln_expression(-$x) - ln_expression(-ln_expression(-$x)),
$x
)
else
ln_expression($x) - ln_expression(ln_expression($x))
)
)
)>>
- (Initial approximation only - use script for better accuracy)*
Script[edit]
# lambertw(x) -> $result (principal branch)
set $e = 2.718281828459045
if $x < -1/$e
set $result = "NaN"
else if $x = 0
set $result = 0
else if $x <= 1.5
# Initial approximation
if $x < 0
set $w = ln_expression(-$x) - ln_expression(-ln_expression(-$x))
else
set $w = $x
# Newton-Raphson iteration (3 iterations)
set $i = 0
while $i < 3
set $w = $w - ($w * power($e, $w) - $x) / (power($e, $w) * ($w + 1))
set $i = $i + 1
set $result = $w
else
# Initial approximation for x > 1.5
set $w = ln_expression($x)
if $x > 10
set $w = ln_expression($x) - ln_expression(ln_expression($x))
# Newton-Raphson iteration
set $i = 0
while $i < 5
set $w = $w - ($w * power($e, $w) - $x) / (power($e, $w) * ($w + 1))
set $i = $i + 1
set $result = $w
Formula[edit]
Newton-Raphson iteration on w e^w - x = 0
Bessel Function J₀ (First Kind, Order 0) 🤪[edit]
Expression[edit]
<<if(abs($x) > 10, power(2/(3.141592653589793*$x), 0.5) * cos($x - 3.141592653589793/4), 1 - ($x/2)^2 + ($x/2)^4/4 - ($x/2)^6/36 + ($x/2)^8/576 )>>
- (Polynomial approximation for |x| ≤ 10, asymptotic for |x| > 10)*
Script[edit]
# bessel_j0(x) -> $result set $x = abs($x) set $pi = 3.141592653589793 if $x > 10 set $result = power(2/($pi*$x), 0.5) * cos($x - $pi/4) else set $x2 = $x / 2 set $x4 = $x2 * $x2 set $x6 = $x4 * $x2 set $x8 = $x6 * $x2 set $result = 1 - $x4 + $x8/4 - $x6*$x4/36 + $x8*$x4/576
Formula[edit]
Series expansion + asymptotic approximation
Chebyshev Polynomial Tₙ(x) 🤪[edit]
Expression (for n=0 to 5)[edit]
<<choose($n, 0, 1, 1, $x, 2, 2*$x^2 - 1, 3, 4*$x^3 - 3*$x, 4, 8*$x^4 - 8*$x^2 + 1, 5, 16*$x^5 - 20*$x^3 + 5*$x, "Use script for n > 5" )>>
Script[edit]
# chebyshev_t(n, x) -> $result
if $n = 0
set $result = 1
else if $n = 1
set $result = $x
else
set $t0 = 1
set $t1 = $x
set $i = 2
while $i <= $n
set $t2 = 2 * $x * $t1 - $t0
set $t0 = $t1
set $t1 = $t2
set $i = $i + 1
set $result = $t1
Formula[edit]
Tₙ(x) = 2x Tₙ₋₁(x) - Tₙ₋₂(x)
Interpolation & Mapping[edit]
Linear Interpolation (lerp)[edit]
Expression[edit]
<<$a + ($b - $a) * $t>>
Script[edit]
# lerp(a, b, t) -> $result set $result = $a + ($b - $a) * $t
Formula[edit]
a + (b-a)t
Inverse Linear Interpolation[edit]
Expression[edit]
<<if($a = $b, 0, ($x - $a) / ($b - $a))>>
Script[edit]
# inverse_lerp(x, a, b) -> $result if $a = $b set $result = 0 else set $result = ($x - $a) / ($b - $a)
Formula[edit]
(x-a)/(b-a)
Remap[edit]
Maps x from [a,b] to [c,d]
Expression[edit]
<<if($a = $b, ($c + $d) / 2, $c + ($d - $c) * (($x - $a) / ($b - $a)) )>>
Script[edit]
# remap(x, a, b, c, d) -> $result if $a = $b set $result = ($c + $d) / 2 else set $t = ($x - $a) / ($b - $a) set $result = $c + ($d - $c) * $t
Formula[edit]
c + (d-c) × (x-a)/(b-a)
Smoothstep[edit]
Smooth interpolation: 0→0, 0.5→0.5, 1→1 with smooth derivatives
Expression[edit]
<<if($x <= 0, 0, if($x >= 1, 1, $x^2 * (3 - 2*$x) ) )>>
Script[edit]
# smoothstep(x) -> $result if $x <= 0 set $result = 0 else if $x >= 1 set $result = 1 else set $result = $x * $x * (3 - 2 * $x)
Formula[edit]
x²(3-2x)
Normalize Angle[edit]
Normalizes angle in radians to [0, 2π)
Expression[edit]
<<if($x < 0, $x + 6.283185307179586 * (1 + floor(-$x / 6.283185307179586)), $x - 6.283185307179586 * floor($x / 6.283185307179586) )>>
Script[edit]
# normalize_angle(radians) -> $result set $two_pi = 6.283185307179586 set $result = $radians % $two_pi if $result < 0 set $result = $result + $two_pi
Formula[edit]
x mod 2π
Catmull-Rom Spline Interpolation[edit]
Smooth interpolation through control points p0, p1, p2, p3
Expression[edit]
<<if($t < 0, $p1,
if($t > 1, $p2,
(
(-0.5*$p0 + 1.5*$p1 - 1.5*$p2 + 0.5*$p3) * $t^3 +
($p0 - 2.5*$p1 + 2*$p2 - 0.5*$p3) * $t^2 +
(-0.5*$p0 + 0.5*$p2) * $t +
$p1
)
)
)>>
Script[edit]
# catmull_rom(p0, p1, p2, p3, t) -> $result
if $t < 0
set $result = $p1
else if $t > 1
set $result = $p2
else
set $t2 = $t * $t
set $t3 = $t2 * $t
set $result = (-0.5*$p0 + 1.5*$p1 - 1.5*$p2 + 0.5*$p3) * $t3 +
($p0 - 2.5*$p1 + 2*$p2 - 0.5*$p3) * $t2 +
(-0.5*$p0 + 0.5*$p2) * $t +
$p1
Formula[edit]
Cubic Hermite spline with tension=0.5
Weird & Fun Functions 🤪[edit]
Tetration (Iterated Exponentiation)[edit]
ⁿa = a^a^a^...^a (n times)
Expression[edit]
<<if($n < 0, "NaN",
if($n = 0, 1,
if($n = 1, $a,
if($n = 2, power($a, $a),
if($n = 3, power($a, power($a, $a)),
"Use script for n > 3"
)
)
)
)
)>>
Script[edit]
# tetrate(a, n) -> $result
if $n < 0
set $result = "NaN"
else if $n = 0
set $result = 1
else
set $result = $a
set $i = 1
while $i < $n
set $result = power($a, $result)
set $i = $i + 1
Formula[edit]
a↑↑n = a^(a↑↑(n-1))
Ackermann Function[edit]
The most famous recursive function that isn't actually recursive in PawScript.
Expression[edit]
<<if($m < 0 or $n < 0, "NaN",
if($m = 0, $n + 1,
if($m = 1, $n + 2,
if($m = 2, 2*$n + 3,
if($m = 3, power(2, $n+3) - 3,
"Use script for m > 3"
)
)
)
)
)>>
- (Closed-form for m ≤ 3, script required for m > 3)*
Script[edit]
# ackermann(m, n) -> $result
# WARNING: Grows extremely fast. m > 4 will likely crash.
if $m < 0 or $n < 0
set $result = "NaN"
else if $m = 0
set $result = $n + 1
else if $m = 1
set $result = $n + 2
else if $m = 2
set $result = 2 * $n + 3
else if $m = 3
set $result = power(2, $n + 3) - 3
else if $m = 4
set $i = 0
set $result = 1
while $i < $n + 3
set $result = power(2, $result)
set $i = $i + 1
set $result = $result - 3
else
set $result = "m too large"
Formula[edit]
Recursive definition (simplified for computation)
Mandelbrot Set Iteration Count 🤪[edit]
Returns iteration count before escape (max 100)
Script[edit]
# mandelbrot(c_real, c_imag, max_iter) -> $result set $z_real = 0 set $z_imag = 0 set $iter = 0 while $iter < $max_iter and $z_real*$z_real + $z_imag*$z_imag <= 4 set $temp = $z_real*$z_real - $z_imag*$z_imag + $c_real set $z_imag = 2*$z_real*$z_imag + $c_imag set $z_real = $temp set $iter = $iter + 1 set $result = $iter
Formula[edit]
zₙ₊₁ = zₙ² + c