HP Forums
(42S) (& 41C): Ceiling (& Floor) Function Programme - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (42S) (& 41C): Ceiling (& Floor) Function Programme (/thread-7691.html)



(42S) (& 41C): Ceiling (& Floor) Function Programme - Gerald H - 01-31-2017 05:39 PM

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



RE: HP 42S: Ceiling Function Programme - Didier Lachieze - 01-31-2017 09:20 PM

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



RE: HP 42S: Ceiling Function Programme - Gerald H - 02-01-2017 06:14 AM

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



RE: HP 42S: Ceiling Function Programme - Werner - 02-01-2017 07:07 AM

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


RE: HP 42S: Ceiling Function Programme - Paul Dale - 02-01-2017 07:24 AM

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


RE: HP 42S: Ceiling Function Programme - Gerald H - 02-01-2017 08:09 AM

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



RE: HP 42S: Ceiling Function Programme - Werner - 02-01-2017 09:20 AM

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


RE: HP 42S: Ceiling Function Programme - Paul Dale - 02-01-2017 09:52 AM

There is always the 34S's single step version Smile

- Pauli


RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme - Gerson W. Barbosa - 02-07-2017 02:20 AM

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.


RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme - Paul Dale - 02-07-2017 02:33 AM

This gets my vote for best use of a hyperbolic function Smile

- Pauli


RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme - rprosperi - 02-07-2017 11:42 PM

(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!


RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme - Dieter - 02-09-2017 09:16 AM

(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


RE: HP 42S (& 41C): Ceiling (& Floor) Function Programme - Gerson W. Barbosa - 02-09-2017 08:33 PM

(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]


RE: (42S) (& 41C): Ceiling (& Floor) Function Programme - Juan14 - 05-15-2020 11:59 PM

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