Posts: 2,700
Threads: 124
Joined: Apr 2022
Reputation:
134
(12-31-2022, 04:39 AM)PhilOfPerth Wrote: (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) ?
You might be assuming toggle takes on -1 value like QB64's True value, no. Toggle is only ever 0 or 1 specially if DIM's as an integer as opposed a float.
b = b + ...
Posts: 2,700
Threads: 124
Joined: Apr 2022
Reputation:
134
(12-31-2022, 03:59 AM)SMcNeill Wrote: Why not just make 2 distinct mods which you can then call upon to get the output in whatever format you want?
Code: (Select All) Print ModP(10, 3), 10 Mod 3, ModN(10, 3)
Print ModP(10, -3), 10 Mod -3, ModN(10, -3)
Print ModP(-10, 3), 10 Mod 3, ModN(-10, 3)
Print ModP(-10, -3), 10 Mod -3, ModN(-10, -3)
Print ModP(10, 3.14), 10 Mod 3.14, ModN(10, 3.14)
Print ModP(10, -3.14), 10 Mod -3.14, ModN(10, -3.14)
Print ModP(-10, 3.14), -10 Mod 3.14, ModN(-10, 3.14)
Print ModP(-10, -3.14), -10 Mod -3.14, ModN(-10, -3.14)
Function ModP## (num As _Float, div As _Float)
Dim tempValue As _Float
If div = 0 Then Error 5: Exit Function
tempValue = num - Int(num / div) * div
If tempValue < 0 Then tempValue = tempValue + Abs(div)
ModP = tempValue
End Function
Function ModN## (num As _Float, div As _Float)
Dim tempValue As _Float
If div = 0 Then Error 5: Exit Function
tempValue = num - Int(num / div) * div
If tempValue > 0 Then tempValue = tempValue - Abs(div)
ModN = tempValue
End Function
I don't know if this is reply to me or Minerva. If to me I gotta ask why is 2 functions more than 2 lines long better than 1 function 2 lines long?
I remind all that I started thread with spec that remainder needs to be between 0 and divisor. So why use ABS?
b = b + ...
Posts: 1,507
Threads: 160
Joined: Apr 2022
Reputation:
116
@Bplus That's my point though -- Why that spec? Why not just use both functions and then have a choice in however you want the answer to be formatted?
Posts: 2,700
Threads: 124
Joined: Apr 2022
Reputation:
134
The spec is for when conditions of program call for a value between 0 and the denominator. Comes up often in keeping an object inside the boundaries of a Basic screen. Of course I can only think of positive divisors because they are really distances. MOD does not work when the object moves out heading left or heading up and the divisor is the Width or Height of screen.
I will now answer my own question. What breaks this is trying to apply it to situation where 0 is neither the low nor high limit eg keeping a number between -5 and 100 say.
b = b + ...
|