Equality...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ataru@nospam.cyberspace.org

    Equality...

    From the FAQ:

    "Non-integer results should not normally be compared for equality."

    Now, I understand why this ends up being true, but then what is the workaround
    should one actually desire to see whether 1.015 * 5 == 5.075? I'm not saying
    I have any desire to do this (I'd use C if I did, lol...), but really, it
    seems like a cheap hack to me... unless I'm missing something...

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cybers pace.org |
  • Richard Cornford

    #2
    Re: Equality...

    <ataru@nospam.c yberspace.org> wrote in message
    news:bi0kkm$h0p $1@chessie.cirr .com...[color=blue]
    >From the FAQ:
    >
    >"Non-integer results should not normally be compared for equality."
    >
    >Now, I understand why this ends up being true, but then what is
    >the workaround should one actually desire to see whether
    >1.015 * 5 == 5.075? I'm not saying I have any desire to do this
    >(I'd use C if I did, lol...), but really, it seems like a cheap
    >hack to me... unless I'm missing something...[/color]

    What seems like a cheap hack? If you want to know whether the IEEE
    double precision floating point representation of 1.015 multiplied by
    the IEEE double precision floating point representation of 5 equals the
    IEEE double precision floating point representation of 5.075 then
    JavaScript will tell you that, as specified. The result will be
    "correct" it just may not be a useful question to ask.

    Richard.


    Comment

    • ataru@nospam.cyberspace.org

      #3
      Re: Equality...

      Richard Cornford <Richard@litote s.demon.co.uk> broke the eternal silence and spoke thus:
      [color=blue]
      > it just may not be a useful question to ask.[/color]

      denseness alert! -> In what way is it not useful? I guess specifically, how
      is it different from asking the same question in, say, C?

      --
      Christopher Benson-Manica | Jumonji giri, for honour.
      ataru(at)cybers pace.org |

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Equality...

        ataru@nospam.cy berspace.org writes:
        [color=blue]
        > denseness alert! -> In what way is it not useful?[/color]

        If the answer to the question "5.0 * 1.015 == 5.075" is "false",
        how much wiser are you?

        As Richard Cornford said, you know that the result of using IEEE
        double precission floating point multiplication on the IEEE double
        precission floating point representations of 5.0 and 1.015, is not the
        same as the IEEE yadda yadda representation of 5.075.

        And then what?
        [color=blue]
        > I guess specifically, how is it different from asking the same
        > question in, say, C?[/color]

        It's not. The problem is the same in any language using inexact
        representation of numbers (i.e., all languages).

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Richard Cornford

          #5
          Re: Equality...

          <ataru@nospam.c yberspace.org> wrote in message
          news:bi10o2$hsk $1@chessie.cirr .com...[color=blue]
          >Richard Cornford <Richard@litote s.demon.co.uk> broke the eternal[/color]
          silence and spoke thus:[color=blue]
          >[color=green]
          >> it just may not be a useful question to ask.[/color]
          >
          >denseness alert! -> In what way is it not useful? I guess
          >specifically , how is it different from asking the same
          >question in, say, C?[/color]

          It is not useful in the sense that - (1.015 * 5 == 5.075) - is false
          because all of the numbers involved are IEEE double precision
          representations of the numbers in the code and as a result are usually
          (close) approximations of those numbers. You could consider yourself
          lucky if multiplying one approximation by another comes up with the same
          number as a third approximation even when that would be the result of
          performing the multiplication and comparison on the original numbers.
          JavaScript numbers are so precise that they don't have to differ by much
          to be not identical.

          If you asked the *same* question in C (using IEEE double precision
          floats) you would get the same answer, so the question is exactly as
          useful.

          It is not a problem unique to computer representations of floating point
          numbers. Consider a decimal fraction representing one third - 0.3333 -,
          does 0.3333 * 3 == 1 -? But in base 3 one third could be represented
          precisely (0.1 * 10 == 1).

          You still haven't said what precisely it is you consider a hack.

          Richard.


          Comment

          • Grant Wagner

            #6
            Re: Equality...

            ataru@nospam.cy berspace.org wrote:
            [color=blue]
            > From the FAQ:
            >
            > "Non-integer results should not normally be compared for equality."
            >
            > Now, I understand why this ends up being true, but then what is the workaround
            > should one actually desire to see whether 1.015 * 5 == 5.075? I'm not saying
            > I have any desire to do this (I'd use C if I did, lol...), but really, it
            > seems like a cheap hack to me... unless I'm missing something...[/color]

            Design a BigDecimal (or similar) class which gives you exact precision to some
            arbitrary number of decimal points. <url:
            http://java.sun.com/j2se/1.4.2/docs/...igDecimal.html /> gives you
            documentation for the types of API calls you'll need in such a class. Things like
            ..compareTo(Big Decimal val), .divide(BigDeci mal val), .multiply(BigDe cimal val)

            The actual Java source code might be extremely valuable in implementing these
            methods, although because it's inherited from java.lang.Numbe r, it might be
            slightly more difficult then copying and pasting the Java code into JavaScript and
            modifying the syntax

            Anyway, assuming you wrote or found a BigDecimal class somewhere, the answer to
            your question would be (in Java, the JavaScript equivalent would be very similar):

            double one = 1.015;
            double two = 5.0;
            double result = 5.075;

            new BigDecimal(Stri ng.valueOf(one) ).multiply(new
            BigDecimal(Stri ng.valueOf(two) )).compareTo(ne w BigDecimal(Stri ng.valueOf(resu lt)))
            == 0


            There are times when you wish to perform exact arithmetic operations on floating
            point values (such as financial calculations), however in those cases, its usually
            best to store the values as longs (integer) values and simply convert the final
            result:

            var one = 10150;
            var two = 50000;
            var result = 50750;

            function unpow() {
            var power = arguments[arguments.lengt h - 1];
            var total = 1;

            for (var i = arguments.lengt h - 2; i >= 0; --i) {
            total *= arguments[i];
            }

            return total / Math.pow(Math.p ow(10, power), arguments.lengt h - 1);
            }

            document.write( unpow(one, two, 4) == unpow(result, 4));

            --
            | Grant Wagner <gwagner@agrico reunited.com>

            * Client-side Javascript and Netscape 4 DOM Reference available at:
            *


            * Internet Explorer DOM Reference available at:
            *
            Gain technical skills through documentation and training, earn certifications and connect with the community


            * Netscape 6/7 DOM Reference available at:
            * http://www.mozilla.org/docs/dom/domref/
            * Tips for upgrading JavaScript for Netscape 7 / Mozilla
            * http://www.mozilla.org/docs/web-deve...upgrade_2.html


            Comment

            • Jim Ley

              #7
              Re: Equality...

              On Thu, 21 Aug 2003 01:15:07 +0100, "Richard Cornford"
              <Richard@litote s.demon.co.uk> wrote:
              [color=blue]
              >If you asked the *same* question in C (using IEEE double precision
              >floats) you would get the same answer, so the question is exactly as
              >useful.[/color]

              Ed. 4 a long time ago back when the minutes of meetings etc. was
              public were talking about switching to decimal arithmetic, if Ed. 4
              comes out ever (some rumour about 1st qtr 2004) then we might see it.
              That would unlikely be relevant to client-side code for years though.

              Jim.
              --
              comp.lang.javas cript FAQ - http://jibbering.com/faq/

              Comment

              • Dr John Stockton

                #8
                Re: Equality...

                JRS: In article <bi0kkm$h0p$1@c hessie.cirr.com >, seen in
                news:comp.lang. javascript, ataru@nospam.cy berspace.org posted at Wed, 20
                Aug 2003 20:11:02 :-[color=blue]
                >From the FAQ:
                >
                >"Non-integer results should not normally be compared for equality."
                >
                >Now, I understand why this ends up being true, but then what is the workaround
                >should one actually desire to see whether 1.015 * 5 == 5.075?[/color]

                1015 * 5 == 5075

                Except when actually testing the properties of IEEE Double arithmetic,
                any reasonable problem using the equality of non-integers can be
                transformed into an equivalent problem using integers. Javascript
                integers are exact up to 2^53 = 9,007,199,254,7 40,992.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                Comment

                • Evertjan.

                  #9
                  Re: Equality...

                  Dr John Stockton wrote on 21 aug 2003 in comp.lang.javas cript:[color=blue]
                  > Javascript integers are exact up to 2^53 = 9,007,199,254,7 40,992.[/color]

                  Then there must also be inexact integers ?

                  9,007,199,254,7 40,992 + 1 ?

                  --
                  Evertjan.
                  The Netherlands.
                  (Please change the x'es to dots in my emailaddress)

                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: Equality...

                    "Evertjan." <exjxw.hannivoo rt@interxnl.net > writes:
                    [color=blue]
                    > Then there must also be inexact integers ?
                    >
                    > 9,007,199,254,7 40,992 + 1 ?[/color]

                    Yes
                    (90071992547409 92 == 900719925474099 3)
                    is true.
                    /L
                    --
                    Lasse Reichstein Nielsen - lrn@hotpop.com
                    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                    'Faith without judgement merely degrades the spirit divine.'

                    Comment

                    • Jim Ley

                      #11
                      Re: Equality...

                      On Fri, 22 Aug 2003 22:35:37 +0100, "Richard Cornford"
                      <Richard@litote s.demon.co.uk> wrote:
                      [color=blue]
                      >"Jim Ley" <jim@jibbering. com> wrote in message
                      >news:3f44ec10. 25075596@news.c is.dfn.de...
                      ><snip>[color=green][color=darkred]
                      >>>... (using IEEE double precision floats) ...[/color]
                      >>
                      >>Ed. 4 a long time ago back when the minutes of meetings etc.
                      >>was public were talking about switching to decimal arithmetic,
                      >>if Ed. 4 comes out ever (some rumour about 1st qtr 2004) then
                      >>we might see it. That would unlikely be relevant to client-side
                      >>code for years though.[/color]
                      >
                      >Does that mean a proposed change to the internal representation of the
                      >existing Number type ("switching" would imply so), or an additional
                      >number type?[/color]

                      I dunno, it was only minutes and they were somewhat cryptic, I think
                      there was certainly some discussion on it, I doubt very much it would
                      be a straight replacement.
                      [color=blue]
                      >It wouldn't necessarily remove the problem, - (((1/3) * 3) == 1) - would
                      >still be false, just move it to a decimal representation of numbers[/color]

                      True, but it would allow us to do things like legal work in
                      calculating the sales tax in Euros (which requires decimal arithmetic
                      I believe to be legal - I don't the penalty!) There's no decimal
                      library in JS (there probably is actually, but nothing built in etc.)
                      unlike other major langs that do that sort of calculation so it's
                      important for JS to have it I believe.
                      [color=blue]
                      >Can current FPUs do maths with decimal representations as input/output?[/color]

                      Don't know, but I think there's lots of work on doing them efficiently
                      now, there's also http://www2.hursley.ibm.com/decimal/ which
                      specifically mentions ECMA 334 ( C# ) in the decimal standards - ECMA
                      334 is the same folk doing javascript... they obviously like it.

                      Jim.
                      --
                      comp.lang.javas cript FAQ - http://jibbering.com/faq/

                      Comment

                      • Richard Cornford

                        #12
                        Re: Equality...

                        "Jim Ley" <jim@jibbering. com> wrote in message
                        news:3f469309.6 997371@news.cis .dfn.de...
                        <snip>[color=blue][color=green]
                        >>Does that mean a proposed change to the internal representation
                        >>of the existing Number type ("switching" would imply so), or
                        >>an additional number type?[/color]
                        >
                        >I dunno, it was only minutes and they were somewhat cryptic, I
                        >think there was certainly some discussion on it, I doubt very
                        >much it would be a straight replacement.[/color]

                        Hmm. A new number type would be difficult, it would require changes to
                        type-converting rules. You wouldn't want numbers that would have
                        previously become IEEE floats unexpectedly becoming decimal types. Maybe
                        a new built in Object to represent decimal numbers, with C++ style
                        operator overloading behind the scenes so it could work with (some)
                        maths operators directly (and its own new DecimalNumber literal format).

                        <snip>[color=blue]
                        >... it would allow us to do things like legal work in
                        >calculating the sales tax in Euros (which requires decimal
                        >arithmetic I believe to be legal - I don't the penalty!) ...[/color]
                        <snip>

                        I bet a few people's hearts skipped a beat reading that. :)

                        Richard.


                        Comment

                        • Dr John Stockton

                          #13
                          Re: Equality...

                          JRS: In article <bi62ba$7md$1$8 300dec7@news.de mon.co.uk>, seen in
                          news:comp.lang. javascript, Richard Cornford
                          <Richard@litote s.demon.co.uk> posted at Fri, 22 Aug 2003 22:35:37 :-[color=blue]
                          >
                          >Can current FPUs do maths with decimal representations as input/output?
                          >The last time I had to directly program an FPU (10 years ago plus) they
                          >used an 80 bit mantissa + 40 bit exponent format internally and only
                          >accepted/produced input/output in IEEE 32 bit and 64 bit formats.
                          >[/color]

                          For an exponent, 40 bits is a lot.

                          What I recall is that they use the 80-bit IEEE format, available in
                          Pascal/Delphi as "extended" internally, though perhaps with a couple of
                          guard bits. Their I/O formats are 32-bit float (single), 64-bit float
                          (double), 80-bit float (extended), and 64-bit integer (comp).
                          Javascript uses IEEE double for Number/

                          Extended has Sign Bit, 15 Exponent Bits, and 64 Mantissa Bits with the
                          binary point following the first; my pas-type.htm#FF refers.

                          H'mmm - I'm referring to the FPU of the PC; you might not be.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
                          <URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
                          <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

                          Comment

                          Working...