Posts: 52
Threads: 9
Joined: Sep 2022
Reputation:
0
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
Posts: 52
Threads: 9
Joined: Sep 2022
Reputation:
0
This is another operator that will give the correct results?
Chris
Posts: 3,446
Threads: 376
Joined: Apr 2022
Reputation:
345
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
Posts: 52
Threads: 9
Joined: Sep 2022
Reputation:
0
That's exactly what I mean.
It cannot be done without the function ?.
Chris
Posts: 2,910
Threads: 305
Joined: Apr 2022
Reputation:
167
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
Posts: 52
Threads: 9
Joined: Sep 2022
Reputation:
0
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
Posts: 1,356
Threads: 58
Joined: Jul 2022
Reputation:
53
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?
Posts: 4,695
Threads: 222
Joined: Apr 2022
Reputation:
322
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
724 855 599 923 575 468 400 206 147 564 878 823 652 556 bxor cross forever