From: jhmeyers@miu.edu (John H Meyers)
Subject: Even %TILE is bugged!
Date: 03 Oct 1996 00:00:00 GMT
Message-ID: <530i58$cp7@news.iastate.edu>
organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
reply-to: jhmeyers@miu.edu
newsgroups: comp.sys.hp48


The HP "EXAMPLES" programs (from G/GX TEACH command), some of which
are repeated in AUR, are proving to be a disappointment.

I've just noticed that even the '%TILE' program (found in the EXAMPLES
directory created by TEACH, and again in AUR p. 2-15) is bugged!

Try: { 1 2 3 4 } 10 %TILE -> Error
     { 1 2 3 4 } 90 %TILE -> Error

Luckily enough, it always works for 50% (thus MEDIAN, using %TILE, is safe),
but that's in fact the *only* percentage value for which it always works!
Try { 1 } with any other percentage, for example.

Consider the list { 1 2 3 4 }; the %TILE program elects to consider
that the four values 1, 2, 3, 4 are the 20th, 40th, 60th, and 80th
percentile, respectively.  Any percentage between these values
averages two adjacent elements; any percentage below 20
or above 80 produces an error!

Here's the original HP program for %TILE:

\<< SWAP SORT DUP SIZE 1 + ROT 100 / * \-> p
    \<< DUP p FLOOR GET SWAP p CEIL GET + 2 / \>> \>>

Which of course could have been shortened to:

\<< SWAP SORT DUP SIZE 1 + ROT %
    DUP2 FLOOR GET ROT ROT CEIL GET + 2 / \>>

But to eliminate the above bugs it should be:

\<< SWAP SORT DUP SIZE 1 + ROT %
    DUP2 FLOOR 1 MAX GET ROT ROT CEIL OVER SIZE MIN GET + 2 / \>>

The above program now works for any percentage p where 0 < p < 100;
to bullet-proof against even p <= 0 and p >= 100, go further:

\<< SWAP SORT DUP SIZE 1 + ROT % DUP2 FLOOR 1 MAX OVER SIZE MIN GET
    ROT ROT CEIL 1 MAX OVER SIZE MIN GET + 2 / \>>

Despite all this, it's still shorter than the original!

Well, thus far we've fixed and replaced the still-slightly-buggy APLY
(was posted a while back); we've just fixed up MEDIAN, and now we're
fixing %TILE.  Don't worry -- PURGEX works fine (use it now!)

It has been my experience from HP34C onwards that "sample programs" in
HP manuals don't match the high quality that went into the calculators;
perhaps they are meant to make the "newbie" owner feel good :)

-----------------------------------------------------------
With best wishes from:  John H Meyers  ( jhmeyers@mum.edu )
