32sii: FLOOR function - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: 32sii: FLOOR function (/thread-162185.html) 32sii: FLOOR function - Dave Britten - 01-20-2010 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? Re: 32sii: FLOOR function - Bart (UK) - 01-20-2010 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. Re: 32sii: FLOOR function - Dave Britten - 01-20-2010 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 ``` Re: 32sii: FLOOR function - Bart (UK) - 01-20-2010 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. Re: 32sii: FLOOR function - Marcus von Cube, Germany - 01-20-2010 If you can live with a limited range (below 1E12) why not just add 9E11, IP and subtract again? Re: 32sii: FLOOR function - Didier Lachieze - 01-20-2010 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 ``` Re: 32sii: FLOOR function + 30b request - hugh steers - 01-20-2010 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. Re: 32sii: FLOOR function - Katie Wasserman - 01-20-2010 What's wrong with: ```FIX 0 .5 - RND ALL (may not be needed) ``` -Katie Re: 32sii: FLOOR function + 30b request - Katie Wasserman - 01-20-2010 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. Re: 32sii: FLOOR function - Glenn Shields - 01-20-2010 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 Re: 32sii: FLOOR function - Gerson W. Barbosa - 01-20-2010 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. Re: 32sii: FLOOR function - Egan Ford - 01-21-2010 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. Re: 32sii: FLOOR function - Egan Ford - 01-21-2010 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. Re: 32sii: FLOOR function - Bart (UK) - 01-21-2010 From the further responses, there are some very good solutions (Katie coming up with some wonderful stuff as usual). Re: 32sii: FLOOR function - Didier Lachieze - 01-21-2010 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 ``` Re: 32sii: FLOOR function - Bart (UK) - 01-21-2010 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! Re: 32sii: FLOOR function - Maarten Ambaum (Reading, UK) - 01-21-2010 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. Re: 32sii: FLOOR function - Didier Lachieze - 01-21-2010 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. Re: 32sii: FLOOR function - Dave Britten - 01-21-2010 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. Re: 32sii: FLOOR function - Gerson W. Barbosa - 01-21-2010 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 ``` Re: 32sii: FLOOR function + 30b request - Gerson W. Barbosa - 01-24-2010 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. Re: 32sii: FLOOR function + 30b request - hugh steers - 01-24-2010 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.