Post Reply 
(15/42/47): Floor and Ceiling Functions
11-04-2023, 12:46 AM (This post was last modified: 12-23-2023 01:32 AM by Eddie W. Shore.)
Post: #1
(15/42/47): Floor and Ceiling Functions
HP 15C

(not accurate for -1 < x < 0)

Three labels are used:
D: floor function
E: ceiling function
1: used in calculation for both (roll stack down one extra time when frac(x)≠0)
Code assumed starts from line 000.

Code:
step #:  key code : key

000:  42, 21, 14:  LBL D
001:  __, __, 36:  ENTER
002:  __, 42, 44:  FRAC
003:  __, 43, 20:  x=0
004:  __, 22, _1:  GTO 1
005:  __, __, 33:  R↓
006:  __, 43, 44:  INT
007:  43, 30, _3:  TEST 3  (x≥0)
008:  __, 43, 32:  RTN
009:  __, __, _1:  1
010:  __, __, 30:  -
011:  __, 43, 32:  RTN

012:  42, 21, 15:  LBL E
013:  __, __, 36:  ENTER
014:  __, 42, 44:  FRAC
015:  __, 43, 20:  x=0
016:  __, 22, _1:  GTO 1
017:  __, __, 33:  R↓
018:  __, 43, 44:  INT
019:  43, 30, _2:  TEST 2 (x<0)
020:  __, 43, 32:  RTN
021:  __, __, _1:  1
022:  __, __, 40:  +
023:  __, 43, 32:  RTN

024:  42, 21, _1:  LBL 1
025:  __, __, 33:  R↓
026:  __, 43, 32:  RTN


DM 42

Code:
00 { 16-Byte Prgm }
01▸LBL "FLOOR"
02 ENTER
03 ENTER
04 1
05 MOD
06 -
07 RTN
08 END

00 { 15-Byte Prgm }
01▸LBL "CEIL"
02 ENTER
03 +/-
04 1
05 MOD
06 +
07 RTN
08 .END.


HP 27S

Code:
FLOOR=X-MOD(X:1)

CEIL=X+MOD(-X:1)


Sources:

Wolfram Research, Inc. "Floor Function". Path: Integer Functions > Floor[z] > Representations through equivalent functions > With related functions. Retrieved October 30, 2023.

https://functions.wolfram.com/IntegerFun.../27/01/05/


Wolfram Research, Inc. "Ceiling Function". Path: Integer Functions > Ceiling[z] > Representations through equivalent functions > With related functions. Retrieved October 30, 2023.

https://functions.wolfram.com/IntegerFun.../27/01/05/
Visit this user's website Find all posts by this user
Quote this message in a reply
12-22-2023, 08:28 AM
Post: #2
RE: (15/42/47): Floor and Ceiling Functions
Hello Eddie;
I'm afraid the 15C versions are not correct; both FLOOR and CEIL fail for X=-0.5, returning 0 and 1 respectively.

Here's what I've been able to come up with for FLOOR; CEIL is similar
2 stack levels, 9 bytes without LBL and RTN. Using, but not changing I

 In: L.XYZT
Out: X.FYZ-

with F=FLOOR(X)

INT
X<> I
LASTX
FRAC
TEST 2 ( X<0? )
DSE I  ( always skips )
CHS    ( nop )
Rv
X<> I


Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
12-22-2023, 01:55 PM
Post: #3
RE: (15/42/47): Floor and Ceiling Functions
(12-22-2023 08:28 AM)Werner Wrote:  Here's what I've been able to come up with for FLOOR; CEIL is similar ...

Or, we reuse code: CEIL(x) = -FLOOR(-x)
Find all posts by this user
Quote this message in a reply
12-24-2023, 10:08 AM
Post: #4
RE: (15/42/47): Floor and Ceiling Functions
(12-22-2023 01:55 PM)Albert Chan Wrote:  
(12-22-2023 08:28 AM)Werner Wrote:  Here's what I've been able to come up with for FLOOR; CEIL is similar ...

Or, we reuse code: CEIL(x) = -FLOOR(-x)

Hi Albert; yes but that uses a subroutine level ;-)

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
03-23-2024, 03:47 PM
Post: #5
RE: (15/42/47): Floor and Ceiling Functions
A modest contribution

1) Some Curious Sequences Involving floor & ceiling functions: The American Mathematical Monthly; Vol. 109, No. 6 (Jun. - Jul., 2002) pp. 559-564

2) Solving Recurrence Relations involving floor & ceiling functions: Electronics Letters; 18th August 1994 Vol. 30 No. 17, pp.1391-1393

3) a Summation Formula for Sequences Involving floor & ceiling functions: ROCKY MOUNTAIN JOURNAL OF MATHEMATICS; Volume 36, Number 5, 2006, pp. 1595-1602

BEST!
SlideRule
Find all posts by this user
Quote this message in a reply
05-17-2024, 08:01 PM
Post: #6
RE: (15/42/47): Floor and Ceiling Functions
Hi.

So, after the smoke clears, what would the CEIL and FLOOR code for the 15C, 34C and 42S? Thanks.
Find all posts by this user
Quote this message in a reply
05-24-2024, 11:02 AM (This post was last modified: 05-24-2024 11:03 AM by dm319.)
Post: #7
RE: (15/42/47): Floor and Ceiling Functions
For 42s something like:

Code:
00 { 15-Byte Prgm }
01▸LBL "CEIL"
02 +/-
03 XEQ "FLR"
04 +/-
05 END

Code:
00 { 32-Byte Prgm }
01▸LBL "FLR"
02 ENTER
03 IP
04 X=Y?
05 GTO 01
06 CLX
07 R↓
08 X<0?
09 GTO 02
10 IP
11 GTO 03
12▸LBL 02
13 IP
14 1
15 -
16 GTO 03
17▸LBL 01
18 X<>Y
19 CLX
20 R↓
21▸LBL 03
22 END
Find all posts by this user
Quote this message in a reply
05-24-2024, 07:43 PM (This post was last modified: 05-24-2024 09:15 PM by Matt Agajanian.)
Post: #8
RE: (15/42/47): Floor and Ceiling Functions
(05-24-2024 11:02 AM)dm319 Wrote:  For 42s something like:

Code:
00 { 15-Byte Prgm }
01▸LBL "CEIL"
02 +/-
03 XEQ "FLR"
04 +/-
05 END

Code:
00 { 32-Byte Prgm }
01▸LBL "FLR"
02 ENTER
03 IP
04 X=Y?
05 GTO 01
06 CLX
07 R↓
08 X<0?
09 GTO 02
10 IP
11 GTO 03
12▸LBL 02
13 IP
14 1
15 -
16 GTO 03
17▸LBL 01
18 X<>Y
19 CLX
20 R↓
21▸LBL 03
22 END

An excellent approach. Well, yours is one more in my programming toolkit. Thanks!

For comparison, here’s what I cobbled together for an HP-67 or RPN-67SD:

*LBL A: [Floor(x)]
001: 31 25 11 LBL A
002: 31 71 x<0?
003: 22 01 GTO 1 [006]
004: 31 83 INT
005: 35 22 RTN

LBL 1:
006: 31 25 01 LBL 1
007: 31 83 INT
008: 01 1
009: 51 -
010: 35 22 RTN

*LBL B: [Ceil(x)]
011: 31 25 12 LBL B
012: 31 51 x=0?
013: 35 22 RTN
014: 31 71 x<0?
015: 22 02 GTO 2 [020]
016: 31 83 INT
017: 01 1
018: 61 +
019: 35 22 RTN

LBL 2:
020: 31 25 02 LBL 2
021: 31 83 INT
022: 35 22 RTN

Even though my version trims off five steps, yours seems a bit more efficient and straightforward.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)