firefox global variables woes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    #16
    Re: firefox global variables woes

    JRS: In article <1108071509.672 729.193150@o13g 2000cwo.googleg roups.com>
    , dated Thu, 10 Feb 2005 13:38:29, seen in news:comp.lang. javascript, rh
    <codeHanger@yah oo.ca> posted :[color=blue]
    >Mick White wrote:[/color]
    [color=blue][color=green]
    >> Why not hard code it?
    >> function GMLen(m,yyyy){//m 0-11
    >> return [31,yy%4==0?new
    >> Date(yyyy,m+1,0 ).getDate():28, 31,30,31,30,31, 31,30,31,30,31][m]
    >> }[/color][/color]
    [color=blue]
    >First of all, what you've presented doesn't fit my definition of "hard
    >code". Secondly, it wouldn't appear to have been tested. And thirdly,
    >it doesn't account for leap years properly.[/color]

    It has a couple of missing 'y's; but, with that fixed, it handles
    Gregorian leap year properly AFAICS.

    [color=blue]
    >This is a case where efficiency happened to be predominant in the
    >choice, particularly given the context in which the response was made.[/color]

    ISTM that the above constructs a whole 12-element array whenever called,
    including the calculation of an element not wanted 11 times out of 12.

    In expression yyyy%4==0?new Date(yyyy,m+1,0 ).getDate():28 the %4 does
    optimise 3 out of 4 years; but

    Y%4 != 0 ? 28 : Y%100 !=0 ? 29 : Y%400 != 0 ? 28 : 29

    seems easy enough and must be cheaper than a Date Object.


    For aesthetic but presumably inefficient expressions,

    28 + (y%4==0) ^ (y%100==0) ^ (y%400==0)
    or 28 + (!(y%4)) ^ (!(y%100)) ^ (!(y%400))
    or 28 | !(y%4)^!(y%100) ^!(y%400)


    But I was asking whether = 30 + ((3*m+1)%5 < 3) could be improved,
    not whether any better routines existed; in other words, I was after
    something generating the lengths arithmetically, as Zeller would.

    That = 30 | (m>>3^m) is indeed good, covering 1,3-12 as it does.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

    Comment

    • Dr John Stockton

      #17
      Re: firefox global variables woes

      JRS: In article <k_8Pd.23$vK5.7 @twister.nyroc. rr.com>, dated Fri, 11
      Feb 2005 20:56:48, seen in news:comp.lang. javascript, Mick White
      <mwhite13BOGUS@ rochester.rr.co m> posted :[color=blue]
      >rh wrote:
      >[snip]
      >need only to calculate February length since it is the only[color=green][color=darkred]
      >>>month whose length varies, in that sense 11/12 may be hard coded.
      >>>Semantics.[/color]
      >>
      >>
      >> Quite right, a more patient response would have been appropriate -- you
      >> have my apology.[/color]
      >
      >No problem
      >y=2004
      >feblen=[28,29][+(new Date(y,1,29).ge tMonth()==1)][/color]

      feblen = new Date(y, 2, 0).getDate()

      should beat that; moreover, it works for all values of 2.

      It might not work in NS4, in which case

      feblen = new Date(y, m+1, 1, -9).getDate()

      could be better.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
      Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links;
      some Astro stuff via astro.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
      No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

      Comment

      • Dr John Stockton

        #18
        Re: firefox global variables woes

        JRS: In article <1108063610.935 776.282760@g14g 2000cwa.googleg roups.com>
        , dated Thu, 10 Feb 2005 11:26:50, seen in news:comp.lang. javascript, rh
        <codeHanger@yah oo.ca> posted :[color=blue]
        >
        > function GMLen3() { var A = [], m, y=2005
        > for (var m=1; m<13; m++) A[m] = 29 + (m>>3^m&1)
        > + (m != 2 || -(y&3 || y&15 && !(y%25)));
        > document.write( "<P>"+A) }[/color]

        Gives 27 for Feb 2006.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
        Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
        Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
        Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

        Comment

        • rh

          #19
          Re: firefox global variables woes

          Dr John Stockton wrote:[color=blue]
          > JRS: In article[/color]
          <1108063610.935 776.282760@g14g 2000cwa.googleg roups.com>[color=blue]
          > , dated Thu, 10 Feb 2005 11:26:50, seen in news:comp.lang. javascript,[/color]
          rh[color=blue]
          > <codeHanger@yah oo.ca> posted :[color=green]
          > >
          > > function GMLen3() { var A = [], m, y=2005
          > > for (var m=1; m<13; m++) A[m] = 29 + (m>>3^m&1)
          > > + (m != 2 || -(y&3 || y&15 && !(y%25)));
          > > document.write( "<P>"+A) }[/color]
          >
          > Gives 27 for Feb 2006.
          >[/color]

          And so it does. It appears that the coercion to Boolean got dropped
          when introducing the subtraction. The continued line would better read:

          + (m != 2 || -!!(y&3 || y&15 && !(y%25)));

          However, the expression I prefer is the subsequent, with range m=
          0...11:

          daysInMo = 28 | ( m^1 && ++m>>3^m|2 || !(y&3 || y&15 && !(y%25)) );

          or for range m= 1...12:

          daysInMo = 28 | ( m^2 && m>>3^m|2 || !(y&3 || y&15 && !(y%25)) );

          which is leaves the % as the single dependence on direct arithimetic
          operation.

          By the way, you may also recall that Lasse provided a concise
          expression that used a shift and mask in a pre-coded integer to
          effectively do the lookup for the tail-length of non-February months.

          <url:http://groups-beta.google.com/group/comp.lang.javas cript/browse_frm/thread/ce9c9637ac5e203 4/9f4c440664c393f f>

          Regards,

          ../rh

          Comment

          • Dr John Stockton

            #20
            Re: firefox global variables woes

            JRS: In article <1108260386.674 391.51880@f14g2 000cwb.googlegr oups.com>,
            dated Sat, 12 Feb 2005 18:06:26, seen in news:comp.lang. javascript, rh
            <codeHanger@yah oo.ca> posted :
            [color=blue]
            >However, the expression I prefer is the subsequent, with range m=
            >0...11:
            >
            >daysInMo = 28 | ( m^1 && ++m>>3^m|2 || !(y&3 || y&15 && !(y%25)) );
            >
            >or for range m= 1...12:
            >
            >daysInMo = 28 | ( m^2 && m>>3^m|2 || !(y&3 || y&15 && !(y%25)) );
            >
            >which is leaves the % as the single dependence on direct arithimetic
            >operation.[/color]


            It would be more readable, and might be better, to work along the lines
            of (/* ++ */ implies conversion of range 1..12 to 0..11)

            daysInMo = /* ++ */ m==2 ? expression(y) : expression(m)

            ++m==2 ? 28 + !(y&3 || y&15 && !(y%25)) : 28 | m>>3^m|2 }
            or 28 | ( ++m==2 ? !(y&3 || y&15 && !(y%25)) : m>>3^m|2 ) }

            which seem slightly faster than

            28 | ( m^1 && ++m>>3^m|2 || !(y&3 || y&15 && !(y%25)) )

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
            Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
            PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
            Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

            Comment

            • rh

              #21
              Re: firefox global variables woes

              Dr John Stockton wrote:[color=blue]
              > JRS: In article[/color]
              <1108260386.674 391.51880@f14g2 000cwb.googlegr oups.com>,[color=blue]
              > dated Sat, 12 Feb 2005 18:06:26, seen in news:comp.lang. javascript,[/color]
              rh[color=blue]
              > <codeHanger@yah oo.ca> posted :
              >[color=green]
              > >However, the expression I prefer is the subsequent, with range m=
              > >0...11:
              > >
              > >daysInMo = 28 | ( m^1 && ++m>>3^m|2 || !(y&3 || y&15 && !(y%25)) );
              > >
              > >or for range m= 1...12:
              > >
              > >daysInMo = 28 | ( m^2 && m>>3^m|2 || !(y&3 || y&15 && !(y%25)) );
              > >
              > >which is leaves the % as the single dependence on direct arithimetic
              > >operation.[/color]
              >
              >
              > It would be more readable, and might be better, to work along the[/color]
              lines[color=blue]
              > of (/* ++ */ implies conversion of range 1..12 to 0..11)
              >
              > daysInMo = /* ++ */ m==2 ? expression(y) : expression(m)
              >
              > ++m==2 ? 28 + !(y&3 || y&15 && !(y%25)) : 28 | m>>3^m|2 }
              > or 28 | ( ++m==2 ? !(y&3 || y&15 && !(y%25)) : m>>3^m|2 ) }
              >
              > which seem slightly faster than
              >
              > 28 | ( m^1 && ++m>>3^m|2 || !(y&3 || y&15 && !(y%25)) )
              >[/color]

              Right, but if utmost speed is the predominant criteria, then Lasse's
              method is really the one of choice.

              Also, in terms of speed of the above, I believe I can provide another
              incarnation which brings a similar marginal speed advantage over the
              tertiary form, and provides a clearer separation of the Feb
              calculation.

              But it all depends on what the goal is. Mine, in this case, happened to
              be to achieve a succinct, efficient computation by taking an
              interesting departure from the usual arithmetical approach in producing
              a result. And I think it's good that some may wonder how that somewhat
              obscure expression can possibly provide the correct result. Those
              wishing to find out, aren't going to get much of a leg up with it
              converted to tertiary form, which necessarily removes some of the
              intentional bit mangling shortcut logic.

              Your criteria may, and quite likely will, differ.

              ../rh

              Comment

              • Dr John Stockton

                #22
                Re: firefox global variables woes

                JRS: In article <1108355810.693 181.267310@c13g 2000cwb.googleg roups.com>
                , dated Sun, 13 Feb 2005 20:36:50, seen in news:comp.lang. javascript, rh
                <codeHanger@yah oo.ca> posted :[color=blue]
                >
                >Right, but if utmost speed is the predominant criteria, then Lasse's
                >method is really the one of choice.[/color]

                I've lost track of that one; is it the one using something like

                (0xeefbb3>>(2*m ))&3

                for the lengths of the non-Feb months?

                My present implementation of that seems of similar speed to the best of
                the others; but my simple testing is better on the slower routines of a
                set than on the faster ones.

                [color=blue]
                >Also, in terms of speed of the above, I believe I can provide another
                >incarnation which brings a similar marginal speed advantage over the
                >tertiary form, and provides a clearer separation of the Feb
                >calculation.
                >
                >But it all depends on what the goal is. Mine, in this case, happened to
                >be to achieve a succinct, efficient computation by taking an
                >interesting departure from the usual arithmetical approach in producing
                >a result. And I think it's good that some may wonder how that somewhat
                >obscure expression can possibly provide the correct result. Those
                >wishing to find out, aren't going to get much of a leg up with it
                >converted to tertiary form, which necessarily removes some of the
                >intentional bit mangling shortcut logic.[/color]

                Well, I have two distinct interests.

                One is to find the fastest reasonably-short and compact javascript
                implementation - for example, the following may be fast but is not
                necessarily compact in use of storage :-

                function SomeOtherRoutin e(y, m) { var x = y + '' + m // Demo only
                alert(x)
                return x }

                A = []

                function ML(y, m) { var T, U
                if (!(T=A[y])) A[y] = T = []
                if (!(U=T[m])) T[m] = U = SomeOtherRoutin e(y, m)
                return U }

                x = [ML(2, 4), ML(2, 4), ML(3, 5), ML(2, 4)]

                which Alerts only twice but sets x to [24,24,35,24]

                so clearly if SomeOtherRoutin e (a stand-in for DaysinMonth) was lengthy
                in execution in comparison with failing the two IF conditions AND if ML
                were called repeatedly for a limited number of parameters, them ML could
                beat direct use of SomeOtherRoutin e. But for many combinations of y & m
                with little repetition, ML is a waste of both time and space.


                The other, in this specific case, is to find the "algebraica lly-best"
                routine that uses the approximate numerical regularity of the months
                (like Zeller) rather than doing bit-juggling.




                I have found a published efficient-looking routine to get Week Number
                (ISO 8601) from CJD (days from Julian BC 4713-01-01 00:00:00h local
                time) by arithmetic without going via explicit YMD; getting day-of-week
                is easy (indeed, that day was a Monday), and I have bodged Year. Has
                anyone seen a routine of similar nature to do the reverse, YWD to CJD
                (not necessarily in javascript)?

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • rh

                  #23
                  Re: firefox global variables woes

                  Dr John Stockton wrote:[color=blue]
                  > JRS: In article[/color]
                  <1108355810.693 181.267310@c13g 2000cwb.googleg roups.com>[color=blue]
                  > , dated Sun, 13 Feb 2005 20:36:50, seen in news:comp.lang. javascript,[/color]
                  rh[color=blue]
                  > <codeHanger@yah oo.ca> posted :[color=green]
                  > >
                  > >Right, but if utmost speed is the predominant criteria, then Lasse's
                  > >method is really the one of choice.[/color]
                  >
                  > I've lost track of that one; is it the one using something like
                  >
                  > (0xeefbb3>>(2*m ))&3
                  >
                  > for the lengths of the non-Feb months?
                  >[/color]

                  That's right, except his implementation used a shift to accomplish the
                  multiplication by 2. However, the change doesn't appear to affect the
                  speed significantly.
                  [color=blue]
                  > My present implementation of that seems of similar speed to the best[/color]
                  of[color=blue]
                  > the others; but my simple testing is better on the slower routines of[/color]
                  a[color=blue]
                  > set than on the faster ones.
                  >[/color]

                  My timing tests, adjusting for the loop overhead, show Lasse's version
                  to be almost twice the speed of the others.

                  <...>
                  [color=blue]
                  >
                  > Well, I have two distinct interests.
                  >
                  > One is to find the fastest reasonably-short and compact javascript
                  > implementation - for example, the following may be fast but is not
                  > necessarily compact in use of storage :-
                  >
                  > function SomeOtherRoutin e(y, m) { var x = y + '' + m // Demo only
                  > alert(x)
                  > return x }
                  >
                  > A = []
                  >
                  > function ML(y, m) { var T, U
                  > if (!(T=A[y])) A[y] = T = []
                  > if (!(U=T[m])) T[m] = U = SomeOtherRoutin e(y, m)
                  > return U }
                  >
                  > x = [ML(2, 4), ML(2, 4), ML(3, 5), ML(2, 4)]
                  >
                  > which Alerts only twice but sets x to [24,24,35,24]
                  >
                  > so clearly if SomeOtherRoutin e (a stand-in for DaysinMonth) was[/color]
                  lengthy[color=blue]
                  > in execution in comparison with failing the two IF conditions AND if[/color]
                  ML[color=blue]
                  > were called repeatedly for a limited number of parameters, them ML[/color]
                  could[color=blue]
                  > beat direct use of SomeOtherRoutin e. But for many combinations of y[/color]
                  & m[color=blue]
                  > with little repetition, ML is a waste of both time and space.
                  >[/color]

                  As you say, that depends entirely on how time consuming
                  someOtherRoutin e is. In this specific case, the computation is highly
                  efficient, and re-computation would surely be more appropriate, both in
                  time and space, than caching values.
                  [color=blue]
                  > The other, in this specific case, is to find the "algebraica lly-best"
                  > routine that uses the approximate numerical regularity of the months
                  > (like Zeller) rather than doing bit-juggling.
                  >[/color]

                  That sounds like fun, but I think I'll content myself with bit-munging
                  for the present. ;-)

                  <...>

                  ../rh

                  Comment

                  Working...