RPN FLOOR and CEIL



#6

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


#7

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


#8

*smack on the head*

You're absolutely right of course. Back to square one.

Werner


#9

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


#10

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<Y?
ISG X
END

if you put the code inline, you need a NOP after ISG X of course.
(I tend to use LBL 00)
BTW these work for all arguments.

Cheers, Werner


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP Prime: FLOOR, iPart , and their use in programs Alberto Candel 6 693 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 645 11-05-2013, 02:44 AM
Last Post: Marcus von Cube, Germany
  Mini challenge. CEIL / FLOOR in RPN M. Joury 47 2,730 10-31-2011, 10:11 AM
Last Post: M. Joury
  32sii: FLOOR function Dave Britten 21 1,161 01-21-2010, 11:33 AM
Last Post: Gerson W. Barbosa
  Dropped HP41CX (carpeted floor), no longer working jim540 29 1,620 01-08-2010, 08:43 AM
Last Post: Mike Morrow
  New Iphone & Itouch calculator - Access RPN and Active RPN Nigel Bamber 1 266 06-10-2009, 04:13 PM
Last Post: Jean-Michel
  Another calculator with RPN (Corvus 500 RPN) Saile (Brazil) 7 554 03-31-2009, 12:14 AM
Last Post: Michael de Estrada
  LED displays and your kitchen floor John Garza (3665) 2 247 11-15-2002, 01:18 PM
Last Post: Victor

Forum Jump: