HP 15C, DM42, HP 27S: Floor and Ceiling Functions
(Updated on 12/22/2023 - see note below for the HP 15C versions)
Notes :
HP 15C: includes HP 15C, Original, Limited, and Collector's Edition, and apps
DM42: includes HP 42S, DM42, Free42, Plus42
Introduction: Floor and Ceiling Functions
The floor and ceiling functions are two common functions that transfers a number to an integer.
Floor Function
Floor: The greatest integer that is less than or equal to x. A common symbol for floor is |_x_| and a function call is floor(x).
Let int(x) be the integer part function (sometimes labeled intg(x) or lp(x)), and frac(x) be the fractional part function (sometimes labeled fp(x)).
# x is an integer
If frac(x) = 0, then return floor(x) = x
# x is not an integer
If x ≥ 0, then return floor(x) = int(x)
If x < 0, then return floor(x) = int(x) - 1
Per the function.wolfram.com web page: an equivalent using the modulus function for floor(x) is:
floor(x) = x - x mod 1
Examples:
floor(2.8) = 2
floor(6) = 6
floor(-2.8) = -3
Ceiling Function
Ceiling: The least integer that is greater than or equal to x. A common symbol for ceiling is |-x-| (except the horizontal lines are the top) and a function call is either ceil(x) or ceiling(x).
# x is an integer
If frac(x) = 0, then return ceil(x) = x
# x is not an integer
If x ≥ 0, then return ceil(x) = int(x) + 1
If x < 0, then return ceil(x) = int(x)
Per the function.wolfram.com web page: an equivalent using the modulus function for floor(x) is:
ceil(x) = x + (-x) mod 1
Examples:
ceil(2.8) = 3
ceil(6) = 6
ceil(-2.8) = -2
Calculator Code: HP 15C, DM 42, HP 27S
HP 15C
This program is NOT accurate for -1 < x < 0 (such as x = -0.5). Gratitude to Werner for alerting me of this. (12/22/2023)
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)
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
DM42/Free 42/HP 42S
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
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/IntegerFunctions/Floor/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/IntegerFunctions/Ceiling/27/01/05/
Eddie
All original content copyright, © 2011-2023. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.