Loading ...
Sorry, an error occurred while loading the content.

Re: JSLint rejects (n % 1)

Expand Messages
  • Brook
    ... Because the answer to (n % 1) is always zero. Any number divided by 1 is itself with no remainder. Perhaps you re looking for (n % 2) in order to test
    Message 1 of 9 , Apr 27, 2011
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Martin" <mbrrtt@...> wrote:
      >
      > JSLint rejects (n % 1) or (n % 1.0), which obtains the fractional part of a nonnegative number n. ("Unexpected '1'") Anybody know why?
      >

      Because the answer to (n % 1) is always zero. Any number divided by 1 is itself with no remainder. Perhaps you're looking for (n % 2) in order to test for the odd/even nature of n?
    • Erik Eckhardt
      That was a singularly unhelpful (and possibly insulting) reply. `javascript:alert(5.2 %1);` yields 0.2 in my browser. ... [Non-text portions of this message
      Message 2 of 9 , Apr 27, 2011
      • 0 Attachment
        That was a singularly unhelpful (and possibly insulting) reply.

        `javascript:alert(5.2 %1);` yields 0.2 in my browser.

        On Wed, Apr 27, 2011 at 10:26 AM, Brook <mrprogguy@...> wrote:

        >
        >
        >
        > --- In jslint_com@yahoogroups.com, "Martin" <mbrrtt@...> wrote:
        > >
        > > JSLint rejects (n % 1) or (n % 1.0), which obtains the fractional part of
        > a nonnegative number n. ("Unexpected '1'") Anybody know why?
        > >
        >
        > Because the answer to (n % 1) is always zero. Any number divided by 1 is
        > itself with no remainder. Perhaps you're looking for (n % 2) in order to
        > test for the odd/even nature of n?
        >
        >
        >


        [Non-text portions of this message have been removed]
      • mathew
        ... More importantly, it was incorrect. ECMA-262 says: If the dividend is a zero and the divisor is finite, the result is the same as the dividend. In the
        Message 3 of 9 , Apr 27, 2011
        • 0 Attachment
          On Wed, Apr 27, 2011 at 13:41, Erik Eckhardt <erik@...> wrote:
          > That was a singularly unhelpful (and possibly insulting) reply.

          More importantly, it was incorrect. ECMA-262 says:

          "If the dividend is a zero and the divisor is finite, the result is
          the same as the dividend.
          In the remaining cases, where neither an infinity, nor a zero, nor NaN
          is involved, the
          floating-point remainder r from a dividend n and a divisor d is
          defined by the mathematical
          relation r = n − (d * q) where q is an integer that is negative only
          if n/d is negative and
          positive only if n/d is positive, and whose magnitude is as large as
          possible without
          exceeding the magnitude of the true mathematical quotient of n and d."

          So r = n - (d * q)
          but we know d = 1, so r = n - q where q is an integer as big as it can
          be without exceeding n.

          Hence r = n - Math.floor(n). Which is what I'd write, because I hate
          'clever' code.


          mathew
          --
          <URL:http://www.pobox.com/~meta/>
        • Merlin
          ... My guess would be that JSLint treats (n % 1) in the same way that it treats (n * 1) and (n / 1). Perhaps it should not, since (n % 1) can return a value
          Message 4 of 9 , Apr 27, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Martin" <mbrrtt@...> wrote:
            >
            > JSLint rejects (n % 1) or (n % 1.0), which obtains the fractional part of a nonnegative number n. ("Unexpected '1'") Anybody know why?
            >

            My guess would be that JSLint treats (n % 1) in the same way that it treats (n * 1) and (n / 1).

            Perhaps it should not, since (n % 1) can return a value that is not the same as n for both positive and negative values of n.

            (n % 1) can, of course, b written as n - Math.floor(n) for n >= 0, and as n + Math.floor(-n) for n <0.
          • Marcel Duran
            The same happens to infinity operations in the form of n / 0: 1 / 0 === Infinity // true JSLint does not tolerate this kind of operation even though ECMA-262
            Message 5 of 9 , Apr 27, 2011
            • 0 Attachment
              The same happens to infinity operations in the form of n / 0:

              1 / 0 === Infinity // true

              JSLint does not tolerate this kind of operation even though ECMA-262 11.5.2
              says:

              "Division of a non-zero finite value by a zero results in a signed infinity.
              ..."

              In this case I'm with JSLint: Infinity keyword is clearer than 1 / 0

              Marcel

              On Wed, Apr 27, 2011 at 1:05 PM, Merlin <g7awz@...> wrote:

              >
              >
              > --- In jslint_com@yahoogroups.com, "Martin" <mbrrtt@...> wrote:
              > >
              > > JSLint rejects (n % 1) or (n % 1.0), which obtains the fractional part of
              > a nonnegative number n. ("Unexpected '1'") Anybody know why?
              > >
              >
              > My guess would be that JSLint treats (n % 1) in the same way that it treats
              > (n * 1) and (n / 1).
              >
              > Perhaps it should not, since (n % 1) can return a value that is not the
              > same as n for both positive and negative values of n.
              >
              > (n % 1) can, of course, b written as n - Math.floor(n) for n >= 0, and as n
              > + Math.floor(-n) for n <0.
              >
              >
              >



              --
              Marcel Duran


              [Non-text portions of this message have been removed]
            • Cheney, Edward A SSG RES USAR USARC
              Classification: UNCLASSIFIED Would it be possible to get this following list of names added to the assume a browser option: * Storage * localStorage *
              Message 6 of 9 , Apr 28, 2011
              • 0 Attachment
                Classification: UNCLASSIFIED
                Would it be possible to get this following list of names added to the "assume a browser" option:

                * Storage
                * localStorage
                * sessionStorage
                * globalStorage

                Thanks,

                Austin Cheney, CISSP
                http://prettydiff.com/
                http://prettydiff.com/beta
                Classification: UNCLASSIFIED
              • Martin
                ... Do you really think that n % 1 is clever code , while n - Math.floor(n) is not? Sure, the operator % doesn t wear its meaning on its face. It s not
                Message 7 of 9 , Apr 28, 2011
                • 0 Attachment
                  --- In jslint_com@yahoogroups.com, "Merlin" <g7awz@...> wrote:
                  >
                  > Perhaps it should not, since (n % 1) can return a value that is not the same as n for both positive and negative values of n.
                  >
                  > (n % 1) can, of course, b written as n - Math.floor(n) for n >= 0, and as n + Math.floor(-n) for n <0.
                  >

                  --- In jslint_com@yahoogroups.com, mathew <meta404@...> wrote:
                  >
                  > Hence r = n - Math.floor(n). Which is what I'd write, because I hate
                  > 'clever' code.
                  >
                  >

                  Do you really think that 'n % 1' is 'clever code', while 'n - Math.floor(n)' is not?

                  Sure, the operator '%' doesn't wear its meaning on its face. It's not standard mathematical notation; you have to look it up the first time you see it, in a book or in the spec. But did you know what 'x || y' or 'x ? y : z' meant the first time you saw them?

                  '%' is in the language and straightforwardly computes what it is supposed to compute. n % 1 returns the fractional part of a nonnegative number. Perhaps -n % 1 (when n > 0) is tricky, but I don't need it and wouldn't use it, anymore than I'd use Math.sqrt(-n) to get NaN.

                  > My guess would be that JSLint treats (n % 1) in the same way that it treats (n * 1) and (n / 1).
                  >

                  I think this is may be the real reason why JSLint complains. As such, it is just a mistake: a bug in JSLint, not a feature. I would hope that Douglas Crockford fixes it, or else comes up with a much better reason why not.
                • mathew
                  ... Yes. The meaning of n - Math.floor(n) is obvious. The meaning of n % 1 is not obvious, so much so that at least one person on this mailing list
                  Message 8 of 9 , Apr 28, 2011
                  • 0 Attachment
                    On Thu, Apr 28, 2011 at 07:46, Martin <mbrrtt@...> wrote:

                    > Do you really think that 'n % 1' is 'clever code', while 'n -
                    > Math.floor(n)' is not?
                    >

                    Yes. The meaning of 'n - Math.floor(n)' is obvious. The meaning of 'n % 1'
                    is not obvious, so much so that at least one person on this mailing list
                    misunderstood it.

                    > Sure, the operator '%' doesn't wear its meaning on its face. It's not
                    > standard mathematical notation; you have to look it up the first time you
                    > see it, in a book or in the spec. But did you know what 'x || y' or 'x ? y :
                    > z' meant the first time you saw them?
                    >
                    The problem isn't the meaning of '%'; I knew that % was remainder/modulo.
                    The problem is that extending modulo to floating point gives you at least
                    three different possible behaviors, as
                    http://en.wikipedia.org/wiki/Modulo_operation points out, and people's
                    expectations aren't necessarily met by those extended definitions.


                    mathew
                    --
                    <URL:http://www.pobox.com/~meta/>


                    [Non-text portions of this message have been removed]
                  Your message has been successfully submitted and would be delivered to recipients shortly.