From: jhmeyers@miu.edu (John H Meyers)
Subject: Units Scaling Program (Eng.notation)
Date: 23 Mar 1997 00:00:00 GMT
Message-ID: <5h3fqg$624$1@news.iastate.edu>
References: <01bc35d8$08391ce0$3f2adec7@lehua.net.lava.net>
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Reply-To: jhmeyers@miu.edu
Newsgroups: comp.sys.hp48


In article <01bc35d8$08391ce0$3f2adec7@lehua.net.lava.net>,
tj <sjvj@lava.net> writes:

> I would like to know if there is a program or if there is a way that would
> allow me to calculate using eng-notation using metric symbols T,G,K,m, ....

  If you mean converting to a specific unit yourself, then
  123456_m  1_km CONVERT --> 123.456_km    (etc.)

  If you mean a program that would automatically adjust the unit prefix
  in steps of 1000 (by multiplying or dividing accordingly, like certain
  Casio calculators having both ENG-> and <-ENG keys and unit symbols),
  then I copy below a revised post containing a program which was
  originally posted in October 1996.

  The program converts units which may already have a unit prefix attached;
  for typing in the characters associated with unit prefixes, you might
  wish to make a custom menu (see "Customizing" in the User's Guide),
  perhaps including the U1 and U2 program names in the menu as well.

--------------- Revised copy of post from October 1996 -------------------

When working with unit objects, we sometimes find that the numerical value
associated with a quantity is somewhat extreme, and that it would be more
conveniently expressed in terms of a unit having a different prefix, e.g.:

  123400000_mm -> 123.4_km

To this end, a directory of programs is provided below to transform any
unit expression into others which are equivalent, but have different
prefixes attached to the first (or only) unit in the expression.

These programs are somewhat reminiscent of the ENG-> and <-ENG functions
on certain Casio calculators, which allow you to utilize them several times
in succession, each time sumultaneously changing both the displayed value
and exponent (or "unit symbol" in some models) so that the value remains
equivalent, even though the presentation keeps changing.

To use these programs, simply enter a unit expression on the stack
and press U1 or U2 (multiple times if desired); the unit expression
keeps changing to display different combinations of numeric part
and unit portion.  U1 and U2 perform scaling in exactly opposite
directions, so that you can reverse the effects.

With compound units (e.g. 45_cm/s), only the first unit in the expression
is scaled.  Reciprocal units (e.g. 20_1/s) and powers of units
(e.g. 5_ft^2) are correctly handled.

Note that if you start with 1_in and press U1 a few times, you will
soon see 1000_min on the stack; this happens to be "milli-inches,"
*not* minutes!  Proof: press UBASE, see .0254_m, *not* 60000_s !
Do *not* edit such "ambiguous" expressions in the command line,
or else the HP48 *will* then change its interpretation of the
*string* "min" to mean "minutes"!

The prefixes "c", "d", "D", and "h" are always accepted with
input units, but may be optionally "stepped over" in the sequence
of output units, depending on your preference (see the listing below).

Caution!!!  SYSEVAL's are potentially dangerous if used incorrectly, typed
incorrectly, etc.; such errors could even cause complete loss of data in
HP48 memory.  While I believe that only "supported" (unchanging) system
functions have been used, no responsibility can be assumed by the author
for the results, and it is highly recommended that you backup all data
before using these programs.  Hand-typing is possible, but great care should
be exercised (yes, "@" introduces *comments*; don't bother typing them!)

Cryptic-looking comments in the ascii listing name the corresponding
SysRPL words used or compared; please forgive the lack of further
explanation about how Unit objects are constructed (perhaps you
can figure it out from this program).

When you realize that you now don't need your Casio's for anything at all,
you can always "...send me your tired, your poor, your huddled Casios,
yearning to breathe free, the wretched refuse of your teeming drawers --
send these, the homeless, tempest-tossed, to me..."
                                     - With apologies to Emma Lazarus

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

%%HP: T(3); @ header for Kermit, \-> denotes right-arrow, etc.
DIR @ Unit-scaling program for HP48 (see comments for S/SX/G/GX versions)
@ Unit_expr1 U1[U2] -> Unit_expr2 [ equivalent (value * scaled_unit) ]

  U1 \<< 0 UU \>> @ first-appearing unit gets smaller

  U2 \<< 1 UU \>> @ first-appearing unit gets larger

  @  Common internal processing function follows:
  UU \<< @ unit_obj  0/1  ->  converted_equivalent_object

     \<< @ sub-program to select next unit-prefix (scaling) letter:

@ Choose ONE set of a PAIR of strings shown below, according to
@ calculator model, and preference of whether or not the output
@ should step through all the small prefixes (100, 10, .1, .01)
@ as well as the "Engineering" prefixes (only multiples of 1000)

@ As of this writing, just one choice has been "uncommented" below;
@ no modification is necessary if you want the pre-selected choice:

@ For the G/GX, to include the small prefixes (10 and 100), use:
@ "yzafpn\Gmmcd.DhkMGTPEZYY"
@ "YZEPTGMkhD.dcm\Gmnpfazyy"

@ For the G/GX, to skip over the small prefixes (10 and 100), use:
  "yzafpn\Gmm.kMGTPEZYYc.d.Dkhk"
  "YZEPTGMk.m\Gmnpfazyyh.D.dmcm"

@ For the S/SX, to include the small prefixes (10 and 100), use:
@ "afpn\Gmmcd.DhkMGTPEE"
@ "EPTGMkhD.dcm\Gmnpfaa"

@ For the S/SX, to skip over the small prefixes (10 and 100), use:
@ "afpn\Gmm.kMGTPEEc.d.Dkhk"
@ "EPTGMk.m\Gmnpfaah.D.dmcm"

@ The "doubled" letter in each of the above strings prevents further
@ scaling after the "final" possible prefix has been reached.

@ Note that \Gm represents a Greek lowercase "mu" ("micro" prefix).

  IFTE DUP ROT POS 1 + DUP SUB @ select which string, per U1 or U2
     \>> \-> d p @ d = direction-of-conversion, p = sub-program
     \<<
        DUP TYPE 13 \=/ { 514 DOERR } IFT @ must be a unit object
        DUP #54AFh SYSEVAL #5459h SYSEVAL @ INNERCOMP, {}N
            DUP 2 GET "1" SAME 3 2 IFTE DUP2 GET @ reciprocal units (tok1) ?
        IF DUP TYPE 24 == @ character object type (unit prefix letter)
        THEN #6475Ch SYSEVAL d p EVAL NUM @ CHR>$, get next prefix symbol
          IF DUP 46 \=/ @ "." indicates "remove previous prefix"
          THEN #18CEAh SYSEVAL #5A75h SYSEVAL 1 \->LIST REPL @ COERCE, #>CHR
          ELSE DROP DUP2 1 SWAP 1 - SUB ROT ROT DUP2 1 + GET
               ROT ROT 3 + OVER SIZE SUB + + @ remove prefix tokens
          END
        ELSE ROT ROT DUP2 @ original first unit was not prefixed
          1 SWAP 1 - SUB ROT ROT 1 + OVER SIZE SUB ROT @ break unit apart
          "." d p EVAL NUM #18CEAh SYSEVAL #5A75h SYSEVAL @ COERCE, #>CHR
          SWAP #10B7Ch SYSEVAL 3 \->LIST SWAP + + @ { .. Char "unit" umP .. }
        END
        #54AFh SYSEVAL #5481h SYSEVAL CONVERT @ INNERCOMP, EXTN=UNITN
     \>>
     \>> @ end of program 'UU'
END @ End of directory, stop here for Kermit transfer
@ Unmodified: 635 bytes (on stack), #4CD4h checksum (Verify me!)
@ Save 130 bytes by using Joe Horn's PACK, from Goodies Disk #2.

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