Operator MOD
#11
SMcNeill

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

Chris
Reply
#12
(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
Reply
#13
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
Reply
#14
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
Reply
#15
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
Reply
#16
(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://staging.qb64phoenix.com/showthre...6#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.
Reply
#17
Jack

There is only one correct result.

Chris
Reply
#18
SMcNeil
There is only one correct result.
 
Chris
Reply
#19
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
Reply
#20
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
Reply




Users browsing this thread: 8 Guest(s)