QB64 Phoenix Edition
Operator MOD - Printable Version

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

Pages: 1 2 3 4 5 6 7 8


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

SMcNeill

Can you complete this formula with data: -7 MOD 5 so that the result is displayed as a variable: PRINT A #

Chris


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

(11-27-2022, 08:54 PM)Chris Wrote: SMcNeill

Can you complete this formula with data: -7 MOD 5 so that the result is displayed as a variable: PRINT A #

Chris


Code: (Select All)
A# = ModAll(-7, 5)
PRINT A#

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 - SMcNeill - 11-27-2022

Remember, MOD takes the closest multiple of your number, and then gives the remainder.

So 13 MOD 5...
10 is the closest multiple of 5...
13 - 10 = 3

In the case of -7 MOD 5...
-5 is the closest multiple of 5...
-7 - -5 = -2

MOD is giving the proper results by this logic.  You're just not used to thinking of it in such a manner.  Wink


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

Code: (Select All)
$Console:Only
_Dest _Console

Option _Explicit
Declare Library
    Function fmod# (ByVal x As Double, Byval y As Double)
End Declare

Print fmod(-1#, 5#)
Print fmod(-1.4#, 5#)
Print fmod(1.4#, 5#)
Print fmod(-7.0#, 5#)
Print fmod(-7.1#, 5#)

result
Code: (Select All)
-1
-1.4
1.4
-2
-2.1



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

My point is to supplement your code with numerical data. So where to enter -7 and 5. I want the result obtained in the variable A #.

Chris


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

(11-27-2022, 09:02 PM)Chris Wrote: My point is to supplement your code with numerical data. So where to enter -7 and 5. I want the result obtained in the variable A #.

Chris

https://qb64phoenix.com/forum/showthread.php?tid=1195&pid=10746#pid10746 -- If that doesn't work for you, then it just can't be done. You might need to try swapping over to COBOL to get your desired results.


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

Jack

There is only one correct result.

Chris


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

SMcNeil
There is only one correct result.
 
Chris


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

Chris, in your view what are the correct results? and what's your reference?
Code: (Select All)
$Console:Only
_Dest _Console

Option _Explicit
Declare Library
    Function fmod# (ByVal x As Double, Byval y As Double)
    Function remainder# (ByVal x As Double, Byval y As Double)
    Function remquo# (ByVal x As Double, Byval y As Double, quo As Long)
End Declare

Dim As Long quo
Print fmod(-1#, 5#)
Print fmod(-1.4#, 5#)
Print fmod(1.4#, 5#)
Print fmod(-7.0#, 5#)
Print fmod(-7.1#, 5#)

Print remquo(-1#, 5#, quo), quo
Print remquo(-1.4#, 5#, quo), quo
Print remquo(1.4#, 5#, quo), quo
Print remquo(-7.0#, 5#, quo), quo
Print remquo(-7.1#, 5#, quo), quo

result
Code: (Select All)
-1
-1.4
1.4
-2
-2.1

-1        0
-1.4      0
1.4      0
-2      -1
-2.1    -1



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

Trying to come up with something other than what's been posted...

Code: (Select All)
FOR i = 5 TO -7 STEP -1
    PRINT i, modx(i, 5)
NEXT

FUNCTION modx (i, j)
    IF SGN(i) < 0 THEN
        IF ABS(i) <> ABS(j) THEN modx = (ABS(j) - ABS(i MOD j)) ELSE modx = 0
    ELSE
        modx = i MOD j
    END IF
END FUNCTION