Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #56029 > unrolled thread

Find day of week from a given date

Started byMark Wills <markwills1970@gmail.com>
First post2017-03-21 11:51 -0700
Last post2017-03-29 08:23 +0000
Articles 20 on this page of 46 — 11 participants

Back to article view | Back to comp.lang.forth


Contents

  Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-21 11:51 -0700
    Re: Find day of week from a given date "Wolfgang Allinger" <all2001@spambog.com> - 2017-03-21 18:17 -0300
      Re: Find day of week from a given date Hans Bezemer <the.beez.speaks@gmail.com> - 2017-03-22 17:47 +0100
        Re: Find day of week from a given date "Wolfgang Allinger" <all2001@spambog.com> - 2017-03-22 20:52 -0300
        Re: Find day of week from a given date Ron Aaron <rambamist@gmail.com> - 2017-03-23 06:57 +0200
    Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-22 19:16 -0700
      Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-22 19:25 -0700
      Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-22 21:28 -0700
        Re: Find day of week from a given date Alex <alex@rivadpm.com> - 2017-03-23 14:12 +0000
          Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 16:25 -0700
          Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 22:02 -0700
            Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 23:07 -0700
            Re: Find day of week from a given date Alex <alex@rivadpm.com> - 2017-03-24 08:46 +0000
      Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-23 05:35 -0700
        Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-23 15:23 -0700
          Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 09:38 -0700
            Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-24 10:24 -0700
              Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-24 13:32 -0500
                Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:19 -0700
                  Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-24 16:01 -0500
              Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:00 -0700
                Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:19 -0700
                  Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-24 23:35 -0700
                    Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-25 01:59 -0700
                      Re: Find day of week from a given date Julian Fondren <julian.fondren@gmail.com> - 2017-03-25 02:50 -0700
                      Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-25 04:37 -0700
                        Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-25 12:32 -0700
                          Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-25 14:56 -0500
                            Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 02:27 -0700
                              Re: Find day of week from a given date Cecil Bayona <cbayona@cbayona.com> - 2017-03-26 14:07 -0500
                Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 13:17 -0700
                  Re: Find day of week from a given date Howerd <howerdo@yahoo.co.uk> - 2017-03-24 14:43 -0700
                    Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 15:36 -0700
                    Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-24 23:47 -0700
                  Re: Find day of week from a given date Julian Fondren <julian.fondren@gmail.com> - 2017-03-25 03:00 -0700
                  Re: Find day of week from a given date "Wolfgang Allinger" <all2001@spambog.com> - 2017-03-25 09:06 -0300
              Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 12:28 -0700
                Re: Find day of week from a given date Julian Fondren <julian.fondren@gmail.com> - 2017-03-24 12:49 -0700
                  Re: Find day of week from a given date Mark Wills <markwills1970@gmail.com> - 2017-03-24 13:15 -0700
              Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 14:49 -0700
                Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 17:08 -0700
    Re: Find day of week from a given date humptydumpty <ouatubi@gmail.com> - 2017-03-25 04:23 -0700
      Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-26 15:00 -0700
        Re: Find day of week from a given date humptydumpty <ouatubi@gmail.com> - 2017-03-27 00:50 -0700
          Re: Find day of week from a given date hughaguilar96@gmail.com - 2017-03-27 09:14 -0700
    Re: Find day of week from a given date "Robert L." <No_spamming@noWhere_7073.org> - 2017-03-29 08:23 +0000

Page 1 of 3  [1] 2 3  Next page →


#56029 — Find day of week from a given date

FromMark Wills <markwills1970@gmail.com>
Date2017-03-21 11:51 -0700
SubjectFind day of week from a given date
Message-ID<26f921a3-3e9b-4292-a5e7-4f2e2ef0de99@googlegroups.com>
A post on the Forth Programming group on Facebook inspired me
to write this code today. Thought I'd share in the interests
of posting some code.

Given a date in British format (day month year) it will tell you which
day of the week it was. 0=Sunday, 1=Monday, 2=Tuesday and so on.

Works from 1/1/1700 to, well, anything I think.

So:

3 12 1970 dow .
4 ok:0

Yay. I was born on a Thursday. Thursday is now my favourite day of the week!

variable year
variable month
variable day

create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
create centuries 4 c, 2 c, 0 c, 6 c, 

: century ( year - n )
    1700 /mod drop 100 / 4 mod centuries + c@ ;

: leapYear? ( year - flag )
    dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ;

: dow ( day month year -- n )
    dup century >r  100 mod year !  1- month !  day !
    r>  year @ dup 4 /  day @
    year @ leapYear? if leap-years else years then month @ + c@
    + + + +  7 mod ;




Tests:

: ? ( day month year -- )
    dow case
        0 of ." Sunday" endof
        1 of ." Monday" endof
        2 of ." Tuesday" endof
        3 of ." Wednesday" endof
        4 of ." Thursday" endof
        5 of ." Friday" endof
        6 of ." Saturday" endof
    endcase space ;

31 1 1883 ?
3 12 1970 ?
25 12 2017 ?
1 1 2018 ?



Leap Year Tests:
: testLeapYears ( -- )
    \ A year will be a leap year if it is divisible by 4 but not by 100.
    \ If a year is divisible by 4 and by 100, it is not a leap year 
    \ unless it is also divisible by 400.
    \ The years 1600, 2000 and 2400 are leap years, while 1700, 1800, 1900,
    \ 2100, 2200 and 2300 are common years.
    \ https://en.wikipedia.org/wiki/Leap_year
    page 
    2401 1600 do i leapYear? if i . then loop ;


:-)

[toc] | [next] | [standalone]


#56062

From"Wolfgang Allinger" <all2001@spambog.com>
Date2017-03-21 18:17 -0300
Message-ID<DyJrz7AUQoB@allinger-307049.user.uni-berlin>
In reply to#56029
 On 21 Mar 17 at group /comp/lang/forth in article 26f921a3-3e9b-4292-a5e7-4f2e2ef0de99@googlegroups.com
 <markwills1970@gmail.com>  (Mark Wills)  wrote:

> A post on the Forth Programming group on Facebook inspired me
> to write this code today. Thought I'd share in the interests
> of posting some code.

> Given a date in British format (day month year) it will tell you which
> day of the week it was. 0=Sunday, 1=Monday, 2=Tuesday and so on.

> Works from 1/1/1700 to, well, anything I think.


Nice, but with "Zeller's congruence" IIRC you don't need any table.
It should be easy in FORTH :) BTDT in the 80th, but can't find my Proggi  
any longer.

https://en.wikipedia.org/wiki/Zeller%27s_congruence

Try this and disclose your new code please. THX


Saludos (an alle Vernünftigen, Rest sh. sig)
Wolfgang

-- 
Ich bin in Paraguay lebender Trollallergiker :) reply Adresse gesetzt!
Ich diskutiere zukünftig weniger mit Idioten, denn sie ziehen mich auf
ihr Niveau herunter und schlagen mich dort mit ihrer Erfahrung! :p
(lt. alter usenet Weisheit)      iPod, iPhone, iPad, iTunes, iRak, iDiot

[toc] | [prev] | [next] | [standalone]


#56120

FromHans Bezemer <the.beez.speaks@gmail.com>
Date2017-03-22 17:47 +0100
Message-ID<58d2aa8b$0$722$e4fe514c@news.xs4all.nl>
In reply to#56062
Wolfgang Allinger wrote:

> 
>  On 21 Mar 17 at group /comp/lang/forth in article
>  26f921a3-3e9b-4292-a5e7-4f2e2ef0de99@googlegroups.com
>  <markwills1970@gmail.com>  (Mark Wills)  wrote:
> 
>> A post on the Forth Programming group on Facebook inspired me
>> to write this code today. Thought I'd share in the interests
>> of posting some code.
> 
>> Given a date in British format (day month year) it will tell you which
>> day of the week it was. 0=Sunday, 1=Monday, 2=Tuesday and so on.
> 
>> Works from 1/1/1700 to, well, anything I think.
> 
> 
> Nice, but with "Zeller's congruence" IIRC you don't need any table.
> It should be easy in FORTH :) BTDT in the 80th, but can't find my Proggi
> any longer.
> 
> https://en.wikipedia.org/wiki/Zeller%27s_congruence
> 
> Try this and disclose your new code please. THX

: JDAY ( d m y -- jd)                  \ day, month, year to Julian date
  swap dup 2 > if 3 - swap else 9 + swap 1- then rot >r swap >r 100 /mod >r
  1461 * 4 / r> 146097 * 4 / + r> 153 * 2 + 5 / + r> + 1721119 +
;

: WEEKDAY jday 7 mod ;                 ( d m y -- n)

Does the trick.

Hans Bezemer

[toc] | [prev] | [next] | [standalone]


#56151

From"Wolfgang Allinger" <all2001@spambog.com>
Date2017-03-22 20:52 -0300
Message-ID<DyNsoReEQoB@allinger-307049.user.uni-berlin>
In reply to#56120
 On 22 Mar 17 at group /comp/lang/forth in article 58d2aa8b$0$722$e4fe514c@news.xs4all.nl
 <the.beez.speaks@gmail.com>  (Hans Bezemer)  wrote:

> Wolfgang Allinger wrote:

>>
>>  On 21 Mar 17 at group /comp/lang/forth in article
>>  26f921a3-3e9b-4292-a5e7-4f2e2ef0de99@googlegroups.com
>>  <markwills1970@gmail.com>  (Mark Wills)  wrote:
>>
>>> A post on the Forth Programming group on Facebook inspired me
>>> to write this code today. Thought I'd share in the interests
>>> of posting some code.
>>
>>> Given a date in British format (day month year) it will tell you which
>>> day of the week it was. 0=Sunday, 1=Monday, 2=Tuesday and so on.
>>
>>> Works from 1/1/1700 to, well, anything I think.
>>
>>
>> Nice, but with "Zeller's congruence" IIRC you don't need any table.
>> It should be easy in FORTH :) BTDT in the 80th, but can't find my Proggi
>> any longer.
>>
>> https://en.wikipedia.org/wiki/Zeller%27s_congruence
>>
>> Try this and disclose your new code please. THX

> : JDAY ( d m y -- jd)                  \ day, month, year to Julian date
>   swap dup 2 > if 3 - swap else 9 + swap 1- then rot >r swap >r 100 /mod >r
>   1461 * 4 / r> 146097 * 4 / + r> 153 * 2 + 5 / + r> + 1721119 +
> ;

> : WEEKDAY jday 7 mod ;                 ( d m y -- n)

> Does the trick.

Nice, but it's not Zeller's congruence.


I have no Forth at my hand now, only my brain, so let me try :)

\ h = ( q + 13(m+1)/5 + k + k/4 + J/4 + 5J) mod 7
( see the wiki for more info )

DECIMAL
: wday ( q m y -- h )
        100 /mod >r >r  ( -- q m R-- J K)
        1+ 13 5 */      ( -- q (m+1)*13/5 )
        r@ +            \ +K
        r> 4 / +        \ +K/4
        r@ 4 / +        \ +J/4
        r> 5 * +        \ +5J
        +               \ +q
        7 mod           ( -- h )
 ;

Hope it's correct, much shorter: 25 words to your ~45 words, no IF ...
prooved Zeller is better :)

Just my 2 cent hmmm 2 guaranies :)

Just my 1st Forth program after 10 years



Saludos (an alle Vernünftigen, Rest sh. sig)
Wolfgang

-- 
Ich bin in Paraguay lebender Trollallergiker :) reply Adresse gesetzt!
Ich diskutiere zukünftig weniger mit Idioten, denn sie ziehen mich auf
ihr Niveau herunter und schlagen mich dort mit ihrer Erfahrung! :p
(lt. alter usenet Weisheit)      iPod, iPhone, iPad, iTunes, iRak, iDiot

[toc] | [prev] | [next] | [standalone]


#56160

FromRon Aaron <rambamist@gmail.com>
Date2017-03-23 06:57 +0200
Message-ID<oavkdf$v2s$1@dont-email.me>
In reply to#56120

On 22/03/2017 18:47, Hans Bezemer wrote:

> : JDAY ( d m y -- jd)                  \ day, month, year to Julian date
>   swap dup 2 > if 3 - swap else 9 + swap 1- then rot >r swap >r 100 /mod >r
>   1461 * 4 / r> 146097 * 4 / + r> 153 * 2 + 5 / + r> + 1721119 +
> ;
> 
> : WEEKDAY jday 7 mod ;                 ( d m y -- n)
> 
> Does the trick.
> 
> Hans Bezemer

In 8th I would do something like:

: dow \ date -- dow
  d:>fixed 7 mod ;

So:

"2017-03-23" d:parse dow .

would print 4 (Thursday; Sunday is 0, etc).

The algorithm from "Calendrical Calculations" is used to represent dates
in "fixed" format internally (with a separate time field, rather than
using floating-point for the fixed value as CC does).

[toc] | [prev] | [next] | [standalone]


#56153

Fromhughaguilar96@gmail.com
Date2017-03-22 19:16 -0700
Message-ID<57330fd7-3493-4c25-8577-f50728476462@googlegroups.com>
In reply to#56029
On Tuesday, March 21, 2017 at 11:51:10 AM UTC-7, Mark Wills wrote:
> A post on the Forth Programming group on Facebook inspired me
> to write this code today. Thought I'd share in the interests
> of posting some code.
> 
> Given a date in British format (day month year) it will tell you which
> day of the week it was. 0=Sunday, 1=Monday, 2=Tuesday and so on.
> 
> Works from 1/1/1700 to, well, anything I think.
> 
> So:
> 
> 3 12 1970 dow .
> 4 ok:0
> 
> Yay. I was born on a Thursday. Thursday is now my favourite day of the week!
> 
> variable year
> variable month
> variable day
> 
> create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> create centuries 4 c, 2 c, 0 c, 6 c, 
> 
> : century ( year - n )
>     1700 /mod drop 100 / 4 mod centuries + c@ ;
> 
> : leapYear? ( year - flag )
>     dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ;
> 
> : dow ( day month year -- n )
>     dup century >r  100 mod year !  1- month !  day !
>     r>  year @ dup 4 /  day @
>     year @ leapYear? if leap-years else years then month @ + c@
>     + + + +  7 mod ;

I remember when I was 16 I wrote a program to do this --- find the day of the week given the year, month and day of the month --- that was written in line-number BASIC on a PDP-11 and was the first non-trivial program I ever wrote in my life.

I glanced over your code and I was totally confused --- I don't know what you are doing --- I don't think that works.

Here is some code I wrote right now. This needs NOVICE.4TH and LIST.4TH (the new versions). This is roughly comparable to my old BASIC program to the best of my recollection:
--------------------------------------------------------------------------

\ ****** 
\ ****** This is the day of the week calculator.
\ ****** This depends upon <SWITCH above, which is why it is in LIST.4TH rather than TOY.4TH
\ ****** 

create months 31 c, 28 c, 31 c, 30 c, 31 c, 30 c, 31 c, 31 c, 30 c, 31 c, 30 c, 31 c,

1583 constant first-year    \ we don't allow years prior to the Gregorian Calender

2 constant first-day        \ this is the day of the week for Jan 1st of FIRST-YEAR

<switch ( day-of-the-week --- str )
    :noname  drop  c" Sunday" ;       0 swof
    :noname  drop  c" Monday" ;       1 swof
    :noname  drop  c" Tacoday" ;      2 swof
    :noname  drop  c" Wednesday" ;    3 swof
    :noname  drop  c" Thursday" ;     4 swof
    :noname  drop  c" Fishday" ;      5 swof
    :noname  drop  c" Saturday" ;     6 swof
    :noname  . true abort" is an invalid day" ;
    fast-switch> days
    
: extra-day { year -- 1|0 }             \ how many extra days February gets for this year
    year 4 mod if       0 exit then
    year 100 mod if     1 exit then
    year 400 mod if     0 exit then
    1 ;
        
: month-days ( month extra -- days )    \ EXTRA is the extra day to add to February
    over 2 <> if  drop  0  then         \ don't use EXTRA if this is not February
    swap 1-  months + c@  + ;

: check-day ( day month year -- )
    extra-day                           \ -- day month extra
    month-days                          \ -- day month-days
    over 1 < -rot  >  or abort" *** DAY-OF-THE-WEEK given a bad day ***" ;
    
: check-month ( month -- )
    1 13 within 0= abort" *** DAY-OF-THE-WEEK given a bad month ***" ;
    
: check-year ( year -- )  
    first-year < abort" *** DAY-OF-THE-WEEK given a bad year ***" ;
    
: day-of-the-week { month day year | extra -- str }
    month check-month  day month year check-day  year check-year
    year extra-day to extra
    0  month 1 ?do  I extra month-days + loop       \ -- prior-days     \ days this year prior to this month    
    day +                                           \ -- days           \ days this year including leap day (if this a leap year)
    365  year 1-  *  +                              \ -- days           \ days up until now not including prior leap days
    year first-year do  I extra-day + loop          \ -- days           \ days up until now including prior leap days        
    first-day -                                     \ -- days           \ adjust for not starting on Sunday
    7 mod  days ;

[toc] | [prev] | [next] | [standalone]


#56154

Fromhughaguilar96@gmail.com
Date2017-03-22 19:25 -0700
Message-ID<f908db8f-c437-4fa0-84fc-fab5c2d22f58@googlegroups.com>
In reply to#56153
On Wednesday, March 22, 2017 at 7:16:27 PM UTC-7, hughag...@gmail.com wrote:
> create months 31 c, 28 c, 31 c, 30 c, 31 c, 30 c, 31 c, 31 c, 30 c, 31 c, 30 c, 31 c,

Here is a poem that appeared in a Stephen King book about werewolves --- I don't know if it was original with Stephen King --- I modified it to make it rhyme better:

Thirty days hath September, April, June and November,
The rest have thirty-one,
Excepting February that has twenty-eight most every year,
And the moon grows full in every one.

[toc] | [prev] | [next] | [standalone]


#56155

Fromhughaguilar96@gmail.com
Date2017-03-22 21:28 -0700
Message-ID<1a5b4119-90e0-45d8-b03b-d277ab88d845@googlegroups.com>
In reply to#56153
On Wednesday, March 22, 2017 at 7:16:27 PM UTC-7, hughag...@gmail.com wrote:
> Here is some code I wrote right now. 

Well, I compared it to these programs on the internet:

agrees --- http://people.albion.edu/imacinnes/calendar/Day_of_the_Week.html
agrees --- http://www.calculatorcat.com/date/day-of-week.html
disagrees --- https://www.timeanddate.com/date/weekday.html
agrees --- http://www.searchforancestors.com/utility/dayofweek.html
agrees --- http://calendarhome.com/calculate/day-of-week
disagrees --- http://www.mathsisfun.com/games/dayofweek.html

I didn't test these thoroughly --- just picked out a few random dates for comparison.

The MATHISFUN calculator says that it uses Zeller's Algorithm for calculating the day of the week. I looked this up on Wikipedia and it appears to be no good --- I didn't look at it closely, but I noticed that it is not taking into account all the extra days of all the previous leap years --- notice how I do this in my program:

year first-year ?do  I extra-day + loop

Christian Zeller came up with his formula in 1882 --- there weren't any computers then --- so, how did he test it?

[toc] | [prev] | [next] | [standalone]


#56180

FromAlex <alex@rivadpm.com>
Date2017-03-23 14:12 +0000
Message-ID<ob0kvm$442$1@dont-email.me>
In reply to#56155
On 3/23/2017 04:28, hughaguilar96@gmail.com wrote:
> On Wednesday, March 22, 2017 at 7:16:27 PM UTC-7, hughag...@gmail.com
> wrote:
>> Here is some code I wrote right now.
>
> Well, I compared it to these programs on the internet:
>
> agrees ---
> http://people.albion.edu/imacinnes/calendar/Day_of_the_Week.html
> agrees --- http://www.calculatorcat.com/date/day-of-week.html
> disagrees --- https://www.timeanddate.com/date/weekday.html agrees
> --- http://www.searchforancestors.com/utility/dayofweek.html agrees
> --- http://calendarhome.com/calculate/day-of-week disagrees ---
> http://www.mathsisfun.com/games/dayofweek.html
>
> I didn't test these thoroughly --- just picked out a few random dates
> for comparison.
>
> The MATHISFUN calculator says that it uses Zeller's Algorithm for
> calculating the day of the week. I looked this up on Wikipedia and it
> appears to be no good

What? This is peak stupid.

> --- I didn't look at it closely, but I noticed
> that it is not taking into account all the extra days of all the
> previous leap years --- notice how I do this in my program:
>
> year first-year ?do  I extra-day + loop

You didn't look very closely then, since Zeller's algorithm works very 
well, and there's a complete and exhaustive explanation in the Wikipedia 
article. But algorithms, efficient & effective problem solving & design 
are well outside your ken. According to you, the measure of a 
programmers is yards of code.

>
> Christian Zeller came up with his formula in 1882 --- there weren't
> any computers then --- so, how did he test it?
>

Dear god. I am speechless.

-- 
Alex

[toc] | [prev] | [next] | [standalone]


#56206

Fromhughaguilar96@gmail.com
Date2017-03-23 16:25 -0700
Message-ID<c3726952-f784-4c5f-9466-e01e758ce481@googlegroups.com>
In reply to#56180
On Thursday, March 23, 2017 at 7:12:54 AM UTC-7, Alex wrote:
> On 3/23/2017 04:28, hughaguilar96@gmail.com wrote:
> > Christian Zeller came up with his formula in 1882 --- there weren't
> > any computers then --- so, how did he test it?
> 
> Dear god. I am speechless.

I hope this condition is permanent!

Anyway, I used my computer to do some testing:
             Zeller  Hugh
2 28 1752    Fri     Mon
2 29 1752    Sat     Tue
3  1 1752    Wed     Wed

This is Zeller's Algorithm as done here:
http://www.mathsisfun.com/games/dayofweek.html

Zeller has everything prior to 3/1/1752 screwed up. I don't know if this is a bug in the mathisfun code or in Zeller's Algorithm. I don't care --- it is not my business to debug other people's code.

[toc] | [prev] | [next] | [standalone]


#56214

Fromhughaguilar96@gmail.com
Date2017-03-23 22:02 -0700
Message-ID<55813b8c-40cb-46a6-a779-2ac4b535ff25@googlegroups.com>
In reply to#56180
On Thursday, March 23, 2017 at 7:12:54 AM UTC-7, Alex wrote:
> This is peak stupid.

I have noticed with comp.lang.forth that the surest way to kill a thread is to post code. Every time I write Forth code and post it, the thread just dies --- nobody wants to respond --- so, I'm not expecting a response.

I rewrote the code slightly so it doesn't require <SWITCH and hence doesn't require LIST.4TH to be loaded --- I also moved it over to TOY.4TH where I have fun little programs like this --- here is the new version (it does require NOVICE.4TH):
---------------------------------------------------------------------------

\ ****** 
\ ****** This is the day of the week calculator.
\ ****** 

create months 31 c, 28 c, 31 c, 30 c, 31 c, 30 c, 31 c, 31 c, 30 c, 31 c, 30 c, 31 c,

1583 constant first-year    \ we don't allow years prior to the Gregorian Calender

2 constant first-day        \ FIRST-YEAR starts on Saturday --- Monday minus 2 is Saturday

align here ," Saturday"
align here ," Friday"
align here ," Thursday"
align here ," Wednesday"
align here ," Tuesday"
align here ," Monday"
align here ," Sunday"

create <days> , , , , , , ,

: days ( n -- str )
    dup 0<  over 7 >=  or abort" *** DAYS given invalid value ***"
    w *  <days> +  @ ;
    
: extra-day { year -- 1|0 }             \ how many extra days February gets for this year
    year 4 mod if       0 exit then
    year 100 mod if     1 exit then
    year 400 mod if     0 exit then
    1 ;
        
: month-days ( month extra -- days )    \ EXTRA is the extra day to add to February
    over 2 <> if  drop  0  then         \ don't use EXTRA if this is not February
    swap 1-  months + c@  + ;

: check-day ( day month year -- )
    extra-day                           \ -- day month extra
    month-days                          \ -- day month-days
    over 1 < -rot  >  or abort" *** DAY-OF-WEEK given a bad day ***" ;
    
: check-month ( month -- )
    1 13 within 0= abort" *** DAY-OF-WEEK given a bad month ***" ;
    
: check-year ( year -- )  
    first-year < abort" *** DAY-OF-WEEK given a bad year ***" ;
    
: day-of-week { month day year | extra -- str }
    month check-month  day month year check-day  year check-year
    year extra-day to extra
    0  month 1 ?do  I extra month-days + loop       \ -- prior-days     \ days this year prior to this month    
    day +                                           \ -- days           \ days this year including leap day (if this a leap year)
    365  year 1-  *  +                              \ -- days           \ days up until now not including prior leap days
    year first-year ?do  I extra-day + loop         \ -- days           \ days up until now including prior leap days        
    first-day -                                     \ -- days           \ adjust for not starting on Sunday
    7 mod  days ;


\ char & comment      \ this code is Mark Wills': https://groups.google.com/forum/#!topic/comp.lang.forth/dj_lRojeDYs

variable year 
variable month 
variable day 

create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
create centuries 4 c, 2 c, 0 c, 6 c, 

: century ( year - n ) 
    1700 /mod drop 100 / 4 mod centuries + c@ ; 

: leapYear? ( year - flag ) 
    dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ; 

: <dow> ( day month year -- n )                                     \ this was Mark's DOW
    dup century >r  100 mod year !  1- month !  day ! 
    r>  year @ dup 4 /  day @ 
    year @ leapYear? if leap-years else years then month @ + c@ 
    + + + +  7 mod ; 

: dow ( month day year -- str )                                     \ this slight upgrade provides a string
    -rot  swap  rot                 \ day month year
    <dow> days ;
    
: test-dow ( month day year -- )
    cr ." Mark: "  3dup dow     count type
    cr ." Hugh: "  day-of-week  count type ;
    
\ Mark's code assumes that 1900 is a leap year, but it isn't.

\ We also have Zeller's Algorithm which is used here: http://www.mathsisfun.com/games/dayofweek.html
\ This provides bad results for everything prior to 3/1/1752.
\ I don't know if it is the algorithm or the implementation that is bad.
    
&

[toc] | [prev] | [next] | [standalone]


#56215

Fromhughaguilar96@gmail.com
Date2017-03-23 23:07 -0700
Message-ID<b6df1cc9-be3c-4324-a523-a32fb4b63a50@googlegroups.com>
In reply to#56214
On Thursday, March 23, 2017 at 10:02:57 PM UTC-7, hughag...@gmail.com wrote:
>     first-day -                                     \ -- days           \ adjust for not starting on Sunday

That comment was incorrect. I should say: \ adjust for not starting on Monday

[toc] | [prev] | [next] | [standalone]


#56217

FromAlex <alex@rivadpm.com>
Date2017-03-24 08:46 +0000
Message-ID<ob2m7t$5s1$1@dont-email.me>
In reply to#56214
On 3/24/2017 05:02, hughaguilar96@gmail.com wrote:
>> This is peak stupid.
> I have noticed with comp.lang.forth that the surest way to kill a
> thread is to post code. Every time I write Forth code and post it,
> the thread just dies --- nobody wants to respond --- so, I'm not
> expecting a response.

The comment was directed at your asinine statement that something was 
wrong with Zeller's algorithm, but you hadn't looked at it. As I said, 
peak stupid.

As to code, you can post & have posted without comment from me. The fact 
it gets no response from anyone isn't my problem.

-- 
Alex

[toc] | [prev] | [next] | [standalone]


#56172

FromMark Wills <markwills1970@gmail.com>
Date2017-03-23 05:35 -0700
Message-ID<59b7760b-833e-4f47-a16f-3a3207af06c0@googlegroups.com>
In reply to#56153
On Thursday, 23 March 2017 02:16:27 UTC, hughag...@gmail.com  wrote:
> I glanced over your code and I was totally confused --- I don't know what you are doing --- I don't think that works.

It does work. It has had a fairly robust testing.

Here's the source where I got the explanation and the algorithm:

http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/

[toc] | [prev] | [next] | [standalone]


#56204

Fromhughaguilar96@gmail.com
Date2017-03-23 15:23 -0700
Message-ID<c229e6c4-4449-4856-b61d-01a51c68c07c@googlegroups.com>
In reply to#56172
On Thursday, March 23, 2017 at 5:35:46 AM UTC-7, Mark Wills wrote:
> On Thursday, 23 March 2017 02:16:27 UTC, hughag...@gmail.com  wrote:
> > I glanced over your code and I was totally confused --- I don't know what you are doing --- I don't think that works.
> 
> It does work. It has had a fairly robust testing.
> 
> Here's the source where I got the explanation and the algorithm:
> 
> http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/

I didn't get my algorithm from anywhere. I just figured it out myself. I won't say that I never make mistakes --- but it is very rare --- this is not an example of me making a mistake.

I don't know if you are using Zeller's Algorithm or not. I haven't really looked at your code, the website you referenced, or Zeller's Algorithm very closely. 

Here is your code slightly upgraded for testing purposes:
----------------------------------------------------------------------------
variable year 
variable month 
variable day 

create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
create centuries 4 c, 2 c, 0 c, 6 c, 

: century ( year - n ) 
    1700 /mod drop 100 / 4 mod centuries + c@ ; 

: leapYear? ( year - flag ) 
    dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ; 

: <dow> ( day month year -- n )                                     \ this was Mark's DOW
    dup century >r  100 mod year !  1- month !  day ! 
    r>  year @ dup 4 /  day @ 
    year @ leapYear? if leap-years else years then month @ + c@ 
    + + + +  7 mod ; 

: dow ( month day year -- str )                                     \ this slight upgrade provides a string
    -rot  swap  rot                 \ day month year
    <dow> days ;
    
: test-dow ( month day year -- )
    cr ." Mark: "  3dup dow     count type
    cr ." Hugh: "  day-of-week  count type ;
----------------------------------------------------------------------------

Here is a test:

2 28 1900 test-dow 
Mark: Tuesday
Hugh: Wednesday ok
3 1 1900 test-dow 
Mark: Thursday
Hugh: Thursday ok

You are assuming that 1900 was a leap year --- it isn't.

Here, once again for reference, is my code for testing if a year has an extra day or not:

: extra-day { year -- 1|0 }             \ how many extra days February gets for this year
    year 4 mod if       0 exit then
    year 100 mod if     1 exit then
    year 400 mod if     0 exit then
    1 ;

Here is a test:

1900 extra-day . 0  ok

[toc] | [prev] | [next] | [standalone]


#56230

FromMark Wills <markwills1970@gmail.com>
Date2017-03-24 09:38 -0700
Message-ID<da8de2fc-7bbe-47a7-9a1b-a94631f4cfdb@googlegroups.com>
In reply to#56204
On Thursday, 23 March 2017 22:23:32 UTC, hughag...@gmail.com  wrote:
> On Thursday, March 23, 2017 at 5:35:46 AM UTC-7, Mark Wills wrote:
> > On Thursday, 23 March 2017 02:16:27 UTC, hughag...@gmail.com  wrote:
> > > I glanced over your code and I was totally confused --- I don't know what you are doing --- I don't think that works.
> > 
> > It does work. It has had a fairly robust testing.
> > 
> > Here's the source where I got the explanation and the algorithm:
> > 
> > http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/
> 
> I didn't get my algorithm from anywhere. I just figured it out myself. I won't say that I never make mistakes --- but it is very rare --- this is not an example of me making a mistake.
> 
> I don't know if you are using Zeller's Algorithm or not. I haven't really looked at your code, the website you referenced, or Zeller's Algorithm very closely. 
> 
> Here is your code slightly upgraded for testing purposes:
> ----------------------------------------------------------------------------
> variable year 
> variable month 
> variable day 
> 
> create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
> create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
> create centuries 4 c, 2 c, 0 c, 6 c, 
> 
> : century ( year - n ) 
>     1700 /mod drop 100 / 4 mod centuries + c@ ; 
> 
> : leapYear? ( year - flag ) 
>     dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ; 
> 
> : <dow> ( day month year -- n )                                     \ this was Mark's DOW
>     dup century >r  100 mod year !  1- month !  day ! 
>     r>  year @ dup 4 /  day @ 
>     year @ leapYear? if leap-years else years then month @ + c@ 
>     + + + +  7 mod ; 
> 
> : dow ( month day year -- str )                                     \ this slight upgrade provides a string
>     -rot  swap  rot                 \ day month year
>     <dow> days ;
>     
> : test-dow ( month day year -- )
>     cr ." Mark: "  3dup dow     count type
>     cr ." Hugh: "  day-of-week  count type ;
> ----------------------------------------------------------------------------
> 
> Here is a test:
> 
> 2 28 1900 test-dow 
> Mark: Tuesday
> Hugh: Wednesday ok
> 3 1 1900 test-dow 
> Mark: Thursday
> Hugh: Thursday ok
> 
> You are assuming that 1900 was a leap year --- it isn't.
> 
> Here, once again for reference, is my code for testing if a year has an extra day or not:
> 
> : extra-day { year -- 1|0 }             \ how many extra days February gets for this year
>     year 4 mod if       0 exit then
>     year 100 mod if     1 exit then
>     year 400 mod if     0 exit then
>     1 ;
> 
> Here is a test:
> 
> 1900 extra-day . 0  ok

The problem isn't the leap year calculation. Try my leap year code above:

1900 leapyear? .
0 ok

See? It's (correctly) not a leap.

The problem (discovered by you) is the algorithm itself as published on

http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/ 

It says to use the *last two digits of the year*, so I faithfully passed in
year mod 100. That results in leapYear? receiving 0 (obviously) which is nonsense year.

I'll modify the code.

It does make one wonder about the quality of some of these descriptions on
the web.

[toc] | [prev] | [next] | [standalone]


#56231

Fromhughaguilar96@gmail.com
Date2017-03-24 10:24 -0700
Message-ID<473ab3b6-caf6-4c10-9f89-1dac115604f3@googlegroups.com>
In reply to#56230
On Friday, March 24, 2017 at 9:39:00 AM UTC-7, Mark Wills wrote:
> On Thursday, 23 March 2017 22:23:32 UTC, hughag...@gmail.com  wrote:
> > On Thursday, March 23, 2017 at 5:35:46 AM UTC-7, Mark Wills wrote:
> > > On Thursday, 23 March 2017 02:16:27 UTC, hughag...@gmail.com  wrote:
> > > > I glanced over your code and I was totally confused --- I don't know what you are doing --- I don't think that works.
> > > 
> > > It does work. It has had a fairly robust testing.
> > > 
> > > Here's the source where I got the explanation and the algorithm:
> > > 
> > > http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/
> > 
> > I didn't get my algorithm from anywhere. I just figured it out myself. I won't say that I never make mistakes --- but it is very rare --- this is not an example of me making a mistake.
> > 
> > I don't know if you are using Zeller's Algorithm or not. I haven't really looked at your code, the website you referenced, or Zeller's Algorithm very closely. 
> > 
> > Here is your code slightly upgraded for testing purposes:
> > ----------------------------------------------------------------------------
> > variable year 
> > variable month 
> > variable day 
> > 
> > create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
> > create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c, 
> > create centuries 4 c, 2 c, 0 c, 6 c, 
> > 
> > : century ( year - n ) 
> >     1700 /mod drop 100 / 4 mod centuries + c@ ; 
> > 
> > : leapYear? ( year - flag ) 
> >     dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ; 
> > 
> > : <dow> ( day month year -- n )                                     \ this was Mark's DOW
> >     dup century >r  100 mod year !  1- month !  day ! 
> >     r>  year @ dup 4 /  day @ 
> >     year @ leapYear? if leap-years else years then month @ + c@ 
> >     + + + +  7 mod ; 
> > 
> > : dow ( month day year -- str )                                     \ this slight upgrade provides a string
> >     -rot  swap  rot                 \ day month year
> >     <dow> days ;
> >     
> > : test-dow ( month day year -- )
> >     cr ." Mark: "  3dup dow     count type
> >     cr ." Hugh: "  day-of-week  count type ;
> > ----------------------------------------------------------------------------
> > 
> > Here is a test:
> > 
> > 2 28 1900 test-dow 
> > Mark: Tuesday
> > Hugh: Wednesday ok
> > 3 1 1900 test-dow 
> > Mark: Thursday
> > Hugh: Thursday ok
> > 
> > You are assuming that 1900 was a leap year --- it isn't.
> > 
> > Here, once again for reference, is my code for testing if a year has an extra day or not:
> > 
> > : extra-day { year -- 1|0 }             \ how many extra days February gets for this year
> >     year 4 mod if       0 exit then
> >     year 100 mod if     1 exit then
> >     year 400 mod if     0 exit then
> >     1 ;
> > 
> > Here is a test:
> > 
> > 1900 extra-day . 0  ok
> 
> The problem isn't the leap year calculation. Try my leap year code above:
> 
> 1900 leapyear? .
> 0 ok
> 
> See? It's (correctly) not a leap.
> 
> The problem (discovered by you) is the algorithm itself as published on
> 
> http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/ 
> 
> It says to use the *last two digits of the year*, so I faithfully passed in
> year mod 100. That results in leapYear? receiving 0 (obviously) which is nonsense year.
> 
> I'll modify the code.
> 
> It does make one wonder about the quality of some of these descriptions on
> the web.

Yes. It is a mistake for people to find algorithms and implement them without understanding how they work. The guy who said to use the "last two digits of the year" most likely found the algorithm in a math book and then posted it on the internet without understanding it, and with minimal testing.

Now I'm interested in the subject. :-) 
I'll think on your algorithm and try to figure it out. I'll also write my own code for Zeller's Algorithm and try to figure it out too. It is possible that the mathisfun guys messed up the implementation, but the algorithm actually works. They didn't provide source-code, or explain the algorithm. I don't know how math can be fun if you aren't figuring things out --- using code, or an algorithm, without understanding it is something that a trained monkey could do.

My own algorithm was just brute-force. I have to loop through the years calling EXTRA-DAY each time until I get to a known year: FIRST-YEAR. This is only realistic with a computer --- nobody would take the time to do this by hand.

On a related note, check this out: http://www.moonstick.com/

Also, I have a book on how to build a sundial. There are actually multiple ways to do this. That is another interesting math problem. Building those things could be an interesting hobby. Put one in your front yard! In my hometown I saw people put old tires painted white and filled with soil in their front yard, then grow flowers there --- a sundial would be somewhat less tacky. ;-)

Keeping track of time is assumed to be the first math problem humans worked on. People had learned arithmetic already, which they used for financial transactions (this is why all the hieroglyphs in English involve money: $ % # etc.). The first actual math involved time though. Also, this is why the five-pointed star is an ancient symbol for religion/science. This is the path that the planet Venus follows along the horizon over a period of about 40 years (the time needed for the orbits of Earth and Venus to synchronize). Venus can be seen with the naked eye. It is possible to build an observatory with stone-age tools that tracks the location of Venus in the sky.

[toc] | [prev] | [next] | [standalone]


#56235

FromCecil Bayona <cbayona@cbayona.com>
Date2017-03-24 13:32 -0500
Message-ID<ob3oik$sk4$1@dont-email.me>
In reply to#56231
On 3/24/2017 12:24 PM, hughaguilar96@gmail.com wrote:
> On Friday, March 24, 2017 at 9:39:00 AM UTC-7, Mark Wills wrote:
>> On Thursday, 23 March 2017 22:23:32 UTC, hughag...@gmail.com  wrote:
>>> On Thursday, March 23, 2017 at 5:35:46 AM UTC-7, Mark Wills wrote:
>>>> On Thursday, 23 March 2017 02:16:27 UTC, hughag...@gmail.com  wrote:
>>>>> I glanced over your code and I was totally confused --- I don't know what you are doing --- I don't think that works.
>>>>
>>>> It does work. It has had a fairly robust testing.
>>>>
>>>> Here's the source where I got the explanation and the algorithm:
>>>>
>>>> http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/
>>>
>>> I didn't get my algorithm from anywhere. I just figured it out myself. I won't say that I never make mistakes --- but it is very rare --- this is not an example of me making a mistake.
>>>
>>> I don't know if you are using Zeller's Algorithm or not. I haven't really looked at your code, the website you referenced, or Zeller's Algorithm very closely.
>>>
>>> Here is your code slightly upgraded for testing purposes:
>>> ----------------------------------------------------------------------------
>>> variable year
>>> variable month
>>> variable day
>>>
>>> create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
>>> create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
>>> create centuries 4 c, 2 c, 0 c, 6 c,
>>>
>>> : century ( year - n )
>>>     1700 /mod drop 100 / 4 mod centuries + c@ ;
>>>
>>> : leapYear? ( year - flag )
>>>     dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ;
>>>
>>> : <dow> ( day month year -- n )                                     \ this was Mark's DOW
>>>     dup century >r  100 mod year !  1- month !  day !
>>>     r>  year @ dup 4 /  day @
>>>     year @ leapYear? if leap-years else years then month @ + c@
>>>     + + + +  7 mod ;
>>>
>>> : dow ( month day year -- str )                                     \ this slight upgrade provides a string
>>>     -rot  swap  rot                 \ day month year
>>>     <dow> days ;
>>>
>>> : test-dow ( month day year -- )
>>>     cr ." Mark: "  3dup dow     count type
>>>     cr ." Hugh: "  day-of-week  count type ;
>>> ----------------------------------------------------------------------------
>>>
>>> Here is a test:
>>>
>>> 2 28 1900 test-dow
>>> Mark: Tuesday
>>> Hugh: Wednesday ok
>>> 3 1 1900 test-dow
>>> Mark: Thursday
>>> Hugh: Thursday ok
>>>
>>> You are assuming that 1900 was a leap year --- it isn't.
>>>
>>> Here, once again for reference, is my code for testing if a year has an extra day or not:
>>>
>>> : extra-day { year -- 1|0 }             \ how many extra days February gets for this year
>>>     year 4 mod if       0 exit then
>>>     year 100 mod if     1 exit then
>>>     year 400 mod if     0 exit then
>>>     1 ;
>>>
>>> Here is a test:
>>>
>>> 1900 extra-day . 0  ok
>>
>> The problem isn't the leap year calculation. Try my leap year code above:
>>
>> 1900 leapyear? .
>> 0 ok
>>
>> See? It's (correctly) not a leap.
>>
>> The problem (discovered by you) is the algorithm itself as published on
>>
>> http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/
>>
>> It says to use the *last two digits of the year*, so I faithfully passed in
>> year mod 100. That results in leapYear? receiving 0 (obviously) which is nonsense year.
>>
>> I'll modify the code.
>>
>> It does make one wonder about the quality of some of these descriptions on
>> the web.
>
> Yes. It is a mistake for people to find algorithms and implement them without understanding how they work. The guy who said to use the "last two digits of the year" most likely found the algorithm in a math book and then posted it on the internet without understanding it, and with minimal testing.
>
> Now I'm interested in the subject. :-)
> I'll think on your algorithm and try to figure it out. I'll also write my own code for Zeller's Algorithm and try to figure it out too. It is possible that the mathisfun guys messed up the implementation, but the algorithm actually works. They didn't provide source-code, or explain the algorithm. I don't know how math can be fun if you aren't figuring things out --- using code, or an algorithm, without understanding it is something that a trained monkey could do.
>
> My own algorithm was just brute-force. I have to loop through the years calling EXTRA-DAY each time until I get to a known year: FIRST-YEAR. This is only realistic with a computer --- nobody would take the time to do this by hand.
>
> On a related note, check this out: http://www.moonstick.com/
>
> Also, I have a book on how to build a sundial. There are actually multiple ways to do this. That is another interesting math problem. Building those things could be an interesting hobby. Put one in your front yard! In my hometown I saw people put old tires painted white and filled with soil in their front yard, then grow flowers there --- a sundial would be somewhat less tacky. ;-)
>
> Keeping track of time is assumed to be the first math problem humans worked on. People had learned arithmetic already, which they used for financial transactions (this is why all the hieroglyphs in English involve money: $ % # etc.). The first actual math involved time though. Also, this is why the five-pointed star is an ancient symbol for religion/science. This is the path that the planet Venus follows along the horizon over a period of about 40 years (the time needed for the orbits of Earth and Venus to synchronize). Venus can be seen with the naked eye. It is possible to build an observatory with stone-age tools that tracks the location of Venus in the sky.
>

I remember writing some routines in C to calculate the date of the week 
and other date related functions, and I did a rough approximation found 
in a book.

That was a mistake as I soon found errors, so I did a lot of digging 
into it and built routines that took all the quirks into its equation 
and was flawless for date ranges way beyond what was needed but after 
testing it I no longer had to worry about those routines and added them 
to my personal library.

I need to see if I still have a copy of the libraries since I started 
using C and C++ again after a long break, date, time, input, output, 
menu, B+ tree routines, all thoroughly tested, I know I have the Pascal 
(Delphi) version available but it would be nice to find a copy of the C 
versions.

The B Tree functions are probably the most useful, as they were tweaked 
to be very fast, people are now used to using someone else's database 
routines such as SQL but these functions blew SQL away in terms of speed.

-- 
Cecil - k5nwa

[toc] | [prev] | [next] | [standalone]


#56238

FromMark Wills <markwills1970@gmail.com>
Date2017-03-24 12:19 -0700
Message-ID<a0adfc98-1c1a-4a11-965f-82a45329f550@googlegroups.com>
In reply to#56235
On Friday, 24 March 2017 18:32:38 UTC, Cecil - k5nwa  wrote:
> On 3/24/2017 12:24 PM, hughaguilar96@gmail.com wrote:
> > On Friday, March 24, 2017 at 9:39:00 AM UTC-7, Mark Wills wrote:
> >> On Thursday, 23 March 2017 22:23:32 UTC, hughag...@gmail.com  wrote:
> >>> On Thursday, March 23, 2017 at 5:35:46 AM UTC-7, Mark Wills wrote:
> >>>> On Thursday, 23 March 2017 02:16:27 UTC, hughag...@gmail.com  wrote:
> >>>>> I glanced over your code and I was totally confused --- I don't know what you are doing --- I don't think that works.
> >>>>
> >>>> It does work. It has had a fairly robust testing.
> >>>>
> >>>> Here's the source where I got the explanation and the algorithm:
> >>>>
> >>>> http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/
> >>>
> >>> I didn't get my algorithm from anywhere. I just figured it out myself. I won't say that I never make mistakes --- but it is very rare --- this is not an example of me making a mistake.
> >>>
> >>> I don't know if you are using Zeller's Algorithm or not. I haven't really looked at your code, the website you referenced, or Zeller's Algorithm very closely.
> >>>
> >>> Here is your code slightly upgraded for testing purposes:
> >>> ----------------------------------------------------------------------------
> >>> variable year
> >>> variable month
> >>> variable day
> >>>
> >>> create years 0 c, 3 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> >>> create leap-years 6 c, 2 c, 3 c, 6 c, 1 c, 4 c, 6 c, 2 c, 5 c, 0 c, 3 c, 5 c,
> >>> create centuries 4 c, 2 c, 0 c, 6 c,
> >>>
> >>> : century ( year - n )
> >>>     1700 /mod drop 100 / 4 mod centuries + c@ ;
> >>>
> >>> : leapYear? ( year - flag )
> >>>     dup >r 400 mod 0=  r@ 100 mod 0<> or  r> 4 mod 0= and ;
> >>>
> >>> : <dow> ( day month year -- n )                                     \ this was Mark's DOW
> >>>     dup century >r  100 mod year !  1- month !  day !
> >>>     r>  year @ dup 4 /  day @
> >>>     year @ leapYear? if leap-years else years then month @ + c@
> >>>     + + + +  7 mod ;
> >>>
> >>> : dow ( month day year -- str )                                     \ this slight upgrade provides a string
> >>>     -rot  swap  rot                 \ day month year
> >>>     <dow> days ;
> >>>
> >>> : test-dow ( month day year -- )
> >>>     cr ." Mark: "  3dup dow     count type
> >>>     cr ." Hugh: "  day-of-week  count type ;
> >>> ----------------------------------------------------------------------------
> >>>
> >>> Here is a test:
> >>>
> >>> 2 28 1900 test-dow
> >>> Mark: Tuesday
> >>> Hugh: Wednesday ok
> >>> 3 1 1900 test-dow
> >>> Mark: Thursday
> >>> Hugh: Thursday ok
> >>>
> >>> You are assuming that 1900 was a leap year --- it isn't.
> >>>
> >>> Here, once again for reference, is my code for testing if a year has an extra day or not:
> >>>
> >>> : extra-day { year -- 1|0 }             \ how many extra days February gets for this year
> >>>     year 4 mod if       0 exit then
> >>>     year 100 mod if     1 exit then
> >>>     year 400 mod if     0 exit then
> >>>     1 ;
> >>>
> >>> Here is a test:
> >>>
> >>> 1900 extra-day . 0  ok
> >>
> >> The problem isn't the leap year calculation. Try my leap year code above:
> >>
> >> 1900 leapyear? .
> >> 0 ok
> >>
> >> See? It's (correctly) not a leap.
> >>
> >> The problem (discovered by you) is the algorithm itself as published on
> >>
> >> http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/
> >>
> >> It says to use the *last two digits of the year*, so I faithfully passed in
> >> year mod 100. That results in leapYear? receiving 0 (obviously) which is nonsense year.
> >>
> >> I'll modify the code.
> >>
> >> It does make one wonder about the quality of some of these descriptions on
> >> the web.
> >
> > Yes. It is a mistake for people to find algorithms and implement them without understanding how they work. The guy who said to use the "last two digits of the year" most likely found the algorithm in a math book and then posted it on the internet without understanding it, and with minimal testing.
> >
> > Now I'm interested in the subject. :-)
> > I'll think on your algorithm and try to figure it out. I'll also write my own code for Zeller's Algorithm and try to figure it out too. It is possible that the mathisfun guys messed up the implementation, but the algorithm actually works. They didn't provide source-code, or explain the algorithm. I don't know how math can be fun if you aren't figuring things out --- using code, or an algorithm, without understanding it is something that a trained monkey could do.
> >
> > My own algorithm was just brute-force. I have to loop through the years calling EXTRA-DAY each time until I get to a known year: FIRST-YEAR. This is only realistic with a computer --- nobody would take the time to do this by hand.
> >
> > On a related note, check this out: http://www.moonstick.com/
> >
> > Also, I have a book on how to build a sundial. There are actually multiple ways to do this. That is another interesting math problem. Building those things could be an interesting hobby. Put one in your front yard! In my hometown I saw people put old tires painted white and filled with soil in their front yard, then grow flowers there --- a sundial would be somewhat less tacky. ;-)
> >
> > Keeping track of time is assumed to be the first math problem humans worked on. People had learned arithmetic already, which they used for financial transactions (this is why all the hieroglyphs in English involve money: $ % # etc.). The first actual math involved time though. Also, this is why the five-pointed star is an ancient symbol for religion/science. This is the path that the planet Venus follows along the horizon over a period of about 40 years (the time needed for the orbits of Earth and Venus to synchronize). Venus can be seen with the naked eye. It is possible to build an observatory with stone-age tools that tracks the location of Venus in the sky.
> >
> 
> I remember writing some routines in C to calculate the date of the week 
> and other date related functions, and I did a rough approximation found 
> in a book.
> 
> That was a mistake as I soon found errors, so I did a lot of digging 
> into it and built routines that took all the quirks into its equation 
> and was flawless for date ranges way beyond what was needed but after 
> testing it I no longer had to worry about those routines and added them 
> to my personal library.
> 
> I need to see if I still have a copy of the libraries since I started 
> using C and C++ again after a long break, date, time, input, output, 
> menu, B+ tree routines, all thoroughly tested, I know I have the Pascal 
> (Delphi) version available but it would be nice to find a copy of the C 
> versions.
> 
> The B Tree functions are probably the most useful, as they were tweaked 
> to be very fast, people are now used to using someone else's database 
> routines such as SQL but these functions blew SQL away in terms of speed.
> 
> -- 
> Cecil - k5nwa

I'd rather see the Pascal ones than the C ones ;-)

[toc] | [prev] | [next] | [standalone]


#56243

FromCecil Bayona <cbayona@cbayona.com>
Date2017-03-24 16:01 -0500
Message-ID<ob419b$sph$1@dont-email.me>
In reply to#56238
On 3/24/2017 2:19 PM, Mark Wills wrote:
> On Friday, 24 March 2017 18:32:38 UTC, Cecil - k5nwa  wrote:
>>
>> I remember writing some routines in C to calculate the date of the week
>> and other date related functions, and I did a rough approximation found
>> in a book.
>>
>> That was a mistake as I soon found errors, so I did a lot of digging
>> into it and built routines that took all the quirks into its equation
>> and was flawless for date ranges way beyond what was needed but after
>> testing it I no longer had to worry about those routines and added them
>> to my personal library.
>>
>> I need to see if I still have a copy of the libraries since I started
>> using C and C++ again after a long break, date, time, input, output,
>> menu, B+ tree routines, all thoroughly tested, I know I have the Pascal
>> (Delphi) version available but it would be nice to find a copy of the C
>> versions.
>>
>> The B Tree functions are probably the most useful, as they were tweaked
>> to be very fast, people are now used to using someone else's database
>> routines such as SQL but these functions blew SQL away in terms of speed.
>>
>> --
>> Cecil - k5nwa
>
> I'd rather see the Pascal ones than the C ones ;-)
>
I switched from C to Borland Pascal when I was writing a large program 
for a school system and kept getting a mysterious crashes for weeks I 
could not figure it out as there was a large number of programs running. 
The error would crash the whole system so I could not tell where it was 
coming from.

I wrote a program to convert the C code to Pascal, after a couple of 
hours it would compile without errors and I had all the run time check 
options turned on to check for invalid conditions. Once I stated running 
it within hours Delphi popped several run time errors all having to do 
with pointers not quite right, after fixing those errors the code would 
not crash anymore so I stayed with Pascal for many years. That is until 
one of the buyers of Borland decided to increase the price of the 
software so much so I could not afford it anymore. The last version of 
Delphi I have is version 7 and that is pretty old but I can't afford 
their software anymore.

Too bad, as Borland Delphi is an excellent development environment for 
Windows. I thought for some of my projects to use Delphi instead of 
C/C++ but I don't have up to date software for Delphi but I do for C so 
that made the decision for me. There are some Delphi clone software 
packages such as Lazarus/Free Pascal but I have not used them so I have 
no clue how reliable they are.

I'm getting back into C due to a eForth project I'm playing with, and C 
is available for just about every CPU there is while Pascal does not 
share that capability although I do have Pascal for AVR, PIC, dsPIC, 
PIC32, ARM, and Windows.

-- 
Cecil - k5nwa

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.forth


csiph-web