Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remainder(n, d) Better than MOD, same as capping wrapping?
#2
@bplus

I responded to the formula Chris posted, in that thread, with a modification for negative mods...


Code: (Select All)
_Title "Remainder test" ' b+ for a modulus that always returns a number between 0 and divisor
' if divisor is negative then return a rational between 0 and some rational d < 0
' if divisor is positive then return a rational between 0 and some rational d > 0
' if divisor is 0 ? can't divide by 0
' Do we need to round? Doesn't look like it but I just ran a couple quick tests. Folks here can find fault with anything! ;-))

' NOTE: when testing don't leave space between , and d  eg, do 5,3 not 5, 3

$Console:Only
Do
    Input "0's quit, please enter n, d to find remainder n/d between 0 and d "; n, d
    If (n = 0) Or (d = 0) Then End
    Print Remainder##(n, d)
    Print
Loop

' modeled on MODn
'ref Pete  https://qb64phoenix.com/forum/showthread.php?tid=1195&pid=10983#pid10983

Function Remainder## (n##, d##)
    If d## = 0 Then Exit Function
    Remainder## = n## - ABS(d##) * Int(n## / ABS(d##))
End Function


So by putting ABS() in those two places your posted code will now handle negative modulo entries, like 7,-5 and -7,-5.

Pete
Shoot first and shoot people who ask questions, later.
Reply


Messages In This Thread
RE: Remainder(n, d) Better than MOD, same as capping wrapping? - by Pete - 12-03-2022, 09:06 PM



Users browsing this thread: 2 Guest(s)