QB64 Phoenix Edition
Operator MOD - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: Chatting and Socializing (https://staging.qb64phoenix.com/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://staging.qb64phoenix.com/forumdisplay.php?fid=2)
+--- Thread: Operator MOD (/showthread.php?tid=1195)

Pages: 1 2 3 4 5 6 7 8


RE: Operator MOD - bplus - 11-29-2022

Sorry forgot to get quote for next page, referring to this:
https://staging.qb64phoenix.com/showthread.php?tid=1195&pid=10829#pid10829

Not sure Steve_Mod of the third type will work with Singles, Doubles, _Floats with the Integer Division.

I see precision problems coming back to haunt us with decimal point numbers, the Reals, probably with Steve_Mod of the 2nd type.


RE: Operator MOD - Kernelpanic - 11-29-2022

(11-28-2022, 10:23 PM)Pete Wrote: Steve, yours needs some work if you pop a negatives in there as the modulo.

Code: (Select All)
$CONSOLE:ONLY
PRINT "Equation  Steve's Results  |  Pete's Results"
. . .
Function Steve_ModX (num1, num2)
  Steve_ModX = ((num1 Mod num2) + num2) Mod num2
End Function

This gives correct results. What I don't understand is why the program should be wrong because of an addendum? That way I can make any program skid.

Code: (Select All)
Function Steve_ModX (num1, num2)
  Steve_ModX = ((num1 Mod num2) + Abs(num2)) Mod num2
End Function

Steve_ModX = ((num1 Mod num2) + Abs(num2)) Mod num2


RE: Operator MOD - Pete - 11-29-2022

Steve updated his function so it would match my routine's capability to work with both positive and negative modulos.

Pete


RE: Operator MOD - Kernelpanic - 11-30-2022

(11-29-2022, 11:58 PM)Pete Wrote: Steve updated his function so it would match my routine's capability to work with both positive and negative modulos.

Pete
Oh, and is that correct?  Huh

[Image: MOD-Negative-Zahlen2022-11-29.jpg]


RE: Operator MOD - SMcNeill - 11-30-2022

(11-30-2022, 02:50 AM)Kernelpanic Wrote:
(11-29-2022, 11:58 PM)Pete Wrote: Steve updated his function so it would match my routine's capability to work with both positive and negative modulos.

Pete
Oh, and is that correct?  Huh

[Image: MOD-Negative-Zahlen2022-11-29.jpg]


Is 2, or -2, the correct answer to SQR(4)?

BOTH are valid results.  You just need to choose one or the other.  Wink


RE: Operator MOD - vince - 11-30-2022

that's both a two and a negative two for all you nobodies


RE: Operator MOD - Kernelpanic - 12-01-2022

(11-30-2022, 03:49 AM)SMcNeill Wrote:
(11-30-2022, 02:50 AM)Kernelpanic Wrote: Oh, and is that correct?  Huh

[Image: MOD-Negative-Zahlen2022-11-29.jpg]


Is 2, or -2, the correct answer to SQR(4)?

BOTH are valid results.  You just need to choose one or the other.  Wink

Hmm, I do not know! This one: -9 MOD -5 = 1 looks to me: Divides integer with no remainder. Shows the number of b in a.
Code: (Select All)
a = -9: b = -5
c = a \ b
Print c

I wrote a little program to explain this for myself.
Code: (Select All)
'Beispiele fuer MOD - 30. Nov. 2022

Option _Explicit

Dim As Integer a, b, c
Dim As Single f, fganz, frest

a = 13: b = 5

Print

'Liefert den Rest einer ganzzahligen Division
'Returns the remainder of an integer division
c = a Mod b
Print Using "MOD liefer den Rest von 13 MOD 5: ##"; c

Print

'Teilt ganzzahlig ohne Rest. Zeigt die Anzahl von b in a.
'Divides integer with no remainder. Returns the number of b in a.
c = a \ b
Print Using "Anzahl von 5 in 13: ## "; c

Print

'Mit Fliesskommazahlen
f = 7.456
frest = Abs(f - Fix(f))
fganz = f - frest
Print Using "Ganzzahl von 7.456: ##"; fganz
Print Using "Nachkommateil von 7.456: .###"; frest

Print
c = fganz Mod b
Print "Ganzzahl MOD b: ";
Print c

Print
c = Fix(f) Mod b
Print "Und das Ganze direkt mit Fix(f): ";
Print c

End



RE: Operator MOD - Chris - 12-03-2022

What problem do You have with implementing a new function to QB64 in the form of:

    MODn(y , x) = y - x * INT(y / x)
    or
     (y MODn x) = y - x * INT(y / x)

All it takes is a little good will. If no one has it, well...
As of today, the MOD is unusable.

Regards - Chris


RE: Operator MOD - SMcNeill - 12-03-2022

(12-03-2022, 08:19 AM)Chris Wrote: What problem do You have with implementing a new function to QB64 in the form of:

    MODn(y , x) = y - x * INT(y / x)

What problem do you have in adding and implementing that function into your own code???

As of today, YOUR code is unusable.  Nobody else is having any problem.


RE: Operator MOD - Pete - 12-03-2022

>>> MODn(y , x) = y - x * INT(y / x)

Another nice integer modula formula, but I'd tweak it to handle negative mods...

Code: (Select All)
$CONSOLE:ONLY
x = 5
FOR y = 20 TO -20 STEP -1
    PRINT y; MODn(y, x)
NEXT

FUNCTION MODn (y, x)
    MODn = y - ABS(x) * INT(y / ABS(x))
END FUNCTION

Pete