From: jhmeyers@miu.edu (John H Meyers)
Newsgroups: comp.sys.hp48
Subject: Units conversion aids
Date: 15 Oct 1998 06:23:31 GMT
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Lines: 118
Message-ID: <7044d3$ga1$1@news.iastate.edu>
Reply-To: jhmeyers@miu.edu
NNTP-Posting-Host: vax1.mum.edu
Xref: republic.btigate.com comp.sys.hp48:17326

On the theme of units conversions, here are three simple "user"
programs; the first two progs expect one unit object on the stack:

o UM - Go immediately to the menu of suitable units to convert to
       (an alternative to the G/GX MUC program, and works on S/SX).

o US - Convert automatically to a pre-chosen set of "preferred" units.

o CU - A single menu of your personalized most common conversions.

All pure "user" coding, all work on S/SX/G/GX; potential
candidates for user key or custom menu assignments
(Chapter 30 in G/GX User's Guide).

Did someone say you could not "comment" user programs?
Well, these programs are *mostly* comments, which you
can maintain as text either on your PC or in your HP48;
the built-in compiler (or Kermit ascii download,
or OBJ\-> command) automatically ignores the comments.

----------------------------------------------------

%%HP: T(3); @ UM - Units conversion aid (S/SX/G/GX)

@ Jack Levy wrote a units conversion aid called MUC for the G/GX
@ (also included in the MathTools library); MUC pre-converts
@ the stack argument to multiple different units, and then
@ presents all the results in a single full-screen CHOOSE list,
@ from which you may select any one, or leave the argument as-is.

@ This program takes an alternative approach, locating the
@ menu where compatible units are found, after which you may
@ optionally make any desired conversion(s).  This is faster,
@ includes a few more unit types, and works also on S/SX.

@ Given a unit object on the stack, this program goes to whichever
@ built-in units menu contains the same type of units, so that you
@ may then convert to any equivalent unit via left-shift [new unit]

@ If there are no available conversions (e.g. for acceleration),
@ the program goes instead to the main units menu.

\<< @ Following is a list of UBASE unit *strings* for units menus
@ ("majority rule" for menus containing inconsistent items)

{ "m" "m^2" "m^3" "s" "m/s" "kg" "kg*m/s^2" "kg*m^2/s^2" "kg*m^2/s^3"
  "kg/(m*s^2)" "K" "A^2*s^3/(kg*m^2)" "r" "cd*sr/m^2" "m^2/s^2" }

@ Perform UBASE and select the appropriate menu (same for S/SX/G/GX)
OVER UBASE DUP UVAL / \->STR 4 OVER SIZE 1 - SUB POS 42 + MENU \>>

@ 223 bytes (on stack), #7DDDh checksum

@ Exercise for SysRPL class: Try re-writing this
@ to use a list of actual unit objects, rather than strings
@ (which would be faster), and then explain why it doesn't work.

@ Hint: Type in 1_m DUP UBASE (examine results thus far).
@       Then do BYTES ROT BYTES (compare results).
@       Use a disassembler or hack library to find out why.

----------------------------------------------------------

%%HP: T(3); @ US - Units conversion aid (S/SX/G/GX)

@ This program takes any old unit object on the stack,
@ plus a list of the *preferred* units you would like to use
@ for various measures, and converts the object to your preference.

@ The program below is a *model* of how to do this; you may
@ *customize* the conversion specifications yourself, for
@ as many different unit combinations as you care to specify.

@ Note that you are not limited to the types of units already
@ built-in; for example, you may create your own preferred
@ units for acceleration, for any user-defined units, etc.

\<< @ What follows is a list of unit pairs; each pair is the
@ UBASE units (as a string), followed by your preferred actual units:
{
  "m" 1_ft @ convert any distance to feet
  "m/s" 1_mph @ convert any speed to miles per hour
  "kg*m^2/s^3" 1_hp @ convert any power to horsepower
  "" 1_r @ convert pure numbers from current angle mode to radians
  @ Etc.
} @ Perform UBASE and select the appropriate conversion
OVER SWAP + DUP2 SWAP UBASE DUP UVAL /
\->STR 4 OVER SIZE 1 - SUB POS 1 + GET CONVERT \>>

@ The units you convert *to* may also be just the standard SI units
@ (e.g. 1_W for power), just to simplify some more complex-looking
@ expressions which may sometimes result from calculations.

@ If you would like to have several alternate conversion lists
@ to use at different times, you may simply separate the lists
@ from the above program, and use the particular list as an
@ additional argument to supply to the program, e.g.
@ some_units LST1 US (where variable LST1 contains the
@ desired list, and US is the rest of the above program).

----------------------------------------------------------------

%%HP: T(3);

@ A few everyday conversions, all in one menu (several pages),
@ with a Casio-like "generic units" conversion page appended.

@ Adapt this to your own most-used everyday conversions

\<< { 1_\^oF 1_\^oC { } 1_\^o 1_r { }  @ \^o is the "degrees" symbol
      1_mi 1_km { } 1_in 1_cm { }
      1_lb 1_kg { } 1_oz 1_g { }
      1_gal 1_l 1_qt 1_ozfl 1_ml 1_in^3
      { Mega 1_M? } { Kilo 1_k? } { 1 1_? } { Milli 1_m? }
        { Micro 1_\Gm? } UVAL } TMENU \>>

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>