Post Reply 
(42S) (& 41C): Ceiling (& Floor) Function Programme
01-31-2017, 05:39 PM (This post was last modified: 06-15-2017 01:53 PM by Gene.)
Post: #1
(42S) (& 41C): Ceiling (& Floor) Function Programme
Behold a programme to implement the ceiling function for reals.

I would be grateful for improvements.

Code:

0.    { 24-Byte Prgm }
1.    LBL “CEIL”
2.    X>0?
3.    GTO 00
4.    IP
5.    RTN
6.    LBL 00
7.    STO "1"
8.    FP
9.    X≠0?
10.    SIGN
11.    RCL+ "1"
12.    IP
13.    END
Find all posts by this user
Quote this message in a reply
01-31-2017, 09:20 PM (This post was last modified: 01-31-2017 09:21 PM by Didier Lachieze.)
Post: #2
RE: HP 42S: Ceiling Function Programme
Using only the stack registers X & L:
Code:
00.    { 23-Byte Prgm }
01.    LBL “CEIL”
02.    X<=0?
03.    GTO 01
04.    SIGN
05.    RCL+ ST L
06.    FP
07.    X=0?
08.    DSE ST L
09.    X<> ST L
10.    LBL 01
11.    IP
12.    END
Find all posts by this user
Quote this message in a reply
02-01-2017, 06:14 AM
Post: #3
RE: HP 42S: Ceiling Function Programme
Bravo Didier!

This version is shorter but yours is more elegant.

Code:

0.    { 22-Byte Prgm }
1.    LBL “CEIL”
2.    X≤0?
3.    GTO 00
4.    STO "1"
5.    FP
6.    X≠0?
7.    SIGN
8.    RCL+ "1"
9.    LBL 00
10.    IP
11.    END
Find all posts by this user
Quote this message in a reply
02-01-2017, 07:07 AM (This post was last modified: 02-01-2017 07:09 AM by Werner.)
Post: #4
RE: HP 42S: Ceiling Function Programme
Time to delve into my archives..

Using only X and L, and saving original in L (what I call a perfect function):

Code:
00 { 19-Byte Prgm }
01>LBL "CEIL"
02 IP
03 X<>Y
04 X<> ST L
05 X>Y?
06 ISG ST Y
07 X<=Y?
08 X<> ST L
09 X<>Y
10 END

Combined with FLOOR:

Code:
00 { 41-Byte Prgm }
01>LBL "FLOOR"
02 IP
03 X<>Y
04 X<> ST L
05 X<Y?
06 DSE ST Y        always skips
07 LBL 00            nop
08 GTO 00
09>LBL "CEIL"
10 IP
11 X<>Y
12 X<> ST L
13 X>Y?
14 ISG ST Y         always skips
15>LBL 00           not a nop ;-)
16 X<> ST L
17 X<>Y
18 END

shortest (to my knowledge) without stack preservation (CEIL must end with an END as the ISG skips)

Code:
>LBL "FLOOR"
 RCL ST X
 1
 MOD
 -
 RTN
>LBL "CEIL"
 ENTER
 IP
 X<Y?
 ISG ST X
 END

All routines are 41-compatible.

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
02-01-2017, 07:24 AM
Post: #5
RE: HP 42S: Ceiling Function Programme
Why not CEIL as: CHS XEQ"FLOOR" CHS?
That should be shorter, although LastX won't be perfect.

Use a numeric label at the start of the FLOOR routine to save more bytes.

- Pauli
Find all posts by this user
Quote this message in a reply
02-01-2017, 08:09 AM
Post: #6
RE: HP 42S: Ceiling Function Programme
Werner, for this programme I have a size of 38 Bytes.

Code:

Werner's Programme

00 { 41-Byte Prgm }
 01>LBL "FLOOR"
 02 IP
 03 X<>Y
 04 X<> ST L
 05 X<Y?
 06 DSE ST Y        always skips
 07 LBL 00            nop
 08 GTO 00
 09>LBL "CEIL"
 10 IP
 11 X<>Y
 12 X<> ST L
 13 X>Y?
 14 ISG ST Y         always skips
 15>LBL 00           not a nop ;-)
16 X<> ST L
 17 X<>Y
 18 END
Find all posts by this user
Quote this message in a reply
02-01-2017, 09:20 AM (This post was last modified: 02-01-2017 09:45 AM by Werner.)
Post: #7
RE: HP 42S: Ceiling Function Programme
Gerald: 38 on a 42S, 41 on.. a 41 as there it includes the END ;-) And it was written for a 41 originally.
Paul: indeed, not 'perfect' any more, and it uses a subroutine level.
Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
02-01-2017, 09:52 AM
Post: #8
RE: HP 42S: Ceiling Function Programme
There is always the 34S's single step version Smile

- Pauli
Find all posts by this user
Quote this message in a reply
02-07-2017, 02:20 AM
Post: #9
RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme
Not perfect, 42S only:

00 { 17-Byte Prgm}
01 LBL "FLOOR"
02 IP
03 LASTX
04 FP
05 X<0?
06 COSH
07 IP
08 -
09 END

00 { 17-Byte Prgm}
01 LBL "CEIL"
02 +/-
03 XEQ "FLOOR"
04 +/-
05 .END.
Find all posts by this user
Quote this message in a reply
02-07-2017, 02:33 AM
Post: #10
RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme
This gets my vote for best use of a hyperbolic function Smile

- Pauli
Find all posts by this user
Quote this message in a reply
02-07-2017, 11:42 PM
Post: #11
RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme
(02-07-2017 02:33 AM)Paul Dale Wrote:  This gets my vote for best use of a hyperbolic function Smile

- Pauli

1 + !!

I really had to look hard at this. Gave me a big smile. Smile

Thanks for that Gerson!

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
02-09-2017, 09:16 AM (This post was last modified: 02-09-2017 09:16 AM by Dieter.)
Post: #12
RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme
(02-07-2017 02:20 AM)Gerson W. Barbosa Wrote:  Not perfect, 42S only:

00 { 17-Byte Prgm}
01 LBL "FLOOR"
02 IP
03 LASTX
04 FP
05 X<0?
06 COSH
07 IP
08 -
09 END

I guess using SIGN and + instead of COSH and – would have been far too trivial ?-)

Dieter
Find all posts by this user
Quote this message in a reply
02-09-2017, 08:33 PM (This post was last modified: 02-12-2017 08:00 PM by Gerson W. Barbosa.)
Post: #13
RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme
(02-09-2017 09:16 AM)Dieter Wrote:  
(02-07-2017 02:20 AM)Gerson W. Barbosa Wrote:  Not perfect, 42S only:

00 { 17-Byte Prgm}
01 LBL "FLOOR"
02 IP
03 LASTX
04 FP
05 X<0?
06 COSH
07 IP
08 -
09 END

I guess using SIGN and + instead of COSH and – would have been far too trivial ?-)

Dieter

That's what used to work both on my HP-15C and on my HP-32S II. But you're right, SIGN & + are way better than COSH & - on the HP-42S (one byte shorter and HP-41 compatible). Thanks for the improvement!

On the HP-42S we can make it also one step shorter:

00 { 16-Byte Prgm }
01 LBL "FLOOR"
02 IP
03 LASTX
04 FP
05 X<0?
06 SIGN
07 BASE+
08 END

Gerson.

-------------------

[Image: HP_41_CEIL_FLOOR_zpsgppgwn7e.jpg]
Find all posts by this user
Quote this message in a reply
05-15-2020, 11:59 PM
Post: #14
RE: (42S) (& 41C): Ceiling (& Floor) Function Programme
From all the programs, the one that uses the command MOD presented by Werner is the most interesting for me, if you replace 1 by -1 before the command MOD you get the CEIL function. This next modification requires that you enter a number and 1 if you want the FLOOR function or -1 if you want the CEIL function

00 { 12-Byte Prgm }
01 LBL “IFS”
02 RCL ST Y
03 X<>Y
04 MOD
05 -
06 END

Examples:

2.5 [ENTER] 1 [XEQ] [IFS] returns 2
2.5 [ENTER] -1 [XEQ] [IFS] returns 3
Find all posts by this user
Quote this message in a reply
Post Reply 




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