isInteger, isFloat functions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FredOz

    #31
    Re: isInteger, isFloat functions?

    In article <BD75213E.21826 %phil.ronanzzz@ virgin.net>,
    phil.ronanzzz@v irgin.net says...[color=blue]
    > On 20/9/04 9:29 pm, Fred Oz wrote:
    >[color=green]
    > > Philip Ronan wrote:[/color]
    >
    > Sorry, I shouldn't have mentioned division by zero in the first place.
    >[/color]

    Don't be sorry, the discussion was very useful (to me, anyway).
    [color=blue]
    > What I meant to refer to is the Javascript (ECMA-262) entity called
    > "Infinity". Any number greater than (roughly?) 10^308 equates to "Infinity"
    > in Javascript.
    >[/color]

    Spot on.
    [color=blue]
    > Let's get back to the original question:
    >
    > If I entered this into a text box:
    > "9999999---99999.9" (replace "---" with 300 "9"s)
    > then it would fail this test for integers:
    > alert(!/\./.test(+t) && +t==t)
    >
    > OK now?
    >[/color]

    Yes. Incidentally, the OP has not followed up ... hopefully something
    here was of use?


    Comment

    • Andrew Thompson

      #32
      Re: isInteger, isFloat functions?

      On Mon, 20 Sep 2004 22:39:22 +0200, Lasse Reichstein Nielsen wrote:
      [color=blue]
      > /L 'and 1/-0 == -Infinity, even though 0 == -0 ... just don't think about it:)'[/color]

      What I was about to add in my 1st post was that
      the 'simple'situati ons are obvious (to me), but
      the others make my head hurt, so I add checks to
      avoid them! ;-)

      --
      Andrew Thompson
      http://www.PhySci.org/codes/ Web & IT Help
      http://www.PhySci.org/ Open-source software suite
      http://www.1point1C.org/ Science & Technology
      http://www.lensescapes.com/ Images that escape the mundane

      Comment

      • Grant Wagner

        #33
        Re: isInteger, isFloat functions?

        Fred Oz wrote:
        [color=blue]
        > Philip Ronan wrote:
        >[color=green]
        > > We are discussing JAVASCRIPT here, right?[/color]
        >
        > Yes. And like all good programmers, if there is *any* chance
        > that your algorithm may result in division by zero, you
        > absolutely must test for it and avoid it.[/color]

        Agreed.
        [color=blue]
        > As Andrew pointed out, 3 browsers gave 3 different (likely fully
        > ECMA-262 compliant) results. So if you don't test for division
        > by zero in the first place, you had better have done it afterward
        > because now you must deal with a bigger problem - infinity![/color]

        Andrew pointed out that 3 browsers gave consistent results. His
        original message contained:

        ---

        All three of IE 6.0026, Moz. 1.7 and Opera 6.54 (editor: probably
        meant 7.54, but I tested 6.05 as well and it was the same) give
        consistent results for me..

        javascript:aler t(1/0) -> 'Infinity'
        javascript:aler t(0/0) -> 'NaN'
        javascript:aler t(-1/0) -> '-Infinity'

        ---

        I have to disagree that "Infinity" is a bigger problem. The behaviour
        of Number.POSITIVE _INFINITY (and NEGATIVE_INFINI TY) are clearly
        documented and if the ECMAScript is following the implementation, the
        results will be the same regardless of the environment:

        <url:

        />


        So you can catch division by zero with:

        var result = (nominator + Number.MIN_VALU E) / denominator;
        if (Math.abs(resul t) == Number.POSITIVE _INFINITY) { ....


        Number.MIN_VALU E should not affect the result too terribly much, and
        it prevents 0/0 == NaN, 0/0 now becomes Infinity as well. Of course,
        identifying and dealing with demoninator == 0 would be better, but
        allowing division by zero in JavaScript is no where near as fatal as
        it can be in other languages.

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq

        Comment

        • Andrew Thompson

          #34
          Re: isInteger, isFloat functions?

          On Tue, 21 Sep 2004 13:34:05 GMT, Grant Wagner wrote:

          (Browser tesing - versions)[color=blue]
          > Opera 6.54 (editor: probably
          > meant 7.54, ..[/color]

          Correct, thank you. ( And I *was* doing so
          well in being precise in that answer.. ;-)

          Comment

          • Dr John Stockton

            #35
            Re: isInteger, isFloat functions?

            JRS: In article <isa8oeo9.fsf@h otpop.com>, dated Mon, 20 Sep 2004
            22:42:30, seen in news:comp.lang. javascript, Lasse Reichstein Nielsen
            <lrn@hotpop.com > posted :[color=blue]
            >"Evertjan." <exjxw.hannivoo rt@interxnl.net > writes:
            >[color=green]
            >> I don't think infinity is an integer but I wouldn't mind if it is.[/color]
            >
            >It is a number in Javascript. Whether it is an integer, I won't
            >begin to think about. Hmm. Math.floor(Infi nity) == Infinity
            >Nah.
            >[color=green]
            >> (+t+1)!=+t ?[/color]
            >
            >You don't need infinity for that in IEEE-754 floating point numbers.
            >As soon as the numbers becomes so large, that the precission is less
            >than 1, then t+1 cannot be represented and the result is the same as
            >t.
            >
            >Example:
            >---
            > var t = Math.pow(2,53);
            > if (t+1 == t) { alert("surprice "); }
            >---[/color]

            But try it with var t = - Math.pow(2,53)
            var t = - Math.pow(2,53) - 66666

            ***

            The OP needs to be aware that, in current javascript, all Numbers are
            floating-point binary IEEE-754 doubles.

            --
            © 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; quotes.htm; pascal.htm; &c, &c.
            No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

            Comment

            • Michael Winter

              #36
              [Correction] Re: isInteger, isFloat functions?

              On Mon, 20 Sep 2004 14:05:10 GMT, Michael Winter
              <M.Winter@bluey onder.co.invali d> wrote:

              [snip]
              [color=blue]
              > /^(0|[1-9])\d*$/[/color]

              This should have actually been

              /^(0|[1-9]\d*)$/

              Where this same pattern, (0|[1-9])\d*, occurs in other expressions (almost
              all), it should be replaced with (0|[1-9]\d*).

              [snip]

              Mike

              --
              Michael Winter
              Replace ".invalid" with ".uk" to reply by e-mail.

              Comment

              Working...