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


Operator MOD - Chris - 11-27-2022

Hello
How to replace MOD to get correct results.
I have been using the MOD for a long time without problems. The problems started with negative values.

(-1 MOD 5) => (-1)
(-1.4 MOD 5) => (-1)
(1.4 MOD 5) => (1)
(-7. MOD 5) => (-2)
(-7.1 MOD 5) => (-2)

All results are incorrect.

Regards - Chris


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

Chris, what am I missing?

Code: (Select All)
PRINT "      Equation            Result    -1=True | 0=False"
PRINT
PRINT "(-1 MOD 5) >= (-1)", (-1 MOD 5), (-1 MOD 5) >= (-1)
PRINT "(-1.4 MOD 5) >= (-1)", (-1.4 MOD 5), (-1.4 MOD 5) >= (-1)
PRINT "(1.4 MOD 5) >= (1)", (1.4 MOD 5), (1.4 MOD 5) >= (1)
PRINT "(-7 MOD 5) >= (-2)", (-7 MOD 5), (-7 MOD 5) >= (-2)
PRINT "(-7.1 MOD 5) >= (-2)", (-7.1 MOD 5), (-7.1 MOD 5) >= (-2)


All equations produce true results.

MOD utilizes integer division and reports the remainder.

Pete


RE: Operator MOD - Chris - 11-27-2022

This is another operator that will give the correct results?

Chris


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

What do you consider to be correct results?

5 MOD 5 = 0
4 MOD 5 = 4
3 MOD 5 = 3
2 MOD 5 = 2
1 MOD 5 = 1
0 MOD 5 = 0
-1 MOD 5 = 4
-2 MOD 5 = 3
-3 MOD 5 = 2
-4 MOD 5 = 1
-5 MOD 5 = 0
-6 MOD 5 = 4
-7 MOD 5 = 3

^Is that the patten/results you're looking for?

If so, then just do something simple like:

FUNCTION ModAll&& (number as _INTEGER64, number2 AS LONG)
    temp&& = number MOD number2 
    IF temp&& < 0 THEN temp&& = temp&& + number
    ModAll = temp&&
END FUNCTION


RE: Operator MOD - Chris - 11-27-2022

That's exactly what I mean.

It cannot be done without the function ?.

Chris


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

MOD often require some user adjustments, such as coding a function, to fit specific desired patterns.

As far as a different keyword to get what want, none that I'm aware of.

Pete


RE: Operator MOD - Chris - 11-27-2022

To the creators of QB64. Can we expect in the near future that a new operator to solve this type of computation will be planted into QB64?

Chris


RE: Operator MOD - mnrvovrfc - 11-27-2022

But Steve's function is good enough, why try to force a BASIC dialect into a "new" implementation of a function that is mathematically sound?


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

Here it is without a new function:
Code: (Select All)
DefInt A-Z
n = 5
For i = 10 To -10 Step -1
    If i < 0 Then
        copy = i
        While copy < 0
            copy = copy + n
        Wend
        Print copy Mod n
    Else
        Print i Mod n
    End If
Next



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

(11-27-2022, 08:30 PM)Chris Wrote: To the creators of QB64. Can we expect in the near future that a new operator to solve this type of computation will be planted into QB64?

Chris

I doubt it.  What you're seeing is just the nature of MOD.  Sometimes you just have to roll your own functions, if the built in ones don't fulfill all your needs.