RPN FLOOR and CEIL « Next Oldest | Next Newest »

 ▼ Werner Member Posts: 163 Threads: 7 Joined: Jul 2007 09-27-2011, 05:37 AM easy, short, no tests, valid for positive and negative arguments ```1.using only FRC CEIL(x) = x - FRC(FRC(x) - 1) FLOOR(x) = x - FRC(FRC(x) + 1) (written 41C style, but easily portable) *LBL"CEIL" ENTER FRC 1 - FRC - RTN *LBL"FLOOR" ENTER FRC 1 + FRC - RTN 2.using MOD: (1 byte shorter for FLOOR..) *LBL"CEIL" RCL X (or ENTER ENTER) -1 MOD - RTN *LBL"FLOOR" RCL X 1 MOD - RTN ``` Cheers, Werner ▼ Dieter Senior Member Posts: 653 Threads: 26 Joined: Aug 2010 09-27-2011, 08:28 AM Yes, as mentioned earlier, MOD can provide an elegant solution here. I especially like the idea with this double FRC since it can be used on virtually all RPN machines, but there is one caveat: There is a numeric pitfall for arguments close to zero. For values so small that numerically 1 ± x = 1 the method will simply return x. For instance, x = 1E-10 will return the correct result (1) on a 12-digit machine, but a 10-digit machine will return 1E-10. Otherwise your idea works well. Dieter ▼ Werner Member Posts: 163 Threads: 7 Joined: Jul 2007 09-27-2011, 09:28 AM *smack on the head* You're absolutely right of course. Back to square one. Werner ▼ Dieter Senior Member Posts: 653 Threads: 26 Joined: Aug 2010 09-28-2011, 07:58 AM The FRC method is fine for the programming contest since for r =< 5000 there will be no fractional part less than frac(sqrt(4901)) = 0,00714. ;-) For the record, here's another way to implement a CEIL resp. FLOOR function. It requires the same 7 bytes as yours, works for any argument and uses just two stack registers. However, SIGN is required. Or any other function that turns 0 < x < 1 into something between 1 and (less than) 2. ``` INT LastX FRC X>0? ; use X>0? for CEIL or X<0? for FLOOR SIGN INT + ``` BTW - the test before the SIGN function is not required because of the special way the HP-41 evaluates this function. ;-) Dieter ▼ Werner Member Posts: 163 Threads: 7 Joined: Jul 2007 09-28-2011, 08:18 AM yes, that's what I used in my 41C program (found in another part of the forum - and lacking the second INT which is not needed for positive args.) Some more CEIL stuff: ```perfect function (i.e. LASTX contains X, no stack level used) *LBL"CEIL" INT RUP X<> L X>Y? ISG Y LBL 00 (NOP) X<> L RDN END and if we're not so picky: *LBL"CEIL" ENTER INT X

 Possibly Related Threads... Thread Author Replies Views Last Post HP Prime: FLOOR, iPart , and their use in programs Alberto Candel 6 917 12-01-2013, 10:17 PM Last Post: Alberto Candel [PRIME] RPN: another attempt at returning more than one value to the RPN stack Marcus von Cube, Germany 5 857 11-05-2013, 02:44 AM Last Post: Marcus von Cube, Germany Mini challenge. CEIL / FLOOR in RPN M. Joury 47 3,382 10-31-2011, 10:11 AM Last Post: M. Joury 32sii: FLOOR function Dave Britten 21 1,469 01-21-2010, 11:33 AM Last Post: Gerson W. Barbosa Dropped HP41CX (carpeted floor), no longer working jim540 29 2,121 01-08-2010, 08:43 AM Last Post: Mike Morrow New Iphone & Itouch calculator - Access RPN and Active RPN Nigel Bamber 1 365 06-10-2009, 04:13 PM Last Post: Jean-Michel Another calculator with RPN (Corvus 500 RPN) Saile (Brazil) 7 760 03-31-2009, 12:14 AM Last Post: Michael de Estrada LED displays and your kitchen floor John Garza (3665) 2 371 11-15-2002, 01:18 PM Last Post: Victor

Forum Jump: