UP | HOME

free42 Simple Math Functions

Author: Mitch Richling
Updated: 2024-02-12 13:22:02

Copyright 2024 Mitch Richling. All rights reserved.

Table of Contents

1. Metadata

The home for this HTML file is: https://richmit.github.io/hp42/sfun.html

A PDF version of this file may be found here: https://richmit.github.io/hp42/sfun.pdf

Files related to this document may be found on github: https://github.com/richmit/hp42

Directory contents:

src - The org-mode file that generated this HTML document
src_42s - Ready to convert source listings for 42s code in this document
docs - This html document and associated PDF
bin - Importable RAW program files

2. Introduction

This org-mode file collects together a handful simple mathematical functions that I find useful.

Menu   Description    
L&P:lnYX lnYX Base Y Logarithm of X    
L&P:▒▒▒▒        
L&P:ln2 ln2 Base 2 logarithm    
L&P:LN   Base 3 logarithm    
L&P:LOG   Base 10 logarithm    
L&P:▒▒▒▒        
L&P:Y↑X   Raise Y to the power of X    
L&P:▒▒▒▒        
L&P:2↑X LBL 58 Raise 2 to the power of X    
L&P:E↑X   Raise e to the power of X    
L&P:10↑X   Raise 10 to the power of X    
L&P:▒▒▒▒        
L&P:Y√X YROOT Yth roots prefering real & pure imaginary answers    
L&P:▒▒▒▒        
L&P:2√X LBL 57 Square root prefering real & pure imaginary answers    
L&P:3√X LBL 56 Cube root prefering real & pure imaginary answers    
L&P:5√X LBL 55 Fifth root prefering real & pure imaginary answers    
L&P:7√X LBL 54 Seventh root prefering real & pure imaginary answers    
CPLX:RPART   Real Part of anumber or matrix    
CPLX:IPART   Imaginary Part of a number or matrix    
CPLX:CONJ   Complex conjugate of a number or matrix    
CPLX:CABS   Magnitude of a number or matrix (built in ABS fails on a complex matrix)    
CPLX:CARG   Complex argument of a number or matrix    
TRIG:SIN        
TRIG:COS        
TRIG:TAN        
TRIG:ASIN        
TRIG:ACOS        
TRIG:ATAN        
TRIG:CSC   1/SIN(X)    
TRIG:SEC   1/COS(X)    
TRIG:COT   1/TAN(X)    
TRIG:ACSC   SIN(1/X)    
TRIG:ASEC   COS(1/X)    
TRIG:ACOT   TAN(1/X)    
TRIG:TAN2        
TRIG:HYPOT        
TRIG:HAV   Haversine = 1/2-cos(X)/2    
TRIG:AHAV   Inverse Haversine = 2*asin(sqrt(X))    
TRIG:▒▒▒▒        
TRIG:▒▒▒▒        
HYP:SINH        
HYP:COSH        
HYP:TANH        
HYP:ASINH        
HYP:ACOSH        
HYP:ATANH        
HYP:CSCH   1/SINH(X)    
HYP:SECH   1/COSH(X)    
HYP:COTH   1/TANH(X)    
HYP:ACSCH   SINH(1/X)    
HYP:ASECH   COSH(1/X)    
HYP:ACOTH   TANH(1/X)    
PERC:%T   Percentage of total Y: TOTAL X: PART X: % of Total
PERC:%CH   percentage of change from Y to X Y: OLD X: New X: % Change
PERC:%   Percentage Y: Y X: X Y: Y X: X% of Y
INT:FLOR FLOOR Truncate toward negative infinity    
INT:CEIL   Truncate toward positive infinity    
INT:ROND ROUND Truncate toward nearest integer (even rule)    
INT:TRUN IP Truncate toward zero    
INT:▒▒▒▒        
INT:FP        
INT:GCD   Greatest Common Denominator    
INT:LCM   Least Common Multiple    
INT:▒▒▒▒        
INT:▒▒▒▒        
INT:DIV BASE÷      
INT:REM MOD      
BINO:COMB        
BINO:PERM        
BINO:! N!      
BINO:PSI DIGAMM      
BINO:GAM GAMMA      
BINO:BETA   Beta function    
ERR:ERF   Error Function    
ERR:ERFC   Complementary Error Function    
ERR:▒▒▒▒        
ERR:NPDF   Standard Normal Probablity Density FUnction    
ERR:NCDF   Standard Normal Cumulative Distribution FUnction    
ERR:▒▒▒▒        
MISC:¦¦   Parallel Operator Y X X: 1/(1/X+1/Y)

4. Menu Code

(MJR-generate-42-menu-code "SFUN" 0 tbl 0 1 'stay 'up 'auto #'MJR-custom-gen-lab #'MJR-custom-gen-sub)

5. Functions

5.1. Complex Numbers

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (RPART)
@@@@ DSC: Real part
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: rpart(x)
@@@@ LBL: 69-72
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-22
LBL "RPART"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
XEQ 89          @@@@ ATYP
X=0?
RTNERR 4        @@## REQ:free42>=2.5.24
1/X             @@## REQ:free42<2.5.24
68
+
GTO IND ST X
LBL 69          @@@@ Real Number
LBL 70          @@@@ Real Matrix
R↓
RTN
LBL 71          @@@@ Complex Number
LBL 72          @@@@ Complex Matrix
R↓
XEQ 98          @@@@ C→R&I
X<>Y
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (IPART)
@@@@ DSC: Imaginary Part
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: ipart(x)
@@@@ LBL: 73-76
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-22
LBL "IPART"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
XEQ 89          @@@@ ATYP
X=0?
RTNERR 4        @@## REQ:free42>=2.5.24
1/X             @@## REQ:free42<2.5.24
72
+
GTO IND ST X
LBL 73          @@@@ Real Number
0
RTN
LBL 74          @@@@ Real Matrix
R↓
DIM?
NEWMAT
RTN
LBL 75          @@@@ Complex Number
LBL 76          @@@@ Complex Matrix
R↓
XEQ 98          @@@@ C→R&I
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CONJ)
@@@@ DSC: Complex Conjugate
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: conj(x)
@@@@ TST: free42_3.0.2
@@@@ LBL: 77-80
@@@@ BUG: Fails on alpha string matrix
@@@@ UPD: 2021-04-22
LBL "CONJ"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
XEQ 89          @@@@ ATYP
X=0?
RTNERR 4        @@## REQ:free42>=2.5.24
1/X             @@## REQ:free42<2.5.24
76
+
GTO IND ST X
LBL 77          @@@@ Real Number
LBL 78          @@@@ Real Matrix
R↓
RTN
LBL 79          @@@@ Complex Number
LBL 80          @@@@ Complex Matrix
R↓
COMPLEX
+/-
COMPLEX
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CABS)
@@@@ DSC: Magnitude/absolute value
@@@@ IN:  X: Number or numeric matrix (element-wise) -- built in ABS won't work with a complex matrix
@@@@ OUT: X: |x|
@@@@ TST: CPXRES free42_3.0
@@@@ LBL: 81-84
@@@@ BUG: Returns 0 for real 0 input
@@@@ UPD: 2024-02-12
LBL "CABS"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
XEQ 89          @@@@ ATYP
X=0?
RTNERR 4        @@## REQ:free42>=2.5.24
1/X             @@## REQ:free42<2.5.24
80
+
GTO IND ST X
LBL 81          @@@@ Real Number
LBL 82          @@@@ Real Matrix
R↓
ABS
RTN
LBL 83          @@@@ Complex Number
LBL 84          @@@@ Complex Matrix
R↓
XEQ 47          @@@@ C→M&A
X<>Y
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CARG)
@@@@ DSC: Complex Argument
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: arg(x)
@@@@ TST: CPXRES free42_3.0
@@@@ LBL: 85-88
@@@@ BUG: Returns 0 for real 0 input
@@@@ UPD: 2024-02-12
LBL "CARG"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
XEQ 89          @@@@ ATYP
X=0?
RTNERR 4        @@## REQ:free42>=2.5.24
1/X             @@## REQ:free42<2.5.24
84
+
GTO IND ST X
LBL 85          @@@@ Real Number
0
RTN
LBL 86          @@@@ Real Matrix
R↓
DIM?
NEWMAT
RTN
LBL 87          @@@@ Complex Number
LBL 88          @@@@ Complex Matrix
R↓
XEQ 47          @@@@ C→M&A
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Complex Number -> Real Part & Imaginary Part
@@@@ NAM: C→R&I 98
@@@@ IN:  X: Complex Number or Complex Matrix
@@@@ OUT: Y: Real Part of X
@@@@      X: Imaginary Part of X
@@@@ LBL: 97
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-22
LBL 98
FUNC 12         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
FS? 73
GTO 97
@@@@ RECT MODE
COMPLEX
RTN
LBL 97
@@@@ POLAR MODE
RECT
COMPLEX
POLAR
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Complex Number -> Magnitude & Argument (angle)
@@@@ NAM: C→M&A 47
@@@@ IN:  X: Complex Number or Complex Matrix
@@@@ OUT: Y: Magnitude of X
@@@@      X: Complex Argument (angle) of X
@@@@ LBL: 99
@@@@ TST: free42_3.0.2
@@@@ UPD: 2024-02-12
LBL 47
FUNC 12         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
FS? 73
GTO 99
@@@@ RECT MODE
POLAR
COMPLEX
RECT
RTN
LBL 99
@@@@ POLAR MODE
COMPLEX
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Arithmetic Type
@@@@ NAM: ATYP 89
@@@@ IN:  X: an object
@@@@ OUT: X: 1 if input X was a real number
@@@@         2 if input X was a real matrix
@@@@         3 if input X was a complex number
@@@@         4 if input X was a complex matrix
@@@@         0 if none of the above are true
@@@@ TST: free42_3.0.2
@@@@ FAQ: Even on free42<2.5.24 or a real 42s, X, Y, & Z are preserved. T & Last X not so much.
@@@@ BUG: A 2 will be returned for a real matrix containing strings
@@@@ BUG: In infinite stack mode with an empty stack 2 will be returned as X=0 when we do L4STK
@@@@ LBL: 90-97
@@@@ UPD: 2021-02-23
LBL 89
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
MAT?
GTO 90
GTO 91
LBL 90          @@@@ Matrix
CLA
ARCL ST X
67
POSA
X>0?
GTO 96
GTO 97
LBL 96          @@@@ Complex Matrix
4
RTN
LBL 97          @@@@ Real/String Matrix
2
RTN
LBL 91          @@@@ Not matrix
REAL?
GTO 92
GTO 93
LBL 92          @@@@ Real number
1
RTN
LBL 93          @@@@ Not matrix or real
CPX?
GTO 94
GTO 95
LBL 94          @@@@ Complex number
3
RTN
LBL 95          @@@@ Not matrix, real, or complex
0
RTN

5.2. Percentages

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (PTOT)
@@@@ DSC: Percentage of total (just like hp-12c button)
@@@@ IN:  Y: Real Number -- Total
@@@@      X: Real Number -- Part
@@@@ OUT: Y: Y
@@@@      X: 100*X/Y
@@@@ UPD: 2021-03-12
LBL "%T"
FUNC 22         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
RCL÷ ST Y
100
×
RTN

5.3. Integers

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ROUND)
@@@@ DSC: Properly round to integer.  N.5 rounded to nearest even number.
@@@@ IN:  X: real number
@@@@ OUT: X: floor(X)
@@@@ UPD: 2021-02-23
@@@@ TST: free42_3.0.2
@@@@ TC:  -2.0 -2 →Int
@@@@ TC:  -1.6 -2 →Nearest
@@@@ TC:  -1.5 -2 →Even
@@@@ TC:  -1.4 -1 →Nearest
@@@@ TC:  -1.0 -1 →Int
@@@@ TC:  -1.6 -1 →Nearest
@@@@ TC:  -0.5  0 Even Rule
@@@@ TC:  -0.4  0 →Nearest
@@@@ TC:   0.0  0 →Int
@@@@ TC:   0.4  0 →Nearest
@@@@ TC:   0.5  0 Even Rule
@@@@ TC:   1.6  1 →Nearest
@@@@ TC:   1.0  1 →Int
@@@@ TC:   1.4  1 →Nearest
@@@@ TC:   1.5  2 →Even
@@@@ TC:   1.6  2 →Nearest
@@@@ TC:   2.0  2 →Int
LBL "ROUND"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER           @@@@ X        X
FP              @@@@ FP       X
ABS             @@@@ |FP|     X
0.5             @@@@ 1/2      |FP|     X
-               @@@@ |FP|-1/2 X
X<>Y            @@@@ X        |FP|-1/2 X
SIGN            @@@@ SGN      |FP|-1/2 X 
LASTX           @@@@ X        SGN      |FP|-1/2 X 
IP              @@@@ IP(X)    SGN      |FP|-1/2 X 
0=? ST Z
GTO 67
0<? ST Z
+
RTN
LBL 67 
@@@@ FP=1/2
XEQ 68          @@@@ ODD?
+
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: RETYES if X is odd, RTNNO otherwise
LBL 68          @@@@ ODD?
FUNC 00
L4STK
2
÷
FP
X=0?
RTNNO
RTNYES

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (FLOOR)
@@@@ DSC: Floor -- Round toward negative infinity
@@@@ IN:  X: real number
@@@@ OUT: X: floor(X)
@@@@ UPD: 2021-02-23
@@@@ TST: free42_3.0.2
LBL "FLOOR"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
FP
LASTX
IP
0≤? ST Y
RTN
1
-
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CEIL)
@@@@ DSC: Ceiling -- Round toward positive infinity
@@@@ IN:  X: real number
@@@@ OUT: X: ceil(X)
@@@@ UPD: 2021-02-23
@@@@ TST: free42_3.0.2
LBL "CEIL"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
FP
LASTX
IP
0≥? ST Y
RTN
1
+
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (GCD)
@@@@ DSC: GCD
@@@@ IN:  Y: real number
@@@@      X: real number
@@@@ OUT: X: GCD(|IP(X)|, |IP(X)|)
@@@@ LBL: 66
@@@@ UPD: 2021-04-22
@@@@ TST: free42_3.0.2
LBL "GCD"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ABS
IP
X<>Y
ABS
IP
X>Y?
X<>Y
LBL 66
STO ST Z
MOD
X>0?
GTO 66
R↓
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (LCM)
@@@@ DSC: LCM
@@@@ IN:  Y: real number
@@@@      X: real number
@@@@ OUT: X: LCM(|IP(X)|, |IP(X)|)
@@@@ USE: GCD
@@@@ UPD: 2021-04-22
@@@@ TST: free42_3.0.2
LBL "LCM"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ABS             @@@@ |X|             Y
IP              @@@@ IP(|X|)         Y
X=0?                                      
RTN                                       
X<>Y            @@@@ Y               IP(|X|) 
ABS             @@@@ |Y|             IP(|X|) 
IP              @@@@ IP(|Y|)         IP(|X|) 
X=0?                                      
RTN                                       
RCL ST Y        @@@@ IP(|X|)         IP(|Y|)         IP(|X|) 
RCL ST Y        @@@@ IP(|Y|)         IP(|X|)         IP(|Y|)         IP(|X|) 
×               @@@@ IP(|Y|)*IP(|X|) IP(|Y|)         IP(|X|) 
RCL ST Z        @@@@ IP(|X|)         IP(|Y|)*IP(|X|) IP(|Y|)         IP(|X|) 
RCL ST Z        @@@@ IP(|Y|)         IP(|X|)         IP(|Y|)*IP(|X|) IP(|Y|)
XEQ "GCD"       @@@@ GCD             IP(|Y|)*IP(|X|) IP(|Y|)         IP(|Y|)
÷               @@@@ LCM             IP(|Y|)         IP(|Y|)         IP(|Y|)
RTN

5.4. Binomials, Factorals, Beta, etc…

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (BETA)
@@@@ DSC: beta function
@@@@ IN:  Y: Number
@@@@ IN:  X: Number
@@@@ OUT: X: beta(x, y) = beta(y, x)
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-02-23
LBL "BETA"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
RCL ST Y
GAMMA
RCL ST Y
GAMMA
×
RCL ST Z
RCL ST Z
+
GAMMA
÷
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (DIGAMM)
@@@@ DSC: digamma function
@@@@ IN:  X: Number
@@@@ OUT: X: psi(X)
@@@@ FAQ: Good to about 1e-5 for real X>0.1
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-05-02
LBL "DIGAMM"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
LSTO "_X"       @@@@ X
2               @@@@ 2 X
+               @@@@ 2+X
LSTO "_S"       @@@@ S
LN              @@@@ SUM            ln(s)
2               @@@@ 2 S SUM
RCL× "_S"       @@@@ 2*S SUM
1/X             @@@@ TRM SUM         
-               @@@@ SUM            ln(s) -1/(2*s)
RCL "_S"        @@@@ S SUM
X↑2             @@@@ S^2 SUM
LSTO "_SS"      @@@@ SS SUM
12              @@@@ 12 S^2 SUM
×               @@@@ 12*S^2 SUM
1/X             @@@@ TRM SUM
-               @@@@ SUM             ln(s) -1/(2*s) -1/(12*s^2)
RCL "_SS"       @@@@ S^2 SUM       
RCL× "_SS"      @@@@ S^4 SUM       
STO ST Z        @@@@ S^4 SUM S^4
120
×
1/X
+               @@@@ SUM S^4         ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4)
X<>Y
RCL× "_SS"      @@@@ S^6 SUM       
STO ST Z        @@@@ S^6 SUM S^6
252
×
1/X         
-               @@@@ SUM S^6         ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4) -1/(252*s^6)
X<>Y
RCL× "_SS"      @@@@ S^8 SUM       
STO ST Z        @@@@ S^8 SUM S^8
240
×
1/X         
+               @@@@ SUM S^8         ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4) -1/(252*s^6) +1/(240*s^8)
X<>Y
RCL× "_SS"      @@@@ S^10 SUM       
STO ST Z        @@@@ S^10 SUM S^10
660
×
1/X
5
×        
-               @@@@ SUM S^10        ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4) -1/(252*s^6) +1/(240*s^8) -5/(660*s^10)
X<>Y
RCL× "_SS"      @@@@ S^12 SUM       
STO ST Z        @@@@ S^12 SUM S^12
32760
×
1/X
691
×        
+               @@@@ SUM S^12        ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4) -1/(252*s^6) +1/(240*s^8) -5/(660*s^10) +691/(32760*s^12)
X<>Y
RCL× "_SS"      @@@@ S^14 SUM       
12
×
1/X
-               @@@@ SUM             ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4) -1/(252*s^6) +1/(240*s^8) -5/(660*s^10) +691/(32760*s^12) -1/(12*s^14)
RCL "_X"                   
1/X                        
-               @@@@ SUM             ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4) -1/(252*s^6) +1/(240*s^8) -5/(660*s^10) +691/(32760*s^12) -1/(12*s^14) -1/x
RCL "_X"                   
1                          
+                          
1/X                        
-               @@@@ SUM             ln(s) -1/(2*s) -1/(12*s^2) +1/(120*s^4) -1/(252*s^6) +1/(240*s^8) -5/(660*s^10) +691/(32760*s^12) -1/(12*s^14) -1/x -1/(x+1)
RTN

5.5. Error & Standard Normal Functions

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (NPDF)
@@@@ DSC: Standard Normal PDF
@@@@ IN:  X: real number
@@@@ OUT: X: Standard Normal PDF value at X
@@@@ UPD: 2021-04-22
@@@@ TST: free42_3.0.2
@@@@ TC:  -2 0.05399096651318805195056
@@@@ TC:  -1 0.2419707245191433497978
@@@@ TC:   0 0.3989422804014326779399
@@@@ TC:   1 0.2419707245191433497978
@@@@ TC:   2 0.05399096651318805195056
LBL "NPDF"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
X↑2             @@@@ X^2
-2              @@@@ -2                     X^2
÷               @@@@ -X^2/2
E↑X             @@@@ EXP(-X^2/2)
2               @@@@ 2                      EXP(-X^2/2)
PI              @@@@ PI                     2             EXP(-X^2/2)
×               @@@@ PI*2                   EXP(-X^2/2)
SQRT            @@@@ SQRT(PI*2)             EXP(-X^2/2)
÷               @@@@ EXP(-X^2/2)/SQRT(PI*2) 
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (NCDF)
@@@@ DSC: Standard Normal CDF
@@@@ IN:  X: real number
@@@@ OUT: X: Standard Normal CDF value at X
@@@@ BUG: Only good to 7 decimal places
@@@@ FAQ: No dependancies, variables, loops, or branches
@@@@ REF: Zelen & Severo (1964)
@@@@ UPD: 2021-04-22
@@@@ TST: free42_3.0.2
@@@@ TC:  -2 0.02275013194817920720028
@@@@ TC:  -1 0.1586552539314570514148 
@@@@ TC:   0 0.5                      
@@@@ TC:   1 0.8413447460685429485852 
@@@@ TC:   2 0.9772498680518207927997 
LBL "NCDF"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
0.2316419       @@@@ b0                     X             ?           ?
RCL× ST Y       @@@@ b0*X                   X             ?           ?
1               @@@@ 1                      b0*X          X           ?           
+               @@@@ 1+b0*X                 X             ?           ?
1/X             @@@@ 1/(1+b0*X)             X             ?           ?
                @@@@ T                      X             ?           ?
X<>Y            @@@@ X                      T             ?           ?
X↑2             @@@@ X^2                    T             ?           ?
-2              @@@@ -2                     X^2           ?           ?
÷               @@@@ -X^2/2                 T             ?           ?
E↑X             @@@@ EXP(-X^2/2)            T             ?           ?
2               @@@@ 2                      EXP(-X^2/2)   T           ?
PI              @@@@ PI                     2             EXP(-X^2/2) T
×               @@@@ PI*2                   EXP(-X^2/2)   T           T
SQRT            @@@@ SQRT(PI*2)             EXP(-X^2/2)   T           T
÷               @@@@ EXP(-X^2/2)/SQRT(PI*2) T             T           T
                @@@@ N                      T             T           T
RCL  ST Y       @@@@ T                      N             T           T
×               @@@@ NT                     T             T           T
0.319381530     @@@@ b1                     NT            T           T
RCL× ST Y       @@@@ PR                     NT            T           T
RCL ST Z        @@@@ T                      PR            NT          T
STO× ST Z       @@@@ T                      PR            NT^2        T
R↓              @@@@ PR                     NT^2          T           T
-0.356563782    @@@@ b2                     PR            NT^2        T
RCL× ST Z       @@@@ NT                     PR            NT^2        T
+               @@@@ PR                     NT^2          T           T
RCL ST Z        @@@@ T                      PR            NT^2        T
STO× ST Z       @@@@ T                      PR            NT^3        T
R↓              @@@@ PR                     NT^3          T           T
1.781477937     @@@@ b3                     PR            NT^3        T
RCL× ST Z       @@@@ NT                     PR            NT^3        T
+               @@@@ PR                     NT^3          T           T
RCL ST Z        @@@@ T                      PR            NT^3        T
STO× ST Z       @@@@ T                      PR            NT^4        T
R↓              @@@@ PR                     NT^4          T           T
-1.821255978    @@@@ b4                     PR            NT^4        T
RCL× ST Z       @@@@ NT                     PR            NT^4        T
+               @@@@ PR                     NT^4          T           T
RCL ST Z        @@@@ T                      PR            NT^4        T
STO× ST Z       @@@@ T                      PR            NT^5        T
R↓              @@@@ PR                     NT^5          T           T
1.330274429     @@@@ b5                     PR            NT^5        T
RCL× ST Z       @@@@ NT                     PR            NT^5        T
+               @@@@ PR                     NT^5          T           T
1               @@@@ 1                      PR            NT^5        T
X<>Y            @@@@ PR                     1             NT^5        T
-               @@@@ 1-PR                   NT^5          T           T
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ERF)
@@@@ DSC: erf (error) function
@@@@ IN:  X: real number
@@@@ OUT: X: erf(X)
@@@@ USE: NCDF
@@@@ LBL: Use: 64-65
@@@@ UPD: 2021-03-30
@@@@ TST: free42_3.0.2
@@@@ TC: -1 -0.8427007929497148693412
@@@@ TC:  0  0.0
@@@@ TC:  1  0.8427007929497148693412
@@@@ TC:  2  0.9953222650189527341621
LBL "ERF"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ENTER
ENTER
2
SQRT
×
ABS
XEQ "NCDF"
2
×
1
-
X<>Y
X<0?
GTO 64
GTO 65
LBL 64
R↓
+/-
RTN
LBL 65
R↓
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ERFC)
@@@@ DSC: erfc (complementary error) function
@@@@ IN:  X: real number
@@@@ OUT: X: erfc(X)
@@@@ USE: ERF
@@@@ UPD: 2021-03-30
@@@@ TST: free42_3.0.2
@@@@ TC: -1 1.842700792949714869341
@@@@ TC:  0 1.0
@@@@ TC:  1 0.1572992070502851306588
@@@@ TC:  2 0.004677734981047265837931
LBL "ERFC"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
XEQ "ERF"
1
X<>Y
-
RTN

5.6. Hyperbolic Trigonometric Functions

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CSCH)
@@@@ DSC: 1/SINH(X)
LBL "CSCH"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
SINH
1/X
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SECH)
@@@@ DSC: 1/COSH(X)
LBL "SECH"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
COSH
1/X
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (COTH)
@@@@ DSC: 1/TANH(X)
LBL "COTH"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
TANH
1/X
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ACSCH)
@@@@ DSC: SINH(1/X)
LBL "ACSCH"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
1/X
ASINH
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ASECH)
@@@@ DSC: COSH(1/X)
LBL "ASECH"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
1/X
ACOSH
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ACOTH)
@@@@ DSC: TANH(1/X)
LBL "ACOTH"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
1/X
ATANH
RTN

5.7. Trigonometric Functions

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (AHAV)
@@@@ DSC: Inverse Haversine
@@@@ IN:  X: number
@@@@ OUT: X: ahav(X)=2*asin(sqrt(X))
@@@@ UPD: 2021-04-18
@@@@ TST: free42_3.0.2
LBL "AHAV"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
SQRT
ASIN
2
×
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (HAV)
@@@@ DSC: Haversine
@@@@ IN:  X: number
@@@@ OUT: X: hav(X)=1/2-cos(X)/2
@@@@ UPD: 2021-04-18
@@@@ TST: free42_3.0.2
LBL "HAV"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
COS
-2
÷
0.5
+
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (HYPOT)
@@@@ DSC: Hypot
@@@@ IN:  Y: number
@@@@ IN:  X: number
@@@@ OUT: X: sqrt(abs(x)^2+abs(y)^2)
@@@@ UPD: 2021-02-23
@@@@ TST: free42_3.0.2
LBL "HYPOT"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
ABS
X<>Y
ABS
COMPLEX
ABS
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (CSC)
@@@@ DSC: 1/SIN(X)
LBL "CSC"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
SIN
1/X
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (SEC)
@@@@ DSC: 1/COS(X)
LBL "SEC"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
COS
1/X
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (COT)
@@@@ DSC: 1/TAN(X)
LBL "COT"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
TAN
1/X
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ACSC)
@@@@ DSC: SIN(1/X)
LBL "ACSC"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
1/X
ASIN
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ASEC)
@@@@ DSC: COS(1/X)
LBL "ASEC"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
1/X
ACOS
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ACOT)
@@@@ DSC: TAN(1/X)
LBL "ACOT"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
1/X
ATAN
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ATAN2)
@@@@ DSC: ATAN2
@@@@ IN:  Y: number
@@@@ IN:  X: number
@@@@ OUT: X: atan2(y, x)
@@@@ BUG: Only works in RAD mode
@@@@ UPD: 2021-02-23
@@@@ TST: free42_3.0.2
@@@@ TC:  atan( 1, 1) =>  pi/4     =   45°
@@@@ TC:  atan(-1, 1) => -pi/4     =  -45°
@@@@ TC:  atan( 1,-1) =>  3*pi/4   =  135°
@@@@ TC:  atan(-1,-1) => -3*pi/4   = -135°
@@@@ TC:  atan( 0, 1) =>  0        =    0°
@@@@ TC:  atan( 1, 0) =>  pi       =   90°
@@@@ TC:  atan(-1, 0) => -pi       =  -90°
@@@@ TC:  atan( 0, 0) => ERROR
@@@@ LBL: Used 59-63
LBL "TAN2"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
X>0?
GTO 59
X=0?
GTO 60
@@@@ X<0
X<>Y
X<0?
GTO 61
@@@@ X<0 & Y>=0
X<>Y
÷
ATAN
PI
+
RTN
LBL 61
@@@@ X<0 & Y<0
X<>Y
÷
ATAN
PI
-
RTN
LBL 60
X<>Y
X=0?
GTO 62
X>0?
GTO 63
@@@@ X=0 & Y<0
PI
-2
÷
RTN
LBL 63
@@@@ X=0 & Y>0
PI
2
÷
RTN
LBL 62
@@@@ X=0 & Y=0 ERROR 0/0
÷
RTN
LBL 59
@@@@ X>0
÷
ATAN
RTN

5.8. Logs, Powers & Roots

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (lnYX)
@@@@ DSC: Base Y Logarithm of X
@@@@ IN:  Y: logarithm base
@@@@      X: number or matrix (element-wise)
@@@@ OUT: X: log_y(x)
@@@@ UPD: 2021-04-14
@@@@ TST: free42_3.0.2
LBL "lnYX"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
LN
X<>Y
LN
÷
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ln2)
@@@@ DSC: Base 2 Logarithm
@@@@ IN:  X: number or matrix (element-wise)
@@@@ OUT: X: log_2(x)
@@@@ UPD: 2021-02-23
@@@@ TST: free42_3.0.2
LBL "ln2"
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
LN
2
LN
÷
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Raise 2 to the power of X
@@@@ NAM: 2↑X 58
@@@@ IN:  X: number or matrix (element-wise)
@@@@ OUT: X: 2^X
@@@@ UPD: 2021-02-23
@@@@ TST: free42_3.0.2
LBL 58
FUNC 11         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
2
X<>Y
Y^X
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Square root prefering real & pure imaginary answers 
@@@@ NAM: ROOT2 57  
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: root of X
@@@@ FAQ: See XYROOT for details
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-14
LBL 57
FUNC 11         @@## REQ:free42>=2.5.24
2
X<>Y
XEQ "YROOT"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Cube root prefering real & pure imaginary answers   
@@@@ NAM: ROOT3 56  
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: root of X
@@@@ FAQ: See XYROOT for details
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-14
LBL 56
FUNC 11         @@## REQ:free42>=2.5.24
3
X<>Y
XEQ "YROOT"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Fifth root prefering real & pure imaginary answers  
@@@@ NAM: ROOT5 55  
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: root of X
@@@@ FAQ: See XYROOT for details
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-14
LBL 55
FUNC 11         @@## REQ:free42>=2.5.24
5
X<>Y
XEQ "YROOT"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@ DSC: Seventh root prefering real & pure imaginary answers
@@@@ NAM: ROOT7 54  
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: root of X
@@@@ FAQ: See XYROOT for details
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-04-14
LBL 54
FUNC 11         @@## REQ:free42>=2.5.24
7
X<>Y
XEQ "YROOT"
RTN

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (YROOT)
@@@@ DSC: Nth roots prefering real & pure imaginary answers
@@@@ IN:  Y: Number
@@@@ IN:  X: Number or numeric matrix (element-wise)
@@@@ OUT: X: Yth root of X
@@@@ BUG: The principal value is not always returned (by design)
@@@@ BUG: Real integers are not recognized in complex form. i.e. 0+2i ≠ 2
@@@@ FAQ: Return is pure imaginary when Y is an odd integer and X<0
@@@@ FAQ: Return is real when  Y is an even integer and X<0
@@@@ TST: free42_3.0.2
@@@@ UPD: 2021-02-23
LBL "YROOT"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
REAL?
GTO 48
GTO 51
LBL 48
@@@@ X is real
X≥0?
GTO 51
@@@@ X is negative, real
RCL ST Y
REAL?
GTO 49
GTO 50
LBL 49
@@@@ X is negative, real; Y is real
FP
X≠0?
GTO 50
@@@@ X is negative, real; Y is real integer
R↓
ABS
RCL ST Y
X<>Y
XEQ 51
+/-
X<>Y
2
÷
FP
X=0?
GTO 53
GTO 52
LBL 53
@@@@ X is negative, real; Y is real integer even
R↓
-1
SQRT
×
RTN
LBL 52
@@@@ X is negative, real; Y is real integer odd
R↓
RTN
LBL 50
@@@@ Not special case.  Stack: Y X Y
R↓
LBL 51
@@@@ Not special case.  Stack: X Y
X<>Y
1/X
Y↑X
RTN

5.9. Everything Else

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ (ref:||)
@@@@ DSC: Parallel Operator
@@@@ IN:  Y: A Number
@@@@ IN:  X: A Number
@@@@ OUT: X: 1/(1/X+1/Y)
@@@@ UPD: 2021-04-28
LBL "||"
FUNC 21         @@## REQ:free42>=2.5.24
L4STK           @@## REQ:free42>=3.0
1/X
X<>Y
1/X
+
1/X
RTN

6. END

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
END

7. EOF