Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with "MOD"
#1
I have a problem with "MOD". The formula used gives the correct result, namely -6. To get the final result, the calculation instruction is: -6 MOD 7.
The result is -6 (corresponds to the Wiki), the same in Julia or Octave. 
The MS calculator, on the other hand, gives 1. And that is actually the desired result. Who is right now?

It is about a formula for calculating days of the week according to the Julian/Grgorian calendar.
Reply
#2
Both are right.   It's a lot like saying, "What's the square root of 4?"  Well heck, that's 4 AND -4!  Both are correct.  Wink

Quote:Floored division:
In this method, Quotient is determined by the floor division of Dividend and Divisor. It rounds off the quotient to the nearest smallest integer. So remainder can be computed from the following expression:

r = a – b* floor(a/b), where r, a, b are Remainder, Dividend and Divisor respectively.


Example:

=> -7 % 5 = -7 – 5 *( floor(-7/5) )
= -7 – 5*( floor(-1.4) )
= -7 – 5*( -2)
= -7+10
= 3


In Python, floor division is used to determine the remainder.

Truncated division:
In this method, Quotient is determined by the truncate division of Dividend and Divisor. It rounds off the quotient towards zero. So remainder can be computed from the following expression:

r = a – b* trunc(a/b), where r, a, b are Remainder, Dividend and Divisor respectively.


Example:

=> -7 % 5 = -7 – 5 *( trunc(-7/5) )
= -7 – 5*( trunc(-1.4) )
= -7 – 5*( -1)
= -7+5
= -2


In C/C++, truncate division is used to determine the remainder

Thus, In programming languages which uses truncate division to find the remainder, we always find remainder as (a%b + b)%b (add quotient to remainder and again take remainder) to avoid negative remainder and get the correct result.

So it you always want a positive value, do this:

result = (num MOD mod_value + mod_value) MOD mod_value

Or, specifically in your case:

result = (-6 MOD 7 + 7) MOD 7

Code: (Select All)
Print -6 Mod 7
Print (-6 Mod 7 + 7) Mod 7
Reply
#3
Thanks, that's how it works. Here's just a start:

Code: (Select All)

'Berechnung des Wochentages nach Julianischer/Gregeorianischer Formal
'https://de.wikipedia.org/wiki/Wochentagsberechnung
'5. Jan. 2025

Option _Explicit

Dim As Integer a, d, m, y, c, w
Dim As String wtag

'Welcher Wochentag war der 12. Juni 2006

d = 12
m = 4
y = 6
c = 20

'Formel nach gregorianischer Berechnung
a = (12 + (((2.6 * 4) - 0.2) + 6) + ((6 / 4) - 1) + (20 / 4) - (2 * 20))
w = (a Mod 7 + 7) Mod 7

Print Using "##"; w
'Print (-6 Mod 7 + 7) Mod 7; Hinweis: McNeill

Select Case w
  Case 1
    wtag = "Montag"
End Select

Print
Print "Der 12. Juni 2006 war ein ", wtag

End
Reply
#4
For the  Gregorian Calendar I code it like this...

Code: (Select All)
mo = Val(Mid$(Date$, 1, 2))
yr = Val(Mid$(Date$, 7, 4))
dy = Val(Mid$(Date$, 4, 2))
    Select Case mo
        Case 1: month$ = "Jan": ordinal = 0
        Case 2: month$ = "Feb": ordinal = 31
        Case 3: month$ = "Mar": ordinal = 59
        Case 4: month$ = "Apr": ordinal = 90
        Case 5: month$ = "May": ordinal = 120
        Case 6: month$ = "Jun": ordinal = 151
        Case 7: month$ = "Jul": ordinal = 181
        Case 8: month$ = "Aug": ordinal = 212
        Case 9: month$ = "Sep": ordinal = 243
        Case 10: month$ = "Oct": ordinal = 273
        Case 11: month$ = "Nov": ordinal = 304
        Case 12: month$ = "Dec": ordinal = 334
    End Select
    xdays = (365 * yr) + yr \ 4 - yr \ 100 + yr \ 400 - 365
    If yr Mod 4 = 0 And yr Mod 100 Or yr Mod 4 = 0 And yr Mod 100 = 0 And yr Mod 400 = 0 Then leapyear = 1
    xdays = xdays + ordinal + 1
    If leapyear And mo < 3 Then xdays = xdays - 1
    xover = xdays Mod 7 ' The day of the week the first day of month and year appears on the calendar. 0 to 6 for Sunday to Saturday.

For the Julian Calendar... I don't give a ****! Big Grin

Pete
Reply
#5
I remind you guys -- if you want everything Date then look not further than here: https://qb64phoenix.com/forum/showthread.php?tid=2103
Reply
#6
(Yesterday, 02:22 AM)SMcNeill Wrote: I remind you guys -- if you want everything Date then look not further than here: https://qb64phoenix.com/forum/showthread.php?tid=2103

What part of "I don't give 4 asterisks!" did you not understand? Huh  Big Grin Big Grin Big Grin

Steve, that looked fun to make. I'll have to fiddle around with it later. Line-wise, it looks larger than my entire popup Gregorian calendar routine, so it must do something interesting somewhere. Fun to check it out, for sure.

Oh, I just realized, you missed one. It doesn't have a Beatles algorithm for ..... 8-days a week.

Pete
Reply




Users browsing this thread: 1 Guest(s)