koslowj@math.ksu.edu (Juergen Koslowski) (04/18/91)
The recently posted program NEW2Q besides being rather long and displaying an unfortunate mix of RPL and "algebraic" notation doesn't quite work as advertised. The fraction returned for "e" and "7" is `23225/8544', which when subtracted from "e" leaves a result with 8 zeros after the decimal point. However, the fraction `15062/5541' is simpler and results in an error with 7 zeros after the decimal point. The following program is much shorter and does correctly find `15062/5541'. It handles negative decimals better than NEW2Q, and it consistently returns fractions of the form `a/b', even if the argument in level 1 is "0" or "1". (If you eliminate the "NEG ALOG" part, you can explicitly enter the desired error on level 1, rather than the desired number of zeros after the decimal point. In essentially this form I posted the program to the HP28 list in the Fall of 1989.) Drawbacks: Occasinally, the program can take a long time to finish. Example: "pi" and "11". Advantages: The simplest fraction approximating the decimal to the given accuracy is guaranteed to be found. References: Section 4.5 in "Concrete Mathematics," by R. L. Graham, D. E. Knuth, and O. Patashnik, Addison Wesley, 1989 Enjoy, J"urgen Koslowski Department of Mathematics Kansas State University koslowj@math.ksu.edu ******************************************************************************** %%HP: T(3)A(R)F(.); \<< NEG ALOG \-> a e \<< a ABS (0,1) (1,0) DO IF ROT DUP 1 < THEN 1 OVER - / 3 ROLLD OVER + ELSE 1 - OVER 4 ROLL + ROT END UNTIL DUP2 + C\->R / a ABS - ABS e < END + SWAP DROP C\->R # 5603Eh SYSEVAL a SIGN * \>> \>> ********************************************************************************