very strange operation result

Discussion in 'Javascript' started by maori, Feb 8, 2008.

  1. maori

    maori Guest

    You can try this in javascript:

    A) alert( 1.9 * 50 ); = 95
    B) alert( 2.9 * 50 ); = 145
    C) alert( 3.9 * 50 ); = 195
    D) alert( 4.9 * 50 ); = 245.00000000000003
    E) alert( 5.9 * 50 ); = 295

    Why D) = 245.00000000000003 ?

    Thanks,
    Maori.
     
    maori, Feb 8, 2008
    #1
    1. Advertising

  2. maori

    Henry Guest

    On Feb 8, 4:20 pm, maori wrote:
    > You can try this in javascript:
    >
    > A) alert( 1.9 * 50 );    =   95
    > B) alert( 2.9 * 50 );    =   145
    > C) alert( 3.9 * 50 );    =   195
    > D) alert( 4.9 * 50 );    =   245.00000000000003
    > E) alert( 5.9 * 50 );    =   295
    >
    > Why D)  = 245.00000000000003 ?


    Javascript's only numeric data type is an IEEE double precision
    floating point number. That type of number can represent many numbers
    precisely, and others it can only approximate. You have encountered
    one of the numbers that can only be approximated.

    This should not be an unexpected feature as it is common in systems
    for representing numbers. For example, a decimal representation of 1
    divided by 3 is unlikely to be precise (because it would be infinitely
    long), while the same number can be precisely represented in base 3 as
    0.1.
     
    Henry, Feb 8, 2008
    #2
    1. Advertising

  3. maori

    timothytoe Guest

    On Feb 8, 8:34 am, Henry <> wrote:
    > On Feb 8, 4:20 pm, maori wrote:
    >
    > > You can try this in javascript:

    >
    > > A) alert( 1.9 * 50 ); = 95
    > > B) alert( 2.9 * 50 ); = 145
    > > C) alert( 3.9 * 50 ); = 195
    > > D) alert( 4.9 * 50 ); = 245.00000000000003
    > > E) alert( 5.9 * 50 ); = 295

    >
    > > Why D) = 245.00000000000003 ?

    >
    > Javascript's only numeric data type is an IEEE double precision
    > floating point number. That type of number can represent many numbers
    > precisely, and others it can only approximate. You have encountered
    > one of the numbers that can only be approximated.
    >
    > This should not be an unexpected feature as it is common in systems
    > for representing numbers. For example, a decimal representation of 1
    > divided by 3 is unlikely to be precise (because it would be infinitely
    > long), while the same number can be precisely represented in base 3 as
    > 0.1.


    What you found did not surprise me. I've seen that kind of things for
    decades. The only system I can remember that didn't have that problem
    was the Atari 8-bit, which turned on the binary coded decimal bit in
    the 6502 for Atari BASIC.

    However, I've come across the problem in JavaScript even when
    multiplying and dividing integers. To me, that seems REALLY weird. For
    example, look where I had to use Math.round. I assume it's the
    division that screws me up. Without the Math.round, I get fractional
    turds...

    <pre>
    //------------------------------------------------------------------------------------------------------------
    // combinations(n,k)
    // returns the combinations of k objects from a set of n objects
    combinations: function(n,k) {
    function factorial(a) {
    return (a<2) ? 1 : a*factorial(a-1);
    }
    return Math.round(factorial(n)/(factorial(k)*factorial(n-k)));
    },
    </pre>
     
    timothytoe, Feb 8, 2008
    #3
  4. maori

    maori Guest

    And, how can I deal with this in javascript, please?


    "Henry" <> escribió en el mensaje
    news:...
    On Feb 8, 4:20 pm, maori wrote:
    > You can try this in javascript:
    >
    > A) alert( 1.9 * 50 ); = 95
    > B) alert( 2.9 * 50 ); = 145
    > C) alert( 3.9 * 50 ); = 195
    > D) alert( 4.9 * 50 ); = 245.00000000000003
    > E) alert( 5.9 * 50 ); = 295
    >
    > Why D) = 245.00000000000003 ?


    Javascript's only numeric data type is an IEEE double precision
    floating point number. That type of number can represent many numbers
    precisely, and others it can only approximate. You have encountered
    one of the numbers that can only be approximated.

    This should not be an unexpected feature as it is common in systems
    for representing numbers. For example, a decimal representation of 1
    divided by 3 is unlikely to be precise (because it would be infinitely
    long), while the same number can be precisely represented in base 3 as
    0.1.
     
    maori, Feb 8, 2008
    #4
  5. maori

    Evertjan. Guest

    timothytoe wrote on 08 feb 2008 in comp.lang.javascript:

    > What you found did not surprise me. I've seen that kind of things for
    > decades. The only system I can remember that didn't have that problem
    > was the Atari 8-bit, which turned on the binary coded decimal bit in
    > the 6502 for Atari BASIC.
    >



    Central Data Basic for the Signetics/Philips 2650 microprosessor,
    rumoured to have been written by a youth called William Gates,
    usded BCD as it's standard Math engine.

    And there were more:

    <http://en.wikipedia.org/wiki/Binary-coded_decimal>

    It is stil used in financial programming, in the code most radio clocks in
    Europe receive from the Frankfurt long wave transmitter, and many other
    applocations.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 8, 2008
    #5
  6. maori

    Doug Miller Guest

    In article <Xns9A3EE689DEB6Aeejj99@194.109.133.242>, "Evertjan." <> wrote:
    >timothytoe wrote on 08 feb 2008 in comp.lang.javascript:
    >
    >> What you found did not surprise me. I've seen that kind of things for
    >> decades. The only system I can remember that didn't have that problem
    >> was the Atari 8-bit, which turned on the binary coded decimal bit in
    >> the 6502 for Atari BASIC.


    Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
    etc.) -- have had true decimal arithmetic at the hardware level for more than
    forty years.

    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
     
    Doug Miller, Feb 9, 2008
    #6
  7. maori

    timothytoe Guest

    On Feb 8, 4:24 pm, (Doug Miller) wrote:
    > In article <Xns9A3EE689DEB6Aeej...@194.109.133.242>, "Evertjan." <> wrote:
    >
    > >timothytoe wrote on 08 feb 2008 in comp.lang.javascript:

    >
    > >> What you found did not surprise me. I've seen that kind of things for
    > >> decades. The only system I can remember that didn't have that problem
    > >> was the Atari 8-bit, which turned on the binary coded decimal bit in
    > >> the 6502 for Atari BASIC.

    >
    > Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
    > etc.) -- have had true decimal arithmetic at the hardware level for more than
    > forty years.
    >
    > --
    > Regards,
    > Doug Miller (alphageek at milmac dot com)
    >
    > It's time to throw all their damned tea in the harbor again.


    I didn't say the Atari was the only one. I said it was the only one I
    remembered.
     
    timothytoe, Feb 9, 2008
    #7
  8. Doug Miller wrote:
    > In article <Xns9A3EE689DEB6Aeejj99@194.109.133.242>, "Evertjan." <> wrote:
    >> timothytoe wrote on 08 feb 2008 in comp.lang.javascript:
    >>
    >>> What you found did not surprise me. I've seen that kind of things for
    >>> decades. The only system I can remember that didn't have that problem
    >>> was the Atari 8-bit, which turned on the binary coded decimal bit in
    >>> the 6502 for Atari BASIC.


    > Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
    > etc.) -- have had true decimal arithmetic at the hardware level for more than
    > forty years.


    And the latest round of the z/Architecture (the current descendant), has
    added the new IEEE-754r decimal floating point, which Intel,
    unfortunately, seems to be resisting, though languages are working to
    add it.

    Before the S/360, most of IBM's product line -- the 650/707x series, the
    702/705/7080 series, the 1401/1440/1460 series, the 1410/7010 series,
    the 350, and the 1620/1710 -- used decimal arithmetic /exclusively/,
    even for addressing. Only the 701/704/709/704x/709x series and the 7030
    supercomputer used binary. Even the one-off NORC supercomputer used
    decimal.


    --
    John W. Kennedy
    If Bill Gates believes in "intelligent design", why can't he apply it to
    Windows?
     
    John W. Kennedy, Feb 9, 2008
    #8
  9. maori

    VK Guest

    On Feb 8, 7:46 pm, timothytoe <> wrote:
    > However, I've come across the problem in JavaScript even when
    > multiplying and dividing integers. To me, that seems REALLY weird.


    First of all, you've come across the problem in dividing integers, not
    in multiplying them. Multiplication doesn't change the dyadicity of
    decadic integers.

    Secondly, it is not particular Javascript problem: it is true for any
    machine using internal dyadic numbers while getting/outputting decadic
    numbers, that means for any up to moment existing or ever existed
    discrete computer. This is why it is always amusing to read some
    "sweet memory" references of the kind "and model X did not have
    it" :)
    It is just always the programming choice what to implement: to make
    the output "as expected by an average user" suffering for some
    precision lost, or to output "as it really is but may be unexpected
    for an average user". Javascript uses the second approach, some
    environment might/may offer the first one.

    Basically decadic rationals is a rather small subset of real numbers,
    and decadic rationals being representable as dyadic rationals as well
    are forming even much more smaller subset of the first set (dyadic
    solenoid). Vulgarly speaking, only some decadic numbers can be exactly
    represented as a finite sequence in decimal system, and out from them
    only some can be represented as a finite sequence in binary system.

    The overall math defining all relations may be rather involving but I
    can post it here. From the practical point of view
    http://www.jibbering.com/faq/index.html#FAQ4_6
    and
    http://www.jibbering.com/faq/index.html#FAQ4_7
    do provide enough of info though I would argue with some rounding
    ideas in FAQ4_6



    For
    > example, look where I had to use Math.round. I assume it's the
    > division that screws me up. Without the Math.round, I get fractional
    > turds...
    >
    > <pre>
    > //------------------------------------------------------------------------------------------------------------
    > // combinations(n,k)
    > // returns the combinations of k objects from a set of n objects
    > combinations: function(n,k) {
    > function factorial(a) {
    > return (a<2) ? 1 : a*factorial(a-1);
    > }
    > return Math.round(factorial(n)/(factorial(k)*factorial(n-k)));
    > },
    > </pre>
     
    VK, Feb 9, 2008
    #9
  10. In comp.lang.javascript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
    9g2000hsa.googlegroups.com>, Sat, 9 Feb 2008 11:50:46, VK
    <> posted:

    >though I would argue with some rounding
    >ideas in FAQ4_6


    The code there is correct but outdated. For more recent code, go via
    the first link.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
    free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> unsupported.
     
    Dr J R Stockton, Feb 10, 2008
    #10
  11. In comp.lang.javascript message <4712391e-7650-4b89-bb93-b9df18202c7b@j2
    0g2000hsi.googlegroups.com>, Sun, 10 Feb 2008 11:03:56, VK
    <> posted:
    >> >though I would argue with some rounding
    >> >ideas in FAQ4_6

    >>
    >> The code there is correct but outdated.  For more recent code, go via
    >> the first link.

    >
    >The code is just fine as a program entity.


    In the FAQ, it is not. It is slower than necessary, for example.

    >I do not agree - as I said a year or so ago - that it prepostulates
    >exactly one rounding algorithm out of possible and respectively call
    >any other possible rounding algorithm as a bug.


    There must be a meaning to that. Have you considered learning to write
    unambiguous English? I believe India is still a good place to learn it.

    > For the absolute
    >majority of users the differences are irrelevant yet it should be
    >stated in the FAQ the chosen rounding algorithm thus the outcome.
    >Simply stating "according to ECMAScript 3rd ed." doesn't tell too
    >much, neither holds some big importance in the domain of question:
    >Ecmascript International is not an IEEE authoritative member.


    The IEEE, of course, is merely a national body, though it has members
    from various countries. Its authority is only local, though its
    abilities can be more widely respected. Its preferences can be
    incompatible with the agreed international ones.

    One has told the FAQ maintainer about the superior authority of ISO/IEC
    16262.


    Bankers' Rounding of IEEE Doubles is probably too awkward for the FAQ.

    A search at ISO for "Language independent arithmetic" reveals standards,
    but costing too many CHF.

    @hotpop failed.

    --
    (c) John Stockton, nr London UK. ??@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    In MS OE, choose Tools, Options, Send; select Plain Text for News and E-mail.
    Don't quote more than is needed, and respond after each quoted part.
     
    Dr J R Stockton, Feb 11, 2008
    #11
  12. maori

    Guest

    Randy seems to have a low threshold... or he's bored.
    Is there anyone out there that has escaped him?
    Said on top for Randy. :)


    On Feb 8, 2:37 pm, Randy Webb <> wrote:
    > maori said the following on 2/8/2008 11:46 AM:
    >
    > Answer:It destroys the order of the conversation
    > Question: Why?
    > Answer: Top-Posting.
    > Question: Whats the most annoying thing on Usenet?
    >
    > > And, how can I deal with this in javascript, please?

    >
    > You could start with the group FAQ:
    >
    > <URL:http://jibbering.com/faq/index.html#FAQ4_7>
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
    > Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
     
    , Feb 11, 2008
    #12
  13. maori

    Guest

    Thanks Randy!

    On Feb 11, 6:51 pm, Randy Webb <> wrote:
    > said the following on 2/11/2008 6:16 PM:
    >
    > > Randy seems to have a low threshold... or he's bored.

    >
    > Neither.
    >
    > > Is there anyone out there that has escaped him?

    >
    > Yes. The many that use a little intelligence to solve a problem rather
    > than wanting someone to spoon feed them.
    >
    > > Said on top for Randy. :)

    >
    > That's nice[1].
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
    > Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractic
     
    , Feb 12, 2008
    #13
  14. Dr J R Stockton wrote:
    > One has told the FAQ maintainer about the superior authority of ISO/IEC
    > 16262.


    And one has been disproved already. It is only that one's assumed
    superiourity prevents one from recognizing that.


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Feb 12, 2008
    #14
  15. maori

    timothytoe Guest

    What is it about comp.lang.javascript that makes people behave as if
    they were in 2nd grade? We should add a "Why do people act childishly
    in clj?" to the FAQ.

    How old are you people?
     
    timothytoe, Feb 13, 2008
    #15
  16. maori

    Evertjan. Guest

    timothytoe wrote on 13 feb 2008 in comp.lang.javascript:

    > What is it about comp.lang.javascript that makes people behave as if
    > they were in 2nd grade?


    You must be first grade, we are only second grade.

    Or are you referring to some local school system, unknown to many of "us"?

    > We


    Are you plural, or childish?

    > should add a "Why do people act childishly in clj?" to the FAQ.


    Perhaps you can give us some first grade advice about Javascript?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 13, 2008
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    510
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,704
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    453
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    533
    Dave Thompson
    Feb 14, 2005
  5. Michael Tan
    Replies:
    32
    Views:
    1,052
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page