32sii: FLOOR function « Next Oldest | Next Newest »

 ▼ Dave Britten Member Posts: 247 Threads: 26 Joined: Oct 2007 01-20-2010, 10:27 AM 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? ▼ Bart (UK) Posting Freak Posts: 850 Threads: 10 Joined: Mar 2009 01-20-2010, 11:22 AM 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. ▼ Dave Britten Member Posts: 247 Threads: 26 Joined: Oct 2007 01-20-2010, 11:36 AM 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 ``` ▼ Bart (UK) Posting Freak Posts: 850 Threads: 10 Joined: Mar 2009 01-20-2010, 12:14 PM 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. ▼ Marcus von Cube, Germany Posting Freak Posts: 3,283 Threads: 104 Joined: Jul 2005 01-20-2010, 12:23 PM If you can live with a limited range (below 1E12) why not just add 9E11, IP and subtract again? ▼ Bart (UK) Posting Freak Posts: 850 Threads: 10 Joined: Mar 2009 01-21-2010, 04:37 AM From the further responses, there are some very good solutions (Katie coming up with some wonderful stuff as usual). Didier Lachieze Member Posts: 248 Threads: 5 Joined: Feb 2008 01-20-2010, 12:24 PM 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 ``` ▼ Glenn Shields Member Posts: 63 Threads: 8 Joined: Dec 2009 01-20-2010, 09:06 PM 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 ▼ Bart (UK) Posting Freak Posts: 850 Threads: 10 Joined: Mar 2009 01-21-2010, 04:54 AM 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! Egan Ford Posting Freak Posts: 1,619 Threads: 147 Joined: May 2006 01-21-2010, 02:47 AM 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. ▼ Didier Lachieze Member Posts: 248 Threads: 5 Joined: Feb 2008 01-21-2010, 04:50 AM 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 ``` ▼ Maarten Ambaum (Reading, UK) Junior Member Posts: 23 Threads: 2 Joined: Jan 2010 01-21-2010, 05:35 AM 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. ▼ Didier Lachieze Member Posts: 248 Threads: 5 Joined: Feb 2008 01-21-2010, 06:00 AM 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. hugh steers Senior Member Posts: 536 Threads: 56 Joined: Jul 2005 01-20-2010, 03:30 PM 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. ▼ Katie Wasserman Posting Freak Posts: 1,477 Threads: 71 Joined: Jan 2005 01-20-2010, 04:04 PM 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. Gerson W. Barbosa Posting Freak Posts: 2,761 Threads: 100 Joined: Jul 2005 01-24-2010, 07:17 AM 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. ▼ hugh steers Senior Member Posts: 536 Threads: 56 Joined: Jul 2005 01-24-2010, 09:04 PM 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. Katie Wasserman Posting Freak Posts: 1,477 Threads: 71 Joined: Jan 2005 01-20-2010, 04:02 PM What's wrong with: ```FIX 0 .5 - RND ALL (may not be needed) ``` -Katie ▼ Gerson W. Barbosa Posting Freak Posts: 2,761 Threads: 100 Joined: Jul 2005 01-20-2010, 09:26 PM 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. ▼ Egan Ford Posting Freak Posts: 1,619 Threads: 147 Joined: May 2006 01-21-2010, 03:32 AM 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. ▼ Dave Britten Member Posts: 247 Threads: 26 Joined: Oct 2007 01-21-2010, 07:59 AM 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. Gerson W. Barbosa Posting Freak Posts: 2,761 Threads: 100 Joined: Jul 2005 01-21-2010, 11:33 AM 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,576 12-10-2013, 06:49 PM Last Post: Han HP Prime: FLOOR, iPart , and their use in programs Alberto Candel 6 1,935 12-01-2013, 10:17 PM Last Post: Alberto Candel Mini challenge. CEIL / FLOOR in RPN M. Joury 47 8,481 10-31-2011, 10:11 AM Last Post: M. Joury RPN FLOOR and CEIL Werner 4 1,154 09-28-2011, 08:18 AM Last Post: Werner Dropped HP41CX (carpeted floor), no longer working jim540 29 5,056 01-08-2010, 08:43 AM Last Post: Mike Morrow Error function 35s vs. 32SII Thomas Radtke 8 1,842 08-01-2007, 12:37 PM Last Post: Les Wright 32Sii BROWN vs. 32Sii BLACK NH 8 1,784 10-10-2003, 04:44 AM Last Post: NH LED displays and your kitchen floor John Garza (3665) 2 823 11-15-2002, 01:18 PM Last Post: Victor x=4? Function Shawn 2 907 10-01-2002, 10:12 AM Last Post: Vieira, Luiz C. (Brazil) Implement 'AND' function with 'XOR' function. tal 4 1,068 12-13-2000, 05:55 AM Last Post: Steve (Australia)

Forum Jump: