11-28-2022, 02:57 AM
Out of boredom I took some examples from Wikipedia. They are in the form of subprograms for when it's not tolerated if the result of division is zero, which is possible in some long-winded calculations. Using a subprogram is clumsy but I don't recommend using a "FUNCTION" to try to return more than one value, one which is the LHS of assignment (FUNCTION-NAME = value) and the other which is one of the parameters, because it confuses beginner and intermediate BASIC programmers. Side-effects are good only for those who know what they are doing. Some of you are saying, "oh just assign q and r as part of sub parameter list", but I did it this way to emphasize that the last two values are going to be changed by the subprogram.
Code: (Select All)
''from C functions displayed in:
''https://en.wikipedia.org/wiki/Modulo_operation
''by mnrvovrfc
''CHANGED: quot, remd
''remd = -1 if denom = 0, otherwise remd is expected to be positive
''must use integer division because "regular" division doesn't work properly in QB64
sub ldive(numer as _integer64, denom as _integer64, quot as _integer64, remd as _integer64)
dim q as _integer64, r as _integer64
if denom = 0 then
quot = 0
remd = -1
exit sub
end if
q = numer \ denom
r = numer MOD denom
if r < 0 then
if denom > 0 then
q = q - 1
r = r + denom
else
q = q + 1
r = r - denom
end if
end if
quot = q
remd = r
end sub
''CHANGED: quot, remd
''remd = -1 if denom = 0
sub ldivf(numer as _integer64, denom as _integer64, quot as _integer64, remd as _integer64)
dim q as _integer64, r as _integer64
if denom = 0 then
quot = 0
remd = -1
exit sub
end if
q = numer \ denom
r = numer MOD denom
if (r > 0 and denom < 0) or (r < 0 and denom > 0) then
q = q - 1
r = r + denom
end if
quot = q
remd = r
end sub