Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Codes fails if sum total is around .8 - why ?

    Hi all again

    Can anyone tell me what I'm doing wrong as this code "seems" to work until the total reaches around .8

    You can test this by adding 12.34 to the 1st, 2nd and 3rd boxes then you get an error - but if you change the last 12.34 to 12.35 it works fine.

    The same happens if you add 50.7 to the 1st and then 50.1 to the 2nd - but it will work if the answer is not around .8

    Thanks if you can spot my basic mistake - even better if you can suggest a better (simpler) method, as this is doing my head in :-)

    Karen



    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>   
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                var am_1 = $('input:text[id$=am_1]').keyup(foo);
                var am_2 = $('input:text[id$=am_2]').keyup(foo);            
                var am_3 = $('input:text[id$=am_3]').keyup(foo);
                var am_4 = $('input:text[id$=am_4]').keyup(foo);          
                var am_5 = $('input:text[id$=am_5]').keyup(foo);
                var am_6 = $('input:text[id$=am_6]').keyup(foo);            
                var am_7 = $('input:text[id$=am_7]').keyup(foo);
                var am_8 = $('input:text[id$=am_8]').keyup(foo);   
                function foo() {
                    var value1 = am_1.val();
                    var value2 = am_2.val();
                    var value3 = am_3.val();
                    var value4 = am_4.val();
                    var value5 = am_5.val();
                    var value6 = am_6.val();
                    var value7 = am_7.val();
                    var value8 = am_8.val();
    
                    var sum = add(value1, value2, value3, value4, value5, value6, value7, value8);
                    $('input:text[id$=TSum]').val(sum);
                }
     
                function add() {
                    var sum = 0;
                    for (var i = 0, j = arguments.length; i < j; i++) {
                        if (IsNumeric(arguments[i])) {
                            sum += parseFloat(arguments[i]);
                        }
                    }
                    return sum;
                }
                function IsNumeric(input) {
                    return (input - 0) == input && input.length > 0;
                }
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <table width="300px" border="1" >
        <tr><td><asp:TextBox runat="server" ID="am_1"></asp:TextBox></td></tr>
        <tr><td><asp:TextBox runat="server" ID="am_2"></asp:TextBox></td></tr>
        <tr><td><asp:TextBox runat="server" ID="am_3"></asp:TextBox></td></tr>
        <tr><td><asp:TextBox runat="server" ID="am_4"></asp:TextBox></td></tr>
        <tr><td><asp:TextBox runat="server" ID="am_5"></asp:TextBox></td></tr>
        <tr><td><asp:TextBox runat="server" ID="am_6"></asp:TextBox></td></tr>
        <tr><td><asp:TextBox runat="server" ID="am_7"></asp:TextBox></td></tr>
        <tr><td><asp:TextBox runat="server" ID="am_8"></asp:TextBox></td></tr>
    
        <tr><td>Total:&nbsp;&nbsp;<asp:TextBox runat="server" ID="TSum"></asp:TextBox></td>
        </tr>
        </table>   
        </div>
        </form>
    </body>
    </html>

  • #2
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    and it seems it's not just me.

    http://www.texaswebdevelopers.com/ex...javascript.asp
    This is the same problem (with a different code) from someone teaching people how to write code.

    On thier website try adding 50.1 to the 1st box and 50.1 to the second - same problem.



    I never was much use a math at school so I can't work this out

    What am I missing please ??

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I take it you mean that 12.34 + 12.34 + 12.34 = 37.019999999999996 and 50.7 + 50.1 = 100.80000000000001

    It happens because computers work in binary so they convert your number to the nearest binary equivalent first and then convert the binary back into the nearest decimal equivalent at the end.

    ECMAScript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an accuracy of 15-16 decimal digits; integers up to just over 9e15 are precise, but few decimal fractions are. Given this, arithmetic is as exact as possible, but no more. Operations on integers are exact if the true result and all intermediates are integers within that range.


    Other examples:-

    Code:
    <script type = "text/javascript">
    
    alert (1e20);
    alert (1e21);
    
    alert (Math.round(0.49999999999999992)) // results 1. 
    
    alert (5 * 1.015);  // does not give exactly 5.075 
    
    alert (0.06+0.01);  //  does not give exactly 0.07 in javascript. 
    
    alert (4.935*100);
    
    alert (3355.53 + 660.97 - 660.97);
    
    alert (1.1 * 1.1);
    
    alert (5.5%2.2);
    
    alert (9999999999999999);
    
    </script>
    If you need an exact answer to a set number of decimal places then shift the decimal point in all your numbers that many places to the right to make integers before doing the calculation and then shift it back after the calculation. For example with currencies that use two decimal places you should always multiply them all by 100 at the start and divide by 100 at the end or the answer might not be exact.

    Code:
    <script type = "text/javascript">
    
    var a = 50.7;
    var b = 50.1;
    alert (a+b); // 100.80000000000001 
    var c = ((a*100)+(b*100))/100;
    alert(c);  // 100.8
    
    </script>

    “What is a cynic? A man who knows the price of everything and the value of nothing.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)
    Last edited by Philip M; 03-09-2013 at 03:42 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Reflo2 (03-09-2013)

  • #4
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    One addition: Be careful what kind of operations you're doing, though. With addition and subtraction it works fine, but multiplication, for example, will get you in trouble:

    Code:
    var a = 4,
        b = 5;
    
    var aShifted = 100 * a,
        bShifted = 100 * b;
    
    // Addition works
    console.log( a + b ); // 9
    console.log( ( aShifted + bShifted ) / 100 ); // 9
    
    // Multiplication not so much
    console.log( a * b ); // 20
    console.log( ( aShifted * bShifted ) / 100 ); // 2000
    Of course this doesn't come surprising if you know elementary school math, but there is a certain risk that one doesn't think about it. Not that the trick wouldn't work in this case, it just has to be slightly modified:

    Code:
    console.log( ( aShifted * bShifted ) / 10000 ); // 20
    In other words: It's not as easy as simply dividing by what you multiplied the numbers with in the beginning. You'll actually have to think about what to divide by in the end.

  • #5
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Philip

    i have tried to *100 then /100 within the code but it either does nothing only gives the answer 0.

    eg
    Code:
                    var sum = add(((value1*100), (value2*100), (value3*100), (value4*100), (value5*100), (value6*100), (value7*100), (value8*100))/100);
                    $('input:text[id$=TSum]').val(sum);
    Where would I add the calculation please

    Karen

  • #6
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Philip

    i have tried to *100 then /100 within the code but it either does nothing only gives the answer 0.

    eg
    Code:
                    var sum = add(((value1*100), (value2*100), (value3*100), (value4*100), (value5*100), (value6*100), (value7*100), (value8*100))/100);
                    $('input:text[id$=TSum]').val(sum);
    Where would I add the calculation please

    Karen

    ps - you are right in that users are adding currency (UK Pound) amounts

  • #7
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    It's probably better to adjust your add method:

    Code:
    function add() {
        var sum = 0;
        for (var i = 0, j = arguments.length; i < j; i++) {
            if (IsNumeric(arguments[i])) {
                sum += 100 * parseFloat(arguments[i]);
            }
        }
        return sum / 100;
    }

  • Users who have thanked Airblader for this post:

    Reflo2 (03-09-2013)

  • #8
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you Philip_M and Airbladder

    Your help has solved a problem I have been working on for 2 days.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Reflo2 View Post
    Thank you Philip_M and Airbladder
    ROTFL How appropriate!

    Reminds me of a message stuck on the front of one of those hot-air hand-drying machines in a university toilet

    "To listen to a lecture by Professor X press here".

    We are sorry that as a result of a typographical error we referred to the now deceased but well-known English actor Denis Prick.
    This should of course have read Pe*is Price. - Daily Mirror

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Philip M View Post
    How appropriate!
    I did not criticize or attack you in this thread at all. If you want to make everything personal from now on because we have different views on some topics, be my guest. Let's start with the good laugh I get every time you complain about books and tutorials using "out-dated technology that became obsolete a long time ago", even though your code "style" became obsolete a decade ago and yet you go on teaching it to beginners. How's that for a hot air lecture? And how appropriate that your quote is about typography, which is partly about making what you do look nice and readable.

    You'd think someone of your age is a little less of a child.
    Last edited by Airblader; 03-09-2013 at 07:55 PM.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Airblader View Post
    I did not criticize or attack you in this thread at all.
    No, not in this thread.

    Would you be surprised to learn that nobody but you has ever raised that objection, in this forum or anywhere else? So what is it that you feel gives you (a newcomer to the forum) a licence to be so critical and combative?

    I quite understand that you have no sense of humour, and feel unrestrained by any lack of self-esteem. But be aware that my motto is the same as that of the Kings of Scotland - Nemo me impune lacessit.

    I see the purpose of this forum is to offer assistance to the OPs. Not to manufacture quarrels with other contributors.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Philip M View Post
    So what is it that you feel gives you (a newcomer to the forum) a licence to be so critical and combative?
    All I did was criticize things that I find worth discussing. If you don't feel the same way, you are always welcome to not participate in such discussions. I don't see any rule or reason why I need to wait a few years before I'm allowed to have my opinion and share my thoughts on other people's posts. Just because you're older, just because you have been in this forum longer, your opinion is not "worth" more than mine (and vice versa).

    I see the purpose of this forum is to offer assistance to the OPs. Not to manufacture quarrels with other contributors.
    I saw that when I posted in this thread, adding a little warning to your post and helping the OP to rewrite his code, when you replied with a whole post dedicated to offending me.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Airblader View Post
    All I did was criticize things that I find worth discussing. If you don't feel the same way, you are always welcome to not participate in such discussions. I don't see any rule or reason why I need to wait a few years before I'm allowed to have my opinion and share my thoughts on other people's posts. Just because you're older, just because you have been in this forum longer, your opinion is not "worth" more than mine (and vice versa).

    Get wise to yourself. Most people would accept that in any subject the opinion of a Johnny-Come-Lately is not worth that of (far) older and more experienced people. You will recall that you received absolutely zero support from others in this forum.

    What you are saying is that you are free to make posts which are dedicated to offending me, but start to scream when the position is reversed.

    It was not me who corrupted your screen name to the comic Airbladder. All I did was laugh at something I found funny.
    You would call that sharing my thoughts. And you frequently mis-spell my name.

    But I have just realised that Airblade is the name of James Dyson's high-tech hand dryer which pumps out a blast of hot air. But you are quite free not to participate in any discussion about that!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #14
    New Coder
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am truly sorry if I have offended anyone by misspelling their name, that was not my intention.

    I, like many people, having been browsing this forum for quite some time as it is an extremely valuable resource. It was only when I couldn’t find the answer I was looking for that I decided to join, so I could post a specific question.

    I am not sure if there is a way that I can “redo” my thanks so that I can
    alter the spelling. If there is a way can someone please tell me.

    Once again I am very sorry and I honestly didn’t mean to offend anyone.

    Karen

    PS. He is a She :-)

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Reflo2 View Post
    I am truly sorry if I have offended anyone by misspelling their name, that was not my intention.

    I, like many people, having been browsing this forum for quite some time as it is an extremely valuable resource. It was only when I couldn’t find the answer I was looking for that I decided to join, so I could post a specific question.

    I am not sure if there is a way that I can “redo” my thanks so that I can
    alter the spelling. If there is a way can someone please tell me.

    Once again I am very sorry and I honestly didn’t mean to offend anyone.

    Karen

    PS. He is a She :-)

    Karen - There is absolutely no need to apologise. Most people would just laugh at your innocent typo. Only the over-sensitive and over-serious would take offence. Airblader (who is devoid of a sense of humour) is cross with me (not you) for making fun of him.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •