HP Forums
(42S) Integer Quotient - 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) Integer Quotient (/thread-9107.html)



(42S) Integer Quotient - Gerald H - 09-19-2017 06:38 AM

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



RE: (42S) Integer Quotient - Werner - 09-19-2017 07:45 AM

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



RE: (42S) Integer Quotient - Gerald H - 09-19-2017 08:46 AM

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.


RE: (42S) Integer Quotient - Werner - 09-19-2017 11:20 AM

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


RE: (42S) Integer Quotient - Gerald H - 09-19-2017 12:09 PM

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.


RE: (42S) Integer Quotient - grsbanks - 09-20-2017 10:17 AM

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


RE: (42S) Integer Quotient - Gerald H - 09-20-2017 12:08 PM

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