32sii: FLOOR function



#15

I recently had a need for a FLOOR function on my 32sii (i.e. largest integer less than or equal, or "round down"). This is what I came up with, but I'm curious if anybody has something more efficient.

LBL F
SF 4
ENTER
FP
x=0?
CF 4
CLx
x<=y?
CF 4
FS? 4
COS
-
IP
CF 4
RTN

This one checks to see if X has a fractional part, and if X is less than zero. If both conditions are met, it uses COS to transform the 0 to a 1 before subtracting, thus correcting for IP rounding in the wrong direction for negative numbers. Can it be done in fewer steps/bytes?


#16

Here's my attempt:

F001 LBL F
F002 ENTER
F003 FP
F004 x<>0?
F005 x<0?
F006 CLx
F007 x>y?
F008 COS
F009 +/-
F010 +
F011 IP
F012 RTN
Done on the 35s, should work if the 32sii has x<>0 (x not equal to 0).


Edited: 20 Jan 2010, 11:26 a.m.


#17

Looks like it has the same issue that was present in my first version I did last night: the program will still subtract 1 if the input is a negative integer. e.g. -6 -> -7

Mine was a lot shorter before I had to add all that flag nonsense to fix that. :) My broken version, just for reference:

LBL F
0
x>y?
COS
-
IP
RTN

#18

Oh, yeah, that was what line 3/4 was supposed to handle, then I had to fix something else (6/7 i think) and it all went to pot. Back to the "drawing board" then.

Edited: 20 Jan 2010, 12:21 p.m.


#19

If you can live with a limited range (below 1E12) why not just add 9E11, IP and subtract again?


#20

From the further responses, there are some very good solutions (Katie coming up with some wonderful stuff as usual).

#21

This one seems to work on my 35s:

F001 LBL F
F002 ENTER
F003 FP
F004 0
F005 x>y?
F006 COS
F007 +/-
F008 REGZ
F009 +
F010 IP
F011 RTN

#22

What an elegant solution. After seeing the responses to the 2*2 bug, the keyboard layout, and all the other known quirks of the
35s, this thread shows the continued need for and enjoyment of
RPN keystroke programming.
Sure the 50g has this function built-in, and has i/o for all the
fantastic RPL programming one can create and use, but it is still
lots of fun to create and debug an RPN program without worrying about having enough ENDs and << >> in all the right places!
And the 35s has the new register capabilities and auto goto line
renumbering, which adds to the appeal (even with all the bugs).
I am sorry that programs can't be saved, because as soon as I got mine, I wrote two programs, one being the "mastermind" game of guessing four numbers, and the other the 15-puzzle test of solvability, both using indirect addressing- a lot of fun.
And BTW, did anyone notice that the data-packing program secretly uses the "0" register?
That's enough to keep the rate of gray cell decay to a smaller number than if we never had this buggy machine. Cheers, Glenn


#23

I find the 35s a very nice calculator to use despite the bugs. For instance, I like the complex input, it's just a pity there's no easy way to do P<>R conversion and extracting RE and IM parts (but program routines can easily be used for this). We all agree on the need for an external comms capability (I have a number of programs too that could do with saving/restoring (~15k mem used - I'm sure there are many on this forum with more)). Although I ahve the listings for them all, I'd hate to type them in again (it took me 2 days to type in Egan's game (of 700+ lines/~3k IIRC)) - and with the buggy lenth/checksums, mistakes would easily creep in!

#24

AFAIK there is no way to recall a stack register on the 32sii. At least I could not find it in the manual. Here is a modified version that should work on the 32sii.

F001 LBL F
F002 IP
F003 LASTx
F004 FP
F005 0
F006 x>y?
F007 COS
F008 x<>y
F009 Rv
F010 -
F011 RTN
Here's another without COS:
F001 LBL F
F002 IP
F003 LASTx
F004 FP
F005 0
F006 x>y?
F007 y^x
F008 x=0?
F009 *
F010 -
F011 RTN

Edited: 21 Jan 2010, 3:10 a.m.


#25

Nice ones. Here is another version for the 32sii:

F001 LBL F
F002 IP
F003 LASTx
F004 FP
F005 1
F006 +
F007 IP
F008 1
F009 -
F010 +
F009 RTN

And a shorter version working on the 35s but not on 32sii as it doesn't support the SIGN function:

F001 LBL F
F002 IP
F003 LASTx
F004 FP
F005 SGN
F006 x>0?
F007 CLx
F008 +
F009 RTN

#26

Didier (et al.),
Some nice solutions here. I assume you are aware of the INTG function on the 35s, which, as far as I can see, is precisely the floor function. I agree it is not as much fun as doing it yourself.


#27

Quote:
I agree it is not as much fun as doing it yourself.


This is the point, it's fun to do some RPN programming! and as the topic is related to the 32sii I was using my 35s to try to find solutions that could apply to the 32sii which doesn't have a built-in FLOOR and which I don't have.
The latest program from Egan in 8 steps is clearly the best one so far.
#28

Yes indeed. can we have floor added to the 30b too!!

the usual offerings are IP and FP. IP (integer part) truncates to zero, so IP(-2.9) = -2, IP(-2.1) = -2, IP(2.1) = 2, IP(2) = 2, IP(-2) = -2.

fine, but also floor is useful, so in the above we have FLOOR(-2.9) = -3, FLOOR(-2.1) = -3, FLOOR(2.1) = 2, FLOOR(2) = 2,FLOOR(-2) = -2 etc.

this came up again because recently i needed ceil(x) (ceiling), which is conveniently defined by ceil(x) = -floor(-x).

so ceil and floor are not both needed, but it would be nice to have one of them and also the existing IP, otherwise a test and branch are required.


#29

What I just posted on this thread will work on the 30b too and you can save the display state. Here's some not so pretty code demonstrating that:

STO 8
RCL Mode
STO 9
3
0
0
STO Mode
RCL 8
.
5
-
RND
RCL 9
STO Mode
ANS
Stop

-Katie

Edited: 20 Jan 2010, 4:13 p.m.

#30

Quote:
so ceil and floor are not both needed, but it would be nice to have one of them and also the existing IP, otherwise a test and branch are required.

In order to avoid any test, we can define floor(x) as

floor(x) = IP(x) + IP(FP(x) + 1) - 1

The corresponding RPN program will be

F01 LBL F
F02 IP
F03 LASTx
F04 FP
F05 1
F06 +
F07 IP
F08 1
F09 -
F10 +
F11 RTN

Not the shortest one though.


#31

i like it. i didnt think it was expressible in terms of IP & FP, but you're right.

I think a test is always a disaster because the jump causes a delay.

#32

What's wrong with:

FIX 0
.5
-
RND
ALL (may not be needed)


-Katie


#33

Quote:
What's wrong with:

FIX 0
.5
-
RND
ALL (may not be needed)

-Katie


0 -> -1

-1 -> -2 . . .

A01 LBL A
A02 ENTER
A03 IP
A04 x<=y?
A05 GTO B
A06 1
A07 -
B01 LBL B
B02 x<>y
B03 Rv
B04 RTN

This works, but takes up an extra label (not a problem on the 32S-II as one would run out of memory long before running out of labels...)

Gerson.


#34

Same without 2nd label:

F01 LBL F
F02 ENTER
F03 IP
F04 x<=y?
F05 RTN
F06 1
F07 -
F08 RTN
Y will contain original arg. Could cause a problem if used in a program.

#35

Very nice. Leaving the original argument in Y allows adding one extra label, which gives you a version of the program that behaves like a built-in command, i.e. no trashing T, and LASTx works as expected:

LBL R
RUp
STO i
RDown
XEQ F
x<>y
SIN
CLx
RCL i
RDown
RTN

Then you can have a "clean" version for interactive use, and the smaller, faster version for program loops.

#36

Here is a 32S-II 9-step program that preserves X and Y, but does not preserve L:

F01 LBL F
F02 IP
F03 LASTx
F04 FP
F05 x<0?
F06 COSH
F07 IP
F08 -
F09 RTN


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP50g: Writing a function that returns a function Chris de Castro 2 1,497 12-10-2013, 06:49 PM
Last Post: Han
  HP Prime: FLOOR, iPart , and their use in programs Alberto Candel 6 1,874 12-01-2013, 10:17 PM
Last Post: Alberto Candel
  Mini challenge. CEIL / FLOOR in RPN M. Joury 47 8,333 10-31-2011, 10:11 AM
Last Post: M. Joury
  RPN FLOOR and CEIL Werner 4 1,134 09-28-2011, 08:18 AM
Last Post: Werner
  Dropped HP41CX (carpeted floor), no longer working jim540 29 4,965 01-08-2010, 08:43 AM
Last Post: Mike Morrow
  Error function 35s vs. 32SII Thomas Radtke 8 1,790 08-01-2007, 12:37 PM
Last Post: Les Wright
  32Sii BROWN vs. 32Sii BLACK NH 8 1,727 10-10-2003, 04:44 AM
Last Post: NH
  LED displays and your kitchen floor John Garza (3665) 2 800 11-15-2002, 01:18 PM
Last Post: Victor
  x=4? Function Shawn 2 881 10-01-2002, 10:12 AM
Last Post: Vieira, Luiz C. (Brazil)
  Implement 'AND' function with 'XOR' function. tal 4 1,043 12-13-2000, 05:55 AM
Last Post: Steve (Australia)

Forum Jump: