Post Reply 
(42, all flavours) Integer Division - how?
01-08-2021, 05:24 PM
Post: #60
RE: (42, all flavours) Integer Division - how?
(12-20-2020 03:22 AM)Albert Chan Wrote:  DIV code, using REM. It work with signed arguments, both a and b.

The latest Free42 Windows version (2.5.23, 1/6/2021) now has FMA.
Thank you. Thomas Okken Smile

PI PI PI X^2 +/- XEQ "FMA"      → -1.37075656833106266883964580072991e-34

FMA based DIV behave the same as my REM based DIV.

Code:
00 { 36-Byte Prgm }
01▸LBL "DIV"    @  L     X     Y     Z     T
02 RCL ST Y
03 RCL ST Y     @        b     a     b     a
04 ÷
05 ENTER
06 IP           @    IP(q)     q     b     a
07 X>Y?
08 DSE ST X     @ floor(q) < 0, always skip
09 X<Y?
10 RTN          @       q      q     b     a
11 X<> ST Z     @       b      q     q     a
12 LSTO "b"
13 +/-
14 R↑           @       a     -b     q     q
15 FMA          @   a-b*q      q     q     q
16 RCL÷ "b"
17 X<0?
18 DSE ST Y
19 NOP
20 X<>Y
21 END

Note: (a-b*q)/b = (sign(b)*a - |b|*q) / |b|

In other words, DIV(a,b) = DIV(sign(b)*a, |b|)
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 - 01-08-2021 05:24 PM



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