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

Re: JSLint rejects (n % 1)

Expand Messages
  • 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 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?
      >

      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 2 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 3 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 4 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 5 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.