![]() |
Mini challenge. CEIL / FLOOR in RPN - 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: Mini challenge. CEIL / FLOOR in RPN (/thread-203283.html) |
Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-29-2011 Hi All, Recently I needed a FLOOR function on my HP41. Easy enough, but I decided to generalize the solution. I wanted FLOOR and CEILING functions that did not effect the stack (Y, Z, and T are preserved) and that did not use any registers. LASTX does not have to be correct. My solution uses 44 bytes on an HP41 including 19 bytes for the LBL FLOOR, LBL CEIL and the END instructions. My solution does not update LASTX correctly but it could if I were willing to use more memory. I use no SP tricks but they are not disallowed. Off the top of my head I can't see how SP would help but I am going to look into it in more detail to see if I am missing something. I worked on this for longer than I expected when I started the exercise and would love to see other solutions. I just can't shake the feeling that I am missing something obvious--some trick that will shave the code to half the size and twice the speed. At one point I thought that I had a very elegant truly tiny solution but it did not work for certain cases. I'll share later. Cheers,
-Marwan
Re: Mini challenge. CEIL / FLOOR in RPN - Allen - 10-29-2011 How do you define FLOOR and CEIL for negative numbers?
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-29-2011 FLOOR(-2.3) = -3.0 Therein lies the trick because otherwise (for a positive number) FLOOR is just INT or IP. INTG on the 33S or 35S is a FLOOR function but as far as I can tell there is no CEILING on those machines. Cheers, -Marwan
Edited: 29 Oct 2011, 8:10 p.m.
Re: Mini challenge. CEIL / FLOOR in RPN - PeterP - 10-29-2011 can we use alpha?
Re: Mini challenge. CEIL / FLOOR in RPN - Egan Ford - 10-29-2011 41 RPN: 1 LBL FLOORQ&D. CEIL shouldn't be much different. Correct me if wrong, but LastX preserved.
Edited: 29 Oct 2011, 9:05 p.m. after one or more responses were posted
Re: Mini challenge. CEIL / FLOOR in RPN - Paul Dale - 10-29-2011 Extending Egan's code using CEIL(x) = -FLOOR(-x):
1 LBL CEIL Is this 38 bytes all up? - Pauli
Edit: merged in Egan's improvements Edited: 29 Oct 2011, 9:12 p.m. after one or more responses were posted
Re: Mini challenge. CEIL / FLOOR in RPN - Egan Ford - 10-29-2011 Doh! Concurrent updates. I changed it, shorter now.
Re: Mini challenge. CEIL / FLOOR in RPN - Paul Dale - 10-29-2011 Easily fixed :-)
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-29-2011 No.
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-29-2011 Don't think that this is right. For FLOOR(-2.3) it returns -2. FLOOR(-2.3) should be -3.00.
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-29-2011 -2.3 return -2.0 not -3.0. INT would give you the same results. Solution not working correctly for negatives. Also, your solution to CEIL is exactly what I used in my solution. That is why LASTX does not work for CEIL in my solution. Cheers,
-Marwan Edited: 29 Oct 2011, 10:35 p.m. after one or more responses were posted
Re: Mini challenge. CEIL / FLOOR in RPN - Paul Dale - 10-29-2011 Blame Egan for the floor -- I just copied his code :-) I checked the one byte longer version and I'm pretty sure floor(-2.3) was correct then.
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-29-2011 I never got to see the 1 byte longer version. I count 39 bytes in the current version (?). If the original version was 1 byte longer and works it is 4 bytes shorter than mine (I think). Cheers,
-Marwan Edited: 29 Oct 2011, 10:41 p.m.
Re: Mini challenge. CEIL / FLOOR in RPN - Marcus von Cube, Germany - 10-30-2011 FLOOR(x) = -CEIL(-x) and vice versa. This should help on the 33/35S.
Re: Mini challenge. CEIL / FLOOR in RPN - Werner - 10-30-2011 43 bytes, perfect function (YZT preserved, LASTX contains last X): 01*LBL"CEIL"it won't win the elegance prize, though ;-) Cheers, Werner Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 Nice. Actually I think it is pretty elegant--a single test, a single exit point (RTN), shorter, and you don't use a flag. Better than mine. But see Pauli's and Marcus's solution and comments above. If you are willing to give up LASTX (as I was) you can use:
LBL CEIL This is what I did in my original solution and why I said that LAST X is correct for FLOOR but not for CEIL. It shortens the code considerably. Cheers, -Marwan
Edited: 30 Oct 2011, 8:47 a.m.
Re: Mini challenge. CEIL / FLOOR in RPN - Dieter - 10-30-2011 This will not work. First, the inital test makes no sense. It uses a well-known technique from the good old days: use two consecutive tests, and you will get a "composite test" for (NOT condition1) OR condition2. So x<0? followed by x<>0? logically equals "X>=0 OR X<>0?", or in other words: "is x greater than zero, or equal to zero, or not equal to zero". Which is the case for any number. So the program will always stop after this test, simply returning INT(x). #-) There also is not much sense in the sequence "X<>0? ST-L" as the test for non-zero X is obsolete: if X actually is zero subtracting it from L will not change anything.
Here's my (also Q&D) first idea for the '41: 01 LBL"FLOOR"It even sets LastX correctly. But it will not work for negative integers, so I'll keep trying. ;-) Dieter
Edited: 30 Oct 2011, 10:12 a.m.
Re: Mini challenge. CEIL / FLOOR in RPN - Dieter - 10-30-2011 *clap* *clap*
Dieter
Re: Mini challenge. CEIL / FLOOR in RPN - Namir - 10-30-2011 I regard the STO X command is the best NOP for the HP-41C and HP-42s.
Namir
Re: Mini challenge. CEIL / FLOOR in RPN - Dieter - 10-30-2011 But STO X requires two bytes, while LBL 00 only takes one. ;-)
Dieter
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 The Zenrom module offers a NOP instruction that is basically a text byte (I think) but I am not sure of the length. Also X <> X works well but is also two bytes as is STO X. Cheers,
-Marwan
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 Here is my original solution. But as noted elsewhere Werner's is better (which was partially what drove the challenge--find a better solution).
LBL CEIL 44 bytes total preserves Y, Z, and T. Preserves LASTX for FLOOR but not for CEIL. I don't see any way to do this without using a DSE instruction so I would love to see Egan's original solution it works. The later solution does not. Cheers,
-Marwan Edited: 30 Oct 2011, 11:06 a.m.
Re: Mini challenge. CEIL / FLOOR in RPN - Gerson W. Barbosa - 10-30-2011 Quote:LBL CEIL I found this relationship yesterday, at Wikipedia:
FLOOR(x) can be defined in terms of INT(x) and FRC(x), but I don't see this being mentioned there:
FLOOR(x) = INT(x) + INT(FRC(x) + 1) - 1 On the HP-15C, the following avoids any test, but preserves Y only. Perhaps it can be optimized on the HP-41.
001- f LBL C ; CEIL Regards, Gerson.
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 Hi Gerson, I found the same relationship. The fly in the ointment in my original challenge, so to speak, was the requirement that the stack be preserved. I am trying to think of ways to optimize your solution on the 41 to meet that requirement. Cheers,
-Marwan
Re: Mini challenge. CEIL / FLOOR in RPN - Gerson W. Barbosa - 10-30-2011 Quote: Yes, I am aware of it. That's not an easy task, congratulations for your and other's accomplishments! Cheers, Gerson.
Edited: 30 Oct 2011, 12:55 p.m.
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 Thanks. I have to say that it took me longer than Werner to come up with a less elegant solution <g>. Cheers,
-Marwan Edited: 30 Oct 2011, 1:24 p.m.
Re: Mini challenge. CEIL / FLOOR in RPN - Werner - 10-30-2011 Hi Marwan,
cheers, Werner
Re: Mini challenge. CEIL / FLOOR in RPN - Werner - 10-30-2011
Equivalent formulas: CEIL(x) = x - FRC(FRC(x) - 1)However, as Dieter pointed out in a previous post a few weeks ago, these do not work for very small x. eg for x = -1e-50 your FLOOR function will return the wrong result
Cheers, Werner
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 You know, you guys are all too damn honest! :) Cheers,
-Marwan
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 Well, I continued to think about this while out cycling today and I think I have a 37 byte solution. There are a couple of caveats:
1. LASTX is not preserved and Here is my latest effort:
LBL FLOOR Cheers, -Marwan EDIT: Fixes issue with longer fraction (ss.eeeii) as pointed out by Werner but adds 2 bytes. 39 bytes now.
Edited: 31 Oct 2011, 7:49 a.m. after one or more responses were posted
28 bytes with alpha lables - Allen - 10-30-2011 Here's a 28 byte version, including 17 bytes for the alpha labels:
FLOOR(x)="the greatest integer less than or equal to x." For CIEL, we increment 1 for negative input, for FLOOR we decrement 1 for positive input, then return the IP of the result in all 4 cases (pos,neg input and CIEL,FLOOR selection).
00 { 28-Byte Prgm }
Note: Does not account for integer inputs when ciel(x>0) or floor(x<0). Edited: 30 Oct 2011, 11:29 p.m. after one or more responses were posted
Re: 28 bytes with alpha lables - M. Joury - 10-30-2011 I came up with this solution very early on in my efforts but it doesn't work. CEIL(2.0) is 2.0 not 3.0 as returned by this algorithm. Similarly FLOOR(-2.0) should return -2.0 and not -3.0. Cheers, -Marwan
Edited: 30 Oct 2011, 11:33 p.m. after one or more responses were posted
Re: 28 bytes with alpha lables - Allen - 10-30-2011 yep, just noted that in the comments.
Re: 28 bytes with alpha lables - M. Joury - 10-30-2011 Yes. So does not work as a generalized solution.
Re: 28 bytes with alpha lables - M. Joury - 10-30-2011 I like the trick you use to save a byte by:
ISG X Saves a byte over:
ISG X Very clever. Can be used to save 3 bytes in Werner's solution (which is, IMHO, the best so far). Cheers,
-Marwan Edited: 30 Oct 2011, 11:52 p.m.
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-30-2011 Hi Werner, Did you see Alan's solution below? It does not work for integer values but he uses a trick that can shorten your version by 3 bytes. Instead of:
ISG Y You can use:
ISG Y A savings of 3 bytes. Cheers,
-Marwan
Re: 28 bytes with alpha lables - Allen - 10-31-2011 Ok, last try for the night. Does NOT use synthetic programming or flags. It works by hard-coding a 3-variable K-map for POS/NEG, INT/Non-INT, inputs and CIEL/FLOOR function. (8 cases)
1) If NEG CEIL then return IP(X) (2 cases)
00 { 43-Byte Prgm }
Edited: 31 Oct 2011, 2:04 a.m.
Re: 28 bytes with alpha lables - Marcus von Cube, Germany - 10-31-2011 Allen, all your solutions have something 'heavenly' ;-)
Re: 28 bytes with alpha lables - Werner - 10-31-2011 But that doesn't work.. ISG and DSE work on numbers in sss.eeeii format,
Cheers, Werner
Re: Mini challenge. CEIL / FLOOR in RPN - Werner - 10-31-2011 Only two bytes ;-)
Werner
Re: Mini challenge. CEIL / FLOOR in RPN - Werner - 10-31-2011 Perhaps we can use a 'test' suite to guarantee that a program returns the correct solution in all cases.
So, how about (just for CEIL): CEIL(0) : 0That will do, I think ;-) Cheers, Werner
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-31-2011 Right, sorry, GTO 00 is 2 bytes not 1 byte. Cheers,
-Marwan
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-31-2011 Good idea. There are so many cases where true decrement / increment operators would have been useful. Cheers,
-Marwan
Re: Mini challenge. CEIL / FLOOR in RPN - Paul Dale - 10-31-2011 That's why the 34S has them :-) I was also tempted to do the equivalent of ISG and DSE but without the skips.
Re: 28 bytes with alpha lables - M. Joury - 10-31-2011 This looks like a 42S solution. I count 44 bytes for a 41. Also see Werner's comment below. That got me as well in my second solution (now fixed but 2 bytes longer). Cheers,
-Marwan
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-31-2011 It always seemed to me that they should have been able to squeeze them into the 41.
Re: 28 bytes with alpha lables - Allen - 10-31-2011 Wow- you're right, I get so used to using ISG/DSE on integers and forget there is a seldom-used increment value on the right!
Re: Mini challenge. CEIL / FLOOR in RPN - M. Joury - 10-31-2011 Following is a test driver based on Werner's test scenarios. It is not small but it does test all the suggested cases automatically. Since I currently have a number of FLOOR solutions in memory I have set it up so that the user enters the name of the CEIL and FLOOR functions. Run it by "XEQ FLTST" or if from within the program area "XEQ A" or simply A in user mode and then follow the prompts. You will be asked first for the CEIL FN name and then the FLOOR FN name. The calculator is already in ALPHA mode so enter the name and press R/S. Total size: 270 bytes.
LBL FLTST I hope this helps a little with testing these routines. Cheers,
-Marwan
|