HP Forums
(32S) Floor Function - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (32S) Floor Function (/thread-8447.html)



(32S) Floor Function - Gerald H - 06-03-2017 07:47 AM

For real input the programme returns Floor of input without disturbing the stack.

Improvements most welcome.

Code:

0.    LBL “F”
1.    X>=0?
2.    GTO O
3.    STO F
4.    FP
5.    STO G
6.    ABS
7.    X≠0?
8.    RCL/ G
9.    RCL+ F
10.    LBL O
11.    IP
12.    RTN



RE: (32S) Floor Function - c785 - 06-29-2017 04:02 PM

I'm not familiar with the 32S, but on the 42S I just solved this myself using the code in the picture (it's labeled "80" because I made it part of a KEYG/MENU structure). It did take me a while though, I'm only just getting used to RPN and its quirks.

[attachment=4991]


RE: (32S) Floor Function - Werner - 06-30-2017 07:05 AM

Doesn't work for -0.999999999999 (-1 + 1e-12), or in general for
-9.9999999999x Eyyy, x=5..9, yyy>=-1
(both the 32S and 42S versions, BTW)

The easiest way without stack preservation is

Code:
>LBL "FLOOR"
  RCL ST X  (or ENTER X<>Y)
  1
  MOD
  -
  END

I don't have a 32S so I don't know what commands are available there.
For the 41/42S, one version with stack preservation is as follows:

Code:
>LBL"FLOOR"
 IP
 X<>Y
 X<> ST L
 X<Y?
 DSE ST Y        always skips
>LBL 00
 X<> ST L
 X<>Y
 END

Cheers, Werner


RE: (32S) Floor Function - c785 - 06-30-2017 07:23 AM

(06-30-2017 07:05 AM)Werner Wrote:  Doesn't work for -0.999999999999 (-1 + 1e-12), or in general for
Are you sure? I just tried, the 42S code works fine on Free42 and on the DM42.


RE: (32S) Floor Function - Werner - 06-30-2017 08:51 AM

I'm sure ;-)
Free42 and DM42 work with 34 digits, so there, take (-1 + 1e-34)

Werner


RE: (32S) Floor Function - c785 - 06-30-2017 11:31 AM

OK, you're right. "IP" after "+" isn't a good idea, so replace the sequence
Code:
+
IP
with
Code:
X<>Y
IP
+
and everything should be OK. Now even -1+10^-34 gives the right result on the DM42.


RE: (32S) Floor Function - c785 - 06-30-2017 02:58 PM

OK, I think I got the idea now. How about this:

Code:
> LBL "FLOOR"
  IP
  RCL- ST L
  X>0?
  DSE ST L
  CLX
  X<> ST L
  RTN



RE: (32S) Floor Function - Werner - 07-01-2017 10:16 AM

LastX is not correct ;-) In my version above, it is.
Werner


RE: (32S) Floor Function - c785 - 07-02-2017 09:29 AM

You're right once again. The only thing I'd do differently is the "nop" command -- a label there is quite misleading and can lead to trouble when the code is part of a larger program, so I prefer "X<>X" in its place.

In any case, RPN is great. What I could have done with RPL in about five minutes now took me several days to grasp. But I did learn a thing or two.


RE: (32S) Floor Function - Werner - 07-02-2017 10:18 AM

X<> ST X is two bytes, and I have a label there for the CEIL function to jump to ;-)

Code:
 { 38-Byte Prgm }
>LBL"CEIL"
 INT
 X<>Y
 X<> L
 X>Y?
 ISG Y        always skips
 X<=Y?        nop
 GTO 00
>LBL"FLOOR"
 INT
 X<>Y
 X<> L
 X<Y?
 DSE Y        always skips
>LBL 00        not a nop
 X<> L
 X<>Y
 END

Cheers, Werner