From: eldorado@en.ecn.purdue.edu (Dave Jansen)
Newsgroups: comp.sys.hp48
Subject: bv.br.ul
Date: 13 Feb 92 23:04:51 GMT
Organization: Purdue University Engineering Computer Network

The routines BV and BR are something I came across in my APL
programming. The best way to explain them is with an example.

How long is 123456789 seconds ?

Do:

 123456789 <enter>
 { 24 60 60 } <enter>
 BR <enter>
 { 1428 21 33 9 )

NOTE: you got a list of FOUR numbers out when you gave it a list of
THREE weights! All the 'overflow' got lumped into that fourth number. 

So that 123456789 seconds is: 

 1,428 days; 21 hoursl; 33 minutes and 9 seconds.


How many seconds is 08:23:45 ?

Do:

 { 8 23 45 } <enter>
 ( 24 60 60 } <enter>
 BV <enter>
 30255

What is 12345 in 16 bit binary?

Do:

 12345 <enter>
 { 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 } <enter>
 BR <enter>
 { 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 1 }

And so on. I find for some things they're quite useful.

Checksums were calculated using the standard HP28 checksum program. 
The checksums are:

        BV      E76C
        BR      D274
        UL      BBF3
        ADD     17D9

Here's a modification of a program I posted awhile back.  The original
posting consisted of two programs called BR (base representation) and BV
(base value).  With BR you could answer questions like: How long is
12345 seconds? By doing: 12345 <enter> { 365 24 60 60 } <enter> BR
<enter> and it would return: { 0 3 25 45 } (12345 seconds is 3 hours 25
minutes and 45 seconds). 

BV would do the reverse: { 0 3 25 45 } <enter> { 365 24 60 60 } <enter>
BV <enter> would return: 12345. 

This was all working quite well, but you had to remember what numbers to
put into the list of base weights.  { 365 24 60 60 } means that there
are 60 seconds to a minute, 60 minutes to an hour and 24 hours to a day
and finally 365 days to a year. 

With the units capability of the HP28S there had to be a better way. 
After a bit of tinkering I came up with: UL, the unit list program. 

The above example in UL would be: 12345 <enter> { d h min s } <enter> UL
<enter> and it would return { 0 3 25 45 }.  To reverse this calculation
with UL you would do: { 0 3 25 34 } <enter> { d h min s } <enter> UL
<enter> and UL returns 12345. 

UL determines which conversion it's doing by checking the type of the
arguments.  They're either both lists or a number and a list. 

One of the nice things about this is that it works nicely with user
defined units!

One thing that I'm not sure about is the use of the IP instructions in
the above code [UL].  This appears to work provided the units being used are
always related to each other in integral steps.  Things will work with
units {a b c} provided 'a' is an integral number of 'b' and 'b' is an
integral number of 'c'.  In most instances this appears to be true, but
is it a problem?

If it is a problem I can't think of an easy way around it.  Any ideas?

One interesting thing I'm using UL for now is the ability to add unit
lists.  With an ADD program like:
you can add miles/feet/inches.  How long is 12 miles, 1234 feet and 8
inches plus 8 miles, 3456 feet and 1 inch?

    {12 1234 8} <enter> {8 3456 1) <enter> {mi ft in} <enter> UL <enter>

and you get the answer: {20 4690 9) (20 miles, 4690 feet and 9 inches). 
The only messy part that I've come across is how to add a sign to a unit
list.  Any ideas?

Once again, if you have any questions just yell.....

-- 
john.Latala@Waterloo.NCR.COM                                   NCR Canada, Ltd
                                                             Waterloo, Ontario
                                                                Canada N2J 4G5
                                                           (519) 884-1710 x454
Cut Here 8<--------------------------------------------------
%%HP: T(3)A(D)F(.);
DIR
@
@ BV - Base value
@
@ Takes two arguments:
@
@  l - a list of numbers 
@  b - a list of wieghts or bases
@
@ Returns:
@
@  n - encoded value
@
@ Calculates:
@
@  ((((l1 * b2) + l2) * b3 + l3) ... ) * bn + ln
@
@ Example:
@
@  How many seconds is 08:23:45 ?
@
@  { 8 23 45 } ( 24 60 60 } BV <enter>
@  30255
@
@------------------------------------------------------------
BV
\<< 
  \-> l b
  \<<
    IF l SIZE b SIZE \=/ THEN
      "lists not same length" 33 CHR + 1 DISP l b
    ELSE
      b 2 b SIZE SUB  @ drop the first element of b
      1 + 'b' STO @ add a 1 element to the end
      0   @ our running total
      1 b SIZE  @ calculate loop iteration
      FOR i
        l i GET  @ get the i'th number
 +  @ add it to the running total
 b i GET  @ get the i'th weight
 *
      NEXT
    END
  \>>
\>>

@ BR - Base representation
@
@ Takes two arguments:
@
@  n - a list of numbers 
@  b - a list of wieghts or bases
@
@ Returns:
@
@  a list representing the encoding of the given number
@  using those weights. Note that the returned list may be
@  one longer than the given list of weights. This is
@  because there was some remainder left when the
@  calculation was completed.
@
@  NOTE: Either list can have real numbers or integers in
@   it, but the returned list is always of real
@   numbers.
@
@ Example:
@
@  How long is 123456789 seconds ?
@
@  123456789 <enter>
@  { 24 60 60 } <enter>
@
@  { 1428 21 33 9 )
@
@  ( 1,428 days; 21 hoursl; 33 minutes and 9 seconds. )
@
@------------------------------------------------------------
BR
\<< 
  { } 
  \-> n b l
  \<<
    WHILE b SIZE 0 \=/ REPEAT @ for each element in the base list
      n    @ get the current number
      b b SIZE GET  @ get the last weight
      DUP2
      IF DUP2 TYPE 10 == SWAP TYPE 10 == OR THEN @ is one an integer?
        DUP2 / * - B\->R  @ integer MOD function; return a real
      ELSE
        MOD   @ real MOD function
      END
      l + 'l' STO  @ add it to the head of the list
      /    @ reduce the number
      IF DUP TYPE 10 \=/ THEN @ get rid of the fraction part
        IP
      END         
      'n' STO   @ put the reduced number back
      b 1 b SIZE 1 - SUB         @ drop the last element from the base list
      'b' STO   @ and put it back
    END
    IF n TYPE 10 == THEN         @ end of list. any of the number left?
      IF n #0 \=/ THEN
        n B\->R l +
      ELSE
        l
      END
    ELSE
      IF n 0 \=/ THEN
        n l +
      ELSE
        l
      END
    END
  \>>
\>>

UL
\<<
  \-> a1 a2
  \<<
    IF a1 TYPE {} TYPE == THEN
      0 
      a2 DUP SIZE GET
      \-> tu
      \<<
        1 a1 SIZE FOR i
          a1 i GET a2 i GET tu CONVERT DROP +
        NEXT
      \>>
    ELSE
      a2 DUP SIZE GET 
      \-> bu
      \<<
        1 a2 SIZE 1 - FOR i
          a1 a2 i GET
          \-> tu
          \<<
            bu tu CONVERT DROP IP DUP 
            tu bu CONVERT DROP IP a1 SWAP - 'a1' STO
          \>>
        NEXT
      \>>
      a1 a2 SIZE \->LIST
    END
  \>>
\>>

ADD
\<<
  \-> n1 n2 ul
  \<<
    n1 ul UL
    n2 ul UL
    +
    ul UL
  \>>
\>>
END

-- 
I am a self-made man who worships his creator.
________________________________________________________________________________
David Jansen            |  INTERNET: eldorado@en.ecn.purdue.edu
Electrical Engineering  |  BITNET:   eldorado%ea.ecn.purdue.edu@purccvm

