Remainder(n, d) Better than MOD, same as capping wrapping?
#10
(12-03-2022, 05:22 PM)bplus Wrote: Break this? 
For ideal modulus, I imagine it should be between 0 and the divisor = modulus whether divisor is pos or negative.

I read johannhowitzer Wrapping, capping and other... and it all fell in place what we are trying to reach, keeping numbers between 0 and the divisor whether integer or float like Pi, we just want a proper remainder.

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://staging.qb64phoenix.com/showthread.php?tid=1195&pid=10983#pid10983
Function Remainder## (n##, d##)
    If d## = 0 Then Exit Function
    Remainder## = n## - (d##) * Int(n## / (d##))
End Function

BTW best toggle I've seen and used often from Chia Pet:
toggle = 1 - toggle
 I didn't see Chia Pet's post, but toggle = 1 - toggle always gives result=toggle; to toggle between 1 and 0 shouldn't it be toggle = Abs(toggle - 1) ?   
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply


Messages In This Thread
RE: Remainder(n, d) Better than MOD, same as capping wrapping? - by PhilOfPerth - 12-31-2022, 04:39 AM



Users browsing this thread: 3 Guest(s)