[comp.sys.handhelds] HP48SX: conversion to cgs units program.

woodhams@phoenix.Princeton.EDU (Michael Woodhams) (01/31/91)

For some strange, dark reason, astrophysicists normally work in cgs
units rather than S.I. like the rest of the universe. Although I am
fighting this when I can, there are times when one has to express
results in cgs units. (SI = simple and intuitive. cgs = confuses
graduate students.) Therefore I have written the following little
program to act like the built in function "UBASE" except it reduces
units to cm instead of m, and g instead of kg. The following is
hand-entered, but I hope the format will be directly loadable by those
with cables to computers.

\<<
 IF DUP TYPE 13 \/= THEN	@ if top of stack is not a unit object
  KILL				@ quit program
 END
 UBASE				@ convert top of stack to SI
 DUP \->STR			@ make copy of SI unit object as a string
 DUP SIZE			@ find size of this string
 \-> ustr len			@ store string and length in local variables
  \<<
   ustr "m" POS			@ find position of "m" in string
   IF DUP 0 \/= THEN		@ if it is found, then
    \-> pos			@ store position of "m" in pos
     \<<
      ustr 1 pos 1 - SUB	@ extract all of string before "m"
      "c" +			@ concatanate "c"
      ustr pos len SUB +	@ concatanate all of string from "m" on
				@ (changes first "m" found into "cm")
      'ustr' STO		@ store back in string
     \>>
   ELSE				@ else ("m" not found)
    DROP			@ drop 0
   END
   ustr "k" POS			@ find position of "k" in string
   IF DUP 0 \/= THEN		@ if "k" found
    \-> pos			@ store position of "k" in pos
     \<<
      ustr 1 pos 1 - SUB	@ extract all of string before "k"
      ustr pos 1 + len SUB +	@ concatanate all of string after "k"
				@ (removes the first "k" from string,
				@ to change kg into g.)
      'ustr' STO		@ store back in string
     \>>
   ELSE				@ else ("k" not found)
    DROP			@ drop 0
   END
   ustr OBJ\->			@ turn string back into a unit object
				@ (this has correct units but not correct
				@ value for cgs version of original object)
   UFACT			@ convert original to new units.
 \>>
\>>
checksum #4718d, bytes 337.