(42S) Integer Quotient
09-19-2017, 06:38 AM
Post: #1
 Gerald H Senior Member Posts: 1,414 Joined: May 2014
(42S) Integer Quotient
For large input, eg 888,888,888,888 the 42S snippet

5 / IP

does not return the correct integer quotient.

I have a programme to do the calculation correctly without disturbing the stack but believe it is inefficient.

Could someone suggest a more efficient programme that leaves the stack intact?

Code:
0.    { 29-Byte Prgm }  1.    LBL “IQT5”  2.    R↑  3.    STO 01  4.    R↓  5.    STO 02  6.    5  7.    MOD  8.    +/-  9.    RCL+ 02  10.    5  11.    /  12.    R↑  13.    X<> 01  14.    R↓  15.    END
09-19-2017, 07:45 AM
Post: #2
 Werner Senior Member Posts: 367 Joined: Dec 2013
RE: (42S) Integer Quotient
You always need an auxiliary variable. I use "." for that.

This program will compute Quotient and Rest

Code:
Y := q.X + r In: Y: Y     X: X Out: Y: r      X: q      L: X 00 { 22-Byte Prgm } 01>LBL "RQ" 02 X<>Y 03 STO "." 04 X<>Y 05 MOD 06 STO- "." 07 LASTX 08 STO/ "." 09 X<> "." 10 END
09-19-2017, 08:46 AM
Post: #3
 Gerald H Senior Member Posts: 1,414 Joined: May 2014
RE: (42S) Integer Quotient
Thank you, Werner, I shall certainly use your programme when I need IDIV2.

For my current problem I guess it's necessary to calculate modulo off the stack using register arithmetic, but that appears to be too complicated to be efficient, or at any rate more efficient than my programme above.
09-19-2017, 11:20 AM
Post: #4
 Werner Senior Member Posts: 367 Joined: Dec 2013
RE: (42S) Integer Quotient
If all you want to do is IDIV5:

Code:
01>LBL "5/" 02 10 03 / 04 IP 05 STO+ ST X 06 LASTX 07 FP 08 STO+ ST X 09 IP 10 + 11 END

Cheers, Werner
09-19-2017, 12:09 PM
Post: #5
 Gerald H Senior Member Posts: 1,414 Joined: May 2014
RE: (42S) Integer Quotient
Beautiful, Werner, I love you.

Which leads to my programme which does what I want.

Store 10 in variable "10" & the following programme finds integer quotient on division by 5 without disturbing the stack:

Code:
0.    { 27-Byte Prgm } 1.    LBL “IQT5” 2.    RCL/ "10" 3.    IP 4.    STO+ ST X 5.    STO "." 6.    CLX 7.    LASTX 8.    FP 9.    STO+ ST X 10.    IP 11.    RCL+ "." 12.    END

The question now is which is faster, my programme in the initial posting or this one - in terms of elegance this one wins hands down.
09-20-2017, 10:17 AM
Post: #6
 grsbanks Senior Member Posts: 922 Joined: Jan 2017
RE: (42S) Integer Quotient
(09-19-2017 06:38 AM)Gerald H Wrote:  For large input, eg 888,888,888,888 the 42S snippet

5 / IP

does not return the correct integer quotient.

Have you tried 5 BASE÷ ? I don't have my HP-42S with me so can't test this.
09-20-2017, 12:08 PM
Post: #7
 Gerald H Senior Member Posts: 1,414 Joined: May 2014
RE: (42S) Integer Quotient
(09-20-2017 10:17 AM)grsbanks Wrote:
(09-19-2017 06:38 AM)Gerald H Wrote:  For large input, eg 888,888,888,888 the 42S snippet

5 / IP

does not return the correct integer quotient.

Have you tried 5 BASE÷ ? I don't have my HP-42S with me so can't test this.

Yes, that's the right idea, but doesn't work for large numbers.
 « Next Oldest | Next Newest »

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