Help with Gauss' formula

Hello,
I’m trying to have Gauss’ formula work in an app of mine, but I always get a negative number.
Can anybody correct my code? Thank you.

https://en.wikibooks.org/wiki/Mathematics_of_the_Jewish_Calendar/Gauss'_Formula_for_the_Date_of_Pesach

[code]dim y as Integer = 73761 + 2019//the Jewish year. In the article it is referred as “A”

dim a as Integer = (12* y + 17) MOD 19
dim b as Integer = y MOD 4
dim n as Double = 32 + (4343/98496) + (1 + (272953/492480)) * a + (b/4) - ((313 / 98496) * y)

//alternative calculation found on internet: it returns the same n value as above
//dim n as double = (3156215 / 98496) + ((765433 / 492480) * ((12*y + 17) MOD 19)) + ((y mod 4) / 4) - ((313 / 98496) * y)

//both n return the same negative number (for instance -179.9876)

//from what I understand from the internet article, I now need to get the integer part of n and its decimal part
dim m as Integer = n
dim o as String = Format(n, “#.####”)
dim splitter() as String = Split(o, “.”)//at present assuming format does not return “,” instead of “.”
dim decim as Integer = val(splitter(1))

dim c as Integer = ((3* y) + (5*b) + N + 5) MOD 7

dim s as String
if c = 2 or c = 4 or c = 6 then
s= str(m+1)
elseif c = 1 and a > 6 and decim >= 1367/2160 then//I don’t know if it is the same as if c = 1, a > 6 etc. of the article
s = str(m+2)
elseif c = 0 and a > 11 and decim >= 23269/25920 then//same as above: is c = 0, a > 11 etc…
s = str(m+1)
end if
Label1.text = s + " March"[/code]

Where did you get this information:

dim y as Integer = 73761 + 2019//the Jewish year. In the article it is referred as "A"

Because your Jewish year is so big, then you will always get a negative number.

Edit: I think it is 3761 + 2019, an extra 70,000 years on your code

[quote=439252:@Alberto De Poo]Where did you get this information:

dim y as Integer = 73761 + 2019//the Jewish year. In the article it is referred as “A”

Because your Jewish year is so big, then you will always get a negative number.

Edit: I think it is 3761 + 2019, an extra 70,000 years on your code[/quote]

Albert is onto something here, although I think it might be 3760 (off the top of my head).

Also, when you calculate c you are multiplying it by the double N but the article shows that you should only be multiplying it by the whole value of that number.

I was going to comment that but because the formula is + N I don’t think it matter if you change to M (talking about the value of c). But I agree, it should be changed to m just to be clear.

@Alberto De Poo and Jonathan Eisen

Correct: the Jewish year should of course be 3761. I think my head was somewhere else both when I entered it and later when I did not notice the mistake.

Right also about c calculation: m instead of the wrong n
dim c as Integer = ((3* y) + (5*b) + m + 5) MOD 7

Thank you guys. Six eyes better than two.

Has this solved your problem or is it still giving you an incorrect result?

https://en.wikibooks.org/wiki/Mathematics_of_the_Jewish_Calendar/Gauss'_Formula_for_the_Date_of_Pesach

Sorry for being late in answering, but since I was getting inconsistent results I took my time to check this and that.
At first I thought I had problems with leap-year, but then I saw that inconsistencies spread too far.
Then I also found that sometimes m would return a day > 31 (for instance 64); so I added some code, below.

Any suggestions? Thanks.

Using the code below for a range of years 2015 - 2037, I get the following wrong results:

2015 Apr 2 instead of 3
2016 Apr 21 instead of 22
2020 Apr 7 instead of 8
2022 Apr 16 instead of 15
2023 Apr 2 instead of 5
2024 Apr 25 instead of 22
2029 March 28 instead of 30
2030 Apr 18 instead of 17
2031 Apr 6 instead of 7
2032 Apr 24 instead of 26
2033 Apr 14 instead of 13
2035 Apr 22 instead of 23
2016 Apr 10 instead of 11

[code]dim selectedYear as Integer = 2019//val(datePopup(2).Text)
dim y as Integer = 3761 + selectedYear

dim a as Integer = ((12 * y) + 17) MOD 19
dim b as Integer = y MOD 4
dim n as Double = 32 + (4343/98496) + (1 + (272953/492480)) * a + (b/4) - ((313 / 98496) * y)

//alternative calculation found on internet: it returns the same n value as above
//dim n as double = (3156215 / 98496) + ((765433 / 492480) * ((12*y + 17) MOD 19)) + ((y mod 4) / 4) - ((313 / 98496) * y)

dim m as Integer = n//sometimes m returns a day >= 31 (for instance 68), that pushes the month to May.

'if checkLeapYear(selectedYear) then//sometimes, here, it returns the right day (m), sometimes not
'm = m + 1
'end if

dim s as string = format(44, “#.#”)
dim decSeparator as string = right(s,1)

dim o as String = Format(n, “##.####”)
dim splitter() as String = Split(o, decSeparator)
dim decim as Integer = val(splitter(1))

dim c as Integer = ((3* y) + (5*b) + m + 5) MOD 7

if c = 2 or c = 4 or c = 6 then
m = m + 1
elseif c = 1 and a > 6 and decim >= 1367/2160 then//not sure if the original "c = 1, a > 6 and m… correspond to c = 1 AND a > 6 etc
m = m+2
elseif c = 0 and a > 11 and decim >= 23269/25920 then//same as above: c = 0, a > 11…
m = m+1
end if

'if checkLeapYear(selectedYear) then//sometimes, here, it returns the right day (m), sometimes not
'm = m + 1
'end if

if m >= 31 then//for day >= 31, in order to set the month (may) back to April
m = m - 31
end if

//adjust the year from julian to gregorian. To be added to d.day
// Below, adjust also the day from julian to greg (10 days)
dim JulToGreg as Integer = val(left(str(selectedYear), 2))//keep the first 2 digits (20 out of 2019)
JulToGreg = JulToGreg - 2 - (JulToGreg\4)//wrong using / instead of \

dim d as new date
d.Year = selectedYear
d.Month = 3
d.Day = m
d.Day = d.Day + julToGreg
if selectedYear > 1582 then//adjust the day from julian to gregorian
d.day = d.day + 10
end if
d.TotalSeconds = d.TotalSeconds
Label1.text = d.LongDate
[/code]

[quote]dim decim as Integer = val(splitter(1))
[/quote]
This could get rather large. Shouldn’t this be just the decimal portion, ie., 0 <= decim < 1.0

Use Str, not format:

dim s as string = str(44, "#.#")
dim o as String = str(n, "##.####")

otherwise you get the local decimal separator.

In any case since you need only the decimal part of your number and not the decimal as integer (you evaluate it against 23269/25920 that’s less than 1), maybe is better:

dim decim as double=o-floor(o)

[quote=439362:@Antonio Rinaldi]maybe is better:

dim decim as double=o-floor(o)[/quote]
Note: this only works if o > zero. For negative numbers you’ll need ceil instead of floor.

Hello,
the good news is that changing the jewish year from 3761 to 3760, and remove one day from the gotten day (i.e. m = m-1), was enough to fix a lot of days.

But something still persists. The “snippet” just below is necessary to make right some wrong dates. The fact that the snippet is at present necessary to get the right date, means that my Xojo application of Gauss’ formula is still to be bettered.
Where? That is the problem.
Meantime thanks to all who contributed.

BTW: from 2015 to 2099, years that require the “snippet” are:
2022, 2044, 2052, 2071, 2079, 2082, 2098, 2100
Testing 2101, dang! wrong date returned.

//snippet

[quote]if m >= 31 then//added June 1 2019
if c = 0 or c = 1 or c = 2 or c = 3 or c = 4 or c = 6 then
m = m - 1
else
m = m - 3
end if
else
m = m - 1
end if[/quote]

======full code

[code]dim selectedYear as Integer = 2019//val(datePopup(2).Text)
dim y as Integer = 3760 + selectedYear//it was 3761

dim a as Integer = ((12 * y) + 17) MOD 19
dim b as Integer = y MOD 4
dim n as Double = 32 + (4343/98496) + (1 + (272953/492480)) * a + (b/4) - ((313 / 98496) * y)

//alternative calculation found on internet: it returns the same n value as above
//dim n as double = (3156215 / 98496) + ((765433 / 492480) * ((12*y + 17) MOD 19)) + ((y mod 4) / 4) - ((313 / 98496) * y)

dim m as Integer = n
dim decim as double
if n > 0 then
decim = n-Floor(n)
else
decim = n-ceil(n)
end if

dim c as Integer = ((3* y) + (5*b) + m + 5) MOD 7

//after changing jewish year to 3760, in general, 1 must be deducted from m. That is: m = m-1
if m >= 31 then//added June 1 2019
if c = 0 or c = 1 or c = 2 or c = 3 or c = 4 or c = 6 then
m = m - 1
else
m = m - 3
end if
else
m = m - 1
end if

if c = 2 or c = 4 or c = 6 then//here it returns right day
m = m + 1
elseif c = 1 and a > 6 and decim >= 1367/2160 then//not sure if the original "c = 1, a > 6 and m… correspond to c = 1 AND a > 6 etc
m = m + 2
elseif c = 0 and a > 11 and decim >= 23269\25920 then//same as above: c = 0, a > 11…
//m = m + 1//for instance, 2022 would return 16 instead of 15 april
end if

//make up days from julian to gregorian.
dim JulToGreg as Integer = val(left(str(selectedYear), 2))//keep the first 2 digits (20 out of 2019)
JulToGreg = JulToGreg - 2 - (JulToGreg\4)//11 for 17xx, 12 for 18xx, 13 for 19xx - 2099, 14 for 2100 etc
m = m + JulToGreg

dim d as new date
d.Year = selectedYear
d.Month = 3
d.Day = m
d.TotalSeconds = d.TotalSeconds
Label14.text = d.LongDate
[/code]

Using the information about passover here, this code gives me the exact day on that page:

[code]dim y as Integer = 3760 + TextField1.Text.Val //the Jewish year. In the article it is referred as “A”

dim a as Integer = (12* y + 17) MOD 19
dim b as Integer = y MOD 4
dim n as Double = 32 + (4343/98496) + (1 + (272953/492480)) * a + (b/4) - ((313 / 98496) * y)

//from what I understand from the internet article, I now need to get the integer part of n and its decimal part
dim m as Integer = n
// m + decim = n then decim = n - m
dim decim as double = n - m

dim c as Integer = ((3* y) + (5*b) + N + 5) MOD 7

if c = 2 or c = 4 or c = 6 then
m = m + 1
elseif c = 1 and a > 6 and decim >= 1367/2160 then//I don’t know if it is the same as if c = 1, a > 6 etc. of the article
m = m + 2
elseif c = 0 and a > 11 and decim >= 23269/25920 then//same as above: is c = 0, a > 11 etc…
m = m + 1
end if

//make up days from julian to gregorian.
dim JulToGreg as Integer = val(left(TextField1.Text, 2))//keep the first 2 digits (20 out of 2019)
JulToGreg = JulToGreg - 2 - (JulToGreg\4)//11 for 17xx, 12 for 18xx, 13 for 19xx - 2099, 14 for 2100 etc
m = m + JulToGreg

dim d as new date
d.Year = TextField1.Text.Val
d.Month = 3
d.Day = m
Label1.text = d.LongDate[/code]

I just modified 3761 to 3760 and decim to be decimal and not integer (will change values when if c = 0,1 on decim (integer) always more than a fraction), then added your code to adjust JulToGreg.

From what I understand, this is the first day of passover but Jewish start the holiday the day before at sundown, so you can just subtract 1 to the result. Tested 2015-2025 and got this dates:

Edit: created a listbox to test more years/dates and all the results are the same as the webpage

Right
IF you need the sign:

If you don’t need it:

Just to have only a direct function

@Alberto De Poo

Right. From my limited reading of the article I thought that decim should have been the last integer part of n. So I was struggling my circuitous way to obtain it, for instance 123456 from 1.123456, and that misunderstanding created a chain-reaction resulting in what in my previous post I called “Snippet”, trying to fix phantomatical exceptions.

Thank you and all who contributed. Very much appreciated.