From: jhmeyers@miu.edu (John H Meyers)
Subject: Casio-style ENG-> and <-ENG for HP48
Date: 30 Sep 1997 00:00:00 GMT
Message-ID: <60rcjc$6vs$1@news.iastate.edu>
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Reply-To: jhmeyers@miu.edu
Newsgroups: comp.sys.hp48


Background:

Many Casio and HP calculators have an ENG key intended
for displaying results in common "engineering" multiples of units
(e.g. grams, milligrams, kilograms, etc.); however, the philosophy of
what an ENG key should do differs between different brands of calculators:

In HP calculators, the ENG "key" is a programmable command to set a
persistent mode in which all numbers are subsequently displayed in
"scientific" notation (mantissa and exponent), but with the exponent
adjusted to be a multiple of 10^3; there is no choice among different
exponents, since a non-zero mantissa is always "normalized"
within the range 1 <= x < 1000, just as a "scientific" mantissa
is always normalized within the range 1 <= x < 10.

In most Casio calculators, the ENG key is non-programmable, usable
only by manually pressing the key each time you want to see a value,
temporarily affecting only the appearance of the currently displayed
value, which resumes its original appearance whenever any other key
is pressed.  If the ENG key is pressed repeatedly, the displayed exponent
keeps changing by another 10^3, and the displayed mantissa correspondingly
changes its appearance, until it can be shifted no further. 

The Casio ENG key also has an inverse (or "2nd") function,
which simply shifts things in the opposite direction.

Another Casio variant in some models optionally displays
"unit prefix" symbols ("k" for 10^3, "m" for 10^-3, etc.)
in place of the traditional "E+nn" style, at least for small nn.

Many past requests for a Casio-like "flexible" ENG display
for the HP48 seemed to be oriented to adjusting HP48 unit objects
to vary their prefixes, much like the latter Casio variant;
thus we would like to see 4500_mg <==> 4.5_g <==> .0045_kg etc.

A User-RPL program to do just this was last posted to the newsgroup
on 1997/03/23 (a revision of an earlier post of 1996/10/11); it can
even be used with "no-unit" values by appending the "generic" unit "?"

As with all prior newsgroup posts, the program can be retrieved
from <http://www.dejanews.com> using a "search filter" specifying
the newsgroup and other criteria, such as dates, authors, or keywords;
note that User-RPL programs (or even small binaries in "ASC" format) are
preserved as posted, while all "uuencoded" and similarly coded material
is filtered out of non-binary group posts by the archiving software
(if it hasn't already been "canceled" by Dick Depew's relentless bot).

Meanwhile, in article <341fabdb.0@news.actrix.gen.nz>,
jfouhy@atlantis.actrix.gen.nz (John Fouhy) asked for a similar
ENG function for pure numbers without units, so here comes one:


%%HP: T(3); @ \-> is right-arrow, \<- is left-arrow, etc. (Kermit header)
@ (The symbol at far left introduces *comments* -- do not type them)
@ (Kermit ascii transfers will automatically ignore comments)

@ A Casio-like ENG function for real numbers (S/SX/G/GX):

DIR      @  A directory, containing four named programs:

ENG\->   @  4.5 ==> "4.5E0" ==> "4500E-3" etc.
  \<< { * - } ENGX \>> @ Arithmetic operators for later use

ENG\<-   @  4.5 ==> "4.5E0" ==> ".0045E3" etc.
  \<< { / + } ENGX \>> @ Arithmetic operators for later use

NORM     @  "4.5E0" or "4500E-3" or ".0045E3" ==> 4.5
  \<< STR\-> \>>

ENGX     @ (internal function called by other programs)
  \<< LIST\-> DROP RCLF \-> a b f \<< @ Save operators and flags
  { @ This outer list contains three programs (sublists):
    { 514 DOERR }  @ Bad Argument Type (neither real nor string)
    { 3 EXP ENG \->STR { DROP DROP } ENGX } @ Recursive call to ENGX
    { @ String, e.g. ".0045E3" (hopefully representing a real number,
      DUP "E" POS DUP2   @      but no further arg checking is performed)
      1 + OVER SIZE SUB  @ "3" (exponent)
      ROT ROT 1 - 1 SWAP SUB  @ ".0045" (mantissa)
      DUP STR\-> 3 ALOG a EVAL STD \->STR  @ "4.5" or ".0000045"
      DUP "E" POS        @ "E" in string? ==> can not adjust further
        { DROP SWAP }    @ Leave previous values unchanged
        { SWAP DROP SWAP STR\-> 3 b EVAL \->STR } @ Adjust exponent
      IFTE "E" SWAP + +  @ Insert "E" between mantissa & exponent
    }
  } { 0 2 } 3 PICK TYPE POS 1 + GET EVAL f STOF \>> \>>

END @ End of the directory
@ Checksums: ENG\-> #D35Ah, ENG\<- #3F9Eh, NORM #2D80h, ENGX #AFFAh
@ Bytes (w/o names):  27.5,          27.5,        12.5,       265.5


Instructions:

With a real number on level 1, press either ENG key as often as desired;
the equivalent representations are displayed in string form.

Press the NORM key to revert back to the original real number.

Notes:

Works fine with "Comma as Fraction Mark" (-51 SF).

Signed numbers work fine, but unit objects are completely different,
and require a different program (mentioned earlier in this post).

Works with (and preserves) any current display mode (STD, FIX, SCI, ENG);
note, however, that our strings will by default show (and preserve)
*all* significant digits of the number being displayed as a string,
so that you will never lose any significant digits by using this program
(finish with NORM to recover the original unaltered real number).

A Casio will shift significant low-order digits right off the display,
which it can afford to do, since it keeps the original number
(and various display flags) stashed away in an internal storage register
while it independently modifies only the display.  Since we rely solely
on our converted string to preserve all the significant digits,
we will not shift any significant digits out of the string
(you can round the actual original number if you want to first
reduce it to fewer significant digits, allowing it to shift further).

Extreme and special cases:

1E499 ==> "10E498" ==> ".01E501" etc. ( NORM ==> 1E499 again )

1E-498 ==> "1E-498" ==> "1000E-501" etc. ( NORM ==> 1E-498 again )

The HP48 itself defaults 1E-499 to SCI display (rather than ENG),
so you do not get "multiple of 3" exponents in this extreme case.

Note that the very well thought out built-in handling by HP of
real number decompilation & compilation contributes greatly to
making this simple program work properly and completely.

Okay, all you Casio fans -- you asked for it, you got it!

Now, who's gonna send me a Toyota?

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