Posts: 42
Threads: 6
Joined: Sep 2022
Reputation:
0
SMcNeill
Can you complete this formula with data: -7 MOD 5 so that the result is displayed as a variable: PRINT A #
Chris
Posts: 2,698
Threads: 328
Joined: Apr 2022
Reputation:
217
11-27-2022, 08:57 PM
(This post was last modified: 11-27-2022, 08:58 PM by SMcNeill.)
(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
Posts: 2,698
Threads: 328
Joined: Apr 2022
Reputation:
217
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.
Posts: 422
Threads: 27
Joined: Apr 2022
Reputation:
26
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
Posts: 42
Threads: 6
Joined: Sep 2022
Reputation:
0
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
Posts: 2,698
Threads: 328
Joined: Apr 2022
Reputation:
217
11-27-2022, 09:06 PM
(This post was last modified: 11-27-2022, 09:08 PM by SMcNeill.)
(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...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.
Posts: 42
Threads: 6
Joined: Sep 2022
Reputation:
0
Jack
There is only one correct result.
Chris
Posts: 42
Threads: 6
Joined: Sep 2022
Reputation:
0
SMcNeil
There is only one correct result.
Chris
Posts: 422
Threads: 27
Joined: Apr 2022
Reputation:
26
11-27-2022, 09:18 PM
(This post was last modified: 11-27-2022, 09:19 PM by Jack.)
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
Posts: 2,183
Threads: 222
Joined: Apr 2022
Reputation:
104
11-27-2022, 09:23 PM
(This post was last modified: 11-28-2022, 03:28 AM by Pete.)
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
|