Post Reply 
(42, all flavours) Integer Division - how?
12-11-2020, 07:32 PM
Post: #5
RE: (42, all flavours) Integer Division - how?
(12-11-2020 10:48 AM)Werner Wrote:  Anybody know how to implement integer division in a fast and reliable way?

I don't know if this is fast, but FMA should work.

10 DEF FND(A,B)
20 Q=FLOOR(A/B)
30 B1=B*1000001 @ B1=B+B1-B1 @ B=B-B1
40 Q1=Q*1000001 @ Q1=Q+Q1-Q1 @ Q=Q-Q1
50 FND=FLOOR((A-Q1*B1-Q*B1-B*Q1-B*Q)/(B+B1))+Q+Q1
60 END DEF

>RUN
>FND(4E11+39,40)
10000000000
>FND(2E12,3)
666666666666
>FND(5E23,1E12-1)
500000000000
>FND(2E23+3E12,4E11)
500000000007
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (42, all flavours) Integer Division - how? - Albert Chan - 12-11-2020 07:32 PM



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