QPI v4.2 Mika Heiskanen
-------- mheiskan@delta.hut.fi
Based on QPirac by A.Coulom and MuPAD rationalize command.
QPI approximates any floating point numbers by a rational number, square
root, multiple of PI, exponential or a logarithm depending on which
approximation seems best.
QPI should work in all ports of any HP48 SX/GX model.
Allowed argument types are
real number --> symbolic / real number
complex number --> symbolic / real number
identifier --> identifier
local name --> local name
symbolic --> symbolic
array of real/complex --> list of symbolic/real
constants (PI,e) --> constant
list of above --> list of above
The rational approximation algorithm is taken from MuPAD, and works quite
well for numbers of any scale. The decision procedure for the minimal
approximation of real numbers is as follows
If x=zero then return (x)
nom,den=approximate(x)
If den < 100 then return (nom/den) % Early abort
nom2,den2=approximate(x*x)
If (x*x<5E5) & (den2<1000) & (den2<den) then % Choose 'smaller'
nom,den=sign(x)*nom2,den2
If nom<10 then return (sqrt(nom,den)) % Early abort
nom2,den2=approximate(x/pi)
If (|x/pi<100) & (den2<1000) & (den2<den) then % Choose 'smaller'
nom,den=nom2,den2
If nom<10 then return (nom/den*pi) % Early abort
nom2,den2=approximate(exp(x))
If (den2<50) & (den2<den) then % Choose 'smaller'
nom,den=nom2,den2
If nom<10 then return (ln(nom/den)) % Early abort
nom2,den2=approximate(ln(x))
If (x>0) & (den2<50) & (den2<den) then % Choose 'smaller'
nom,den=nom2,den2
If nom<10 then return (exp(nom/den)) % Early abort
Return (nom/den) in the found minimal form
The rational approximation algorithm itself uses 9-digit accuracy in
the approximation by default. The accuracy can be overridden by using
the FIX/SCI/ENG modes where the accuracy is taken to be the accuracy
of the display (1-11).
Example:
[ -.714285714286 -1.41421356237 ]
[ -.405465108108 1.11751906874 ]
[ -2.44346095279 .657047293577 ]
==>
{ { '-5/7' '-SQRT(2)' }
{ 'LN(2/3)' 'EXP(1/9)' }
{ '-7/9*PI' '5/7*SQRT(11/13' } }