HP Forums

Full Version: (42S) (& 41C): Ceiling (& Floor) Function Programme
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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
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
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
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
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
There is always the 34S's single step version Smile

- Pauli
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.
This gets my vote for best use of a hyperbolic function Smile

- Pauli
(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!
(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
(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]
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
Reference URL's