JavaScript Math vs Excel

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

    #1

    JavaScript Math vs Excel

    Hi,

    I'm currently writing a web application for bicycle wheel builders
    that calculate the spoke length for all sorts of variations of hubs
    and rims.

    I have translated the formula from an Excel spreadsheet and in
    JavaScript it looks like this:

    var sll=Math.sqrt(M ath.pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/
    (spokes/2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/
    (spokes/2)))),2)+Math.p ow((c2l+osb),2)-shd/2);

    The problem is that I get slightly different results when I use
    JavaScript than when I use Open Office Calc. It isn't much, less than
    a millimeter, which shouldn't really matter when you're building a
    wheel but it is slightly worrying me.

    My question, is it possible that the JavaScript Math object is not as
    powerful as the Excel or OpenOffice one and that there are just slight
    rounding errors that are causing this disparity? Or to rephrase the
    question, can I stop checking the formula over and over again for
    errors?

    Thanks
    Lenni

  • thollister911

    #2
    Re: JavaScript Math vs Excel

    Lenni --

    Very interesting project. I love math and am an engineer ... are you sure
    your algorythm is accurate? A detailed description of the variables you
    use within that formula and how you derived each step of the calculation
    would be something you might want to get a second opinion on? I am not a
    hub expert ... but the amount a spoke rotates axially, the hub width, and
    the distance to axle centerline are certainly variables of interest. Rim
    diameter and offset need to be considered too.

    When you raise a value to a power ... there is usually a great amount of
    sensitivety with precision of the exponent. And you do this often in your
    formula. I suggest printing out 15+ digits within your spreadsheet to
    determine its precision and doing the same within other programs.
    Restrict the most precise to the accuracy of the least precise until you
    get an exact match. Off by a millimeter is not acceptable in high
    performance situations.

    Break up your formula into many groupings and compare each one-to-one on
    each platform ... instead of just looking at the end result. A true error
    analysis is not a trivial task.

    I am just now learning JavaScript, but I have done a lot of C and C++
    programming where precision was very important and any discreptancy had
    to be explored fully.

    Dig in deeper is my suggestion. I wish I knew JavaScript well enough to
    be more helpful. Good luck!

    -- tom

    Lenni <Lenniboy@googl email.comwrote in news:834f2e95-9331-46a5-a2c4-
    43a2792fa288@b1 g2000hsg.google groups.com:
    Hi,
    >
    I'm currently writing a web application for bicycle wheel builders
    that calculate the spoke length for all sorts of variations of hubs
    and rims.
    >
    I have translated the formula from an Excel spreadsheet and in
    JavaScript it looks like this:
    >
    var sll=Math.sqrt(M ath.pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/
    (spokes/2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/
    (spokes/2)))),2)+Math.p ow((c2l+osb),2)-shd/2);
    >
    The problem is that I get slightly different results when I use
    JavaScript than when I use Open Office Calc. It isn't much, less than
    a millimeter, which shouldn't really matter when you're building a
    wheel but it is slightly worrying me.
    >
    My question, is it possible that the JavaScript Math object is not as
    powerful as the Excel or OpenOffice one and that there are just slight
    rounding errors that are causing this disparity? Or to rephrase the
    question, can I stop checking the formula over and over again for
    errors?
    >
    Thanks
    Lenni
    >
    >

    Comment

    • SAM

      #3
      Re: JavaScript Math vs Excel

      Le 10/26/08 10:04 PM, Lenni a écrit :
      >
      in JavaScript it looks like this:
      >
      var sll=Math.sqrt(M ath.pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/
      (spokes/2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/
      (spokes/2)))),2)+Math.p ow((c2l+osb),2)-shd/2);
      >
      The problem is that I get slightly different results when I use
      JavaScript than when I use Open Office Calc. It isn't much, less than
      a millimeter, which shouldn't really matter when you're building a
      wheel but it is slightly worrying me.
      Javascript calculate on base 64 (I think, or something like that) and
      sometimes it can't give the exactly number such as :
      1.0000000009
      instead of 1

      You could try by using your variables multiplied by 1000 or 100000
      and finally get back the roundness of the result divided by the same
      coefficient

      Perhaps that could work ?

      --
      sm

      Comment

      • Joost Diepenmaat

        #4
        Re: JavaScript Math vs Excel

        SAM <stephanemoriau x.NoAdmin@wanad oo.fr.invalidwr ites:
        Javascript calculate on base 64 (I think, or something like that)
        That is complete crap. The ecmascript specs are quite clear about how JS
        numbers are represented: as IEEE 754 floats. It also states that all
        numeric operations are according to the IEEE 754 rules. Most languages
        that don't have explicit rules follow these in practice (since most CPUs
        do).
        and sometimes it can't give the exactly number such as : 1.0000000009
        instead of 1
        And any language doing floating binary calculations will, under certain
        (common) circumstances. Binary representation of floating points can not
        represent all numbers exactly that decimal representation can.

        See http://docs.sun.com/source/806-3568/ncg_goldberg.html

        --
        Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

        Comment

        • Evertjan.

          #5
          Re: JavaScript Math vs Excel

          SAM wrote on 27 okt 2008 in comp.lang.javas cript:
          avascript calculate on base 64 (I think, or something like that) and
          sometimes it can't give the exactly number such as :
          1.0000000009
          instead of 1
          >
          You could try by using your variables multiplied by 1000 or 100000
          and finally get back the roundness of the result divided by the same
          coefficient
          >
          Such division could sometimes introduce the same kind of error, methinks.

          Better use regex do a string manipulation imitating such division:


          var aNumber = 1.2345
          var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.$1')


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

          Comment

          • Dr J R Stockton

            #6
            Re: JavaScript Math vs Excel

            In comp.lang.javas cript message <Xns9B45664504D 47eejj99@194.10 9.133.242>
            , Tue, 28 Oct 2008 09:03:18, Evertjan. <exjxw.hannivoo rt@interxnl.net >
            posted:
            >
            >var aNumber = 1.2345
            >var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.$1')
            >
            Fails rather badly on numbers over about 1e18, though a Zimbabwean
            economist might not notice; also on small numbers.

            --
            (c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
            Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
            Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
            Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)

            Comment

            • Conrad Lender

              #7
              Re: JavaScript Math vs Excel

              On 2008-10-28 20:21, Dr J R Stockton wrote:
              >>var aNumber = 1.2345
              >>var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.$1')
              >
              Fails rather badly on numbers over about 1e18, though a Zimbabwean
              economist might not notice; also on small numbers.
              Care to explain that remark about the Zimbabwean economist?


              - Conrad

              Comment

              • RobG

                #8
                Re: JavaScript Math vs Excel

                On Oct 29, 1:27 pm, Conrad Lender <crlen...@yahoo .comwrote:
                On 2008-10-28 20:21, Dr J R Stockton wrote:
                >
                >var aNumber = 1.2345
                >var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'..$1')
                >
                Fails rather badly on numbers over about 1e18, though a Zimbabwean
                economist might not notice; also on small numbers.
                >
                Care to explain that remark about the Zimbabwean economist?
                The economy is a complete basket case, I think it is an oblique
                reference to the inflation rate of about 231,000,000% (yes, two
                hundred and thirty one million percent)[1] and probably rising -
                fast. It was a mere 11,200,00%[2] two months ago.

                1. <URL: http://www.guardian.co.uk/world/2008/oct/09/zimbabwe >
                2. <URL: http://edition.cnn.com/2008/BUSINESS...ion/index.html
                >
                --
                Rob

                Comment

                • Conrad Lender

                  #9
                  Re: JavaScript Math vs Excel

                  On 2008-10-29 04:44, RobG wrote:
                  >>Fails rather badly on numbers over about 1e18, though a
                  >>Zimbabwean economist might not notice; also on small numbers.
                  >>
                  >Care to explain that remark about the Zimbabwean economist?
                  >
                  The economy is a complete basket case, I think it is an oblique
                  reference to the inflation rate of about 231,000,000%
                  Oh dear. At that rate they'll soon have their inflation *rate* overflow,
                  unless somebody anticipated the need for more than 4 bytes to hold that
                  number. On the other hand, if it's signed, it will just wrap around and
                  become negative. That should solve the problem.

                  Thanks for the explanation.


                  - Conrad

                  Comment

                  • gimme_this_gimme_that@yahoo.com

                    #10
                    Re: JavaScript Math vs Excel

                    Try writing the code in VBScript.

                    My experience is that you'll *never* get JavaScript and Excel VBA to
                    return *identical* results when there is lots of math and rounding.

                    Not tested - but the function might look like this:

                    <script language="VBScr ipt">
                    <!--
                    Function SLL(cross,erd,s pokes,erd,osb)
                    SSL =Math.sqrt(Math .pow(((fdl/2*Math.sin((2*M ath.PI*cross)))/ (spokes/
                    2)),2)+Math.pow ((erd/2-((fdl/2)*Math.cos(2*M ath.PI*cross/ (spokes/
                    2)))),2)+Math.p ow((c2l+osb),2)-shd/2);
                    End Function
                    //-->
                    </script>

                    This script is tested and works in both Excel VBA and VBScript.

                    <script language="VBScr ipt">
                    <!--
                    Function VBRound(a, b)
                    Result = ""
                    If 0 = b Then
                    Result = ""
                    ElseIf "" = b Then
                    Result = a
                    ElseIf 0 = a then
                    Result = 0
                    Else
                    Result = b * ((a \ b) - CInt(((a Mod b) >= (b / 2))))
                    End If
                    VBRound = Result
                    End Function
                    //-->
                    </script>

                    Comment

                    • gimme_this_gimme_that@yahoo.com

                      #11
                      Re: JavaScript Math vs Excel

                      Speaking of VBScript - the strategy you should take is to write the
                      Function in Excel VBA - then translate to VBScript.

                      You started with Excel formulas - you need to start with Excel VBA.

                      Comment

                      • sasuke

                        #12
                        Re: JavaScript Math vs Excel

                        On Oct 28, 2:03 pm, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
                        SAM wrote on 27 okt 2008 in comp.lang.javas cript:
                        >
                        avascript calculate on base 64 (I think, or something like that) and
                        sometimes it can't give the exactly number such as :
                        1.0000000009
                        instead of 1
                        >
                        You could try by using your variables multiplied by 1000 or 100000
                        and finally get back the roundness of the result divided by the same
                        coefficient
                        >
                        Such division could sometimes introduce the same kind of error, methinks.
                        >
                        Better use regex do a string manipulation imitating such division:
                        >
                        var aNumber = 1.2345
                        var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.$1')
                        var aNumber = 1.2345678;
                        var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.
                        $1');
                        alert(resultStr ing); // 1235.0.678

                        ../sasuke

                        Comment

                        • Evertjan.

                          #13
                          Re: JavaScript Math vs Excel

                          sasuke wrote on 30 okt 2008 in comp.lang.javas cript:
                          >Better use regex do a string manipulation imitating such division:
                          >>
                          >var aNumber = 1.2345
                          >var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.$
                          1')
                          >
                          var aNumber = 1.2345678;
                          var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.
                          $1');
                          alert(resultStr ing); // 1235.0.678
                          >
                          You are right, should be[, only where the absolute value is above 1]:

                          var aNumber = 1.2345678;
                          var resultString =
                          Math.floor(aNum ber*1000+.5).to String().replac e(/(\d\d\d)$/,'.$1');
                          alert(resultStr ing); // 1.235




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

                          Comment

                          • Evertjan.

                            #14
                            Re: JavaScript Math vs Excel

                            Evertjan. wrote on 30 okt 2008 in comp.lang.javas cript:
                            sasuke wrote on 30 okt 2008 in comp.lang.javas cript:
                            >
                            >>Better use regex do a string manipulation imitating such division:
                            >>>
                            >>var aNumber = 1.2345
                            >>var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)
                            $/,'.$
                            >1')
                            >>
                            >var aNumber = 1.2345678;
                            >var resultString = (aNumber*1000+. 5).toString().r eplace(/(\d\d\d)$/,'.
                            >$1');
                            >alert(resultSt ring); // 1235.0.678
                            >>
                            >
                            You are right, should be[, only where the absolute value is above 1]:
                            >
                            var aNumber = 1.2345678;
                            var resultString =
                            Math.floor(aNum ber*1000+.5).to String().replac e(/(\d\d\d)$/,'.$1');
                            alert(resultStr ing); // 1.235
                            Wow, not that easy in regex, try:

                            <script type='text/javascript'>

                            function round3dec(x) {
                            x = Math.floor(x*10 00+.5)+'';
                            var s = x.replace(/(^\-?).*/,'$1');
                            x = x.replace(/^\-?/,'0000');
                            return s + (1*x.replace(/\d\d\d$/,'')) + '.' +
                            x.replace(/-?\d*(\d\d\d)$/,'$1');
                            };

                            alert( round3dec(21.23 45678) );
                            alert( round3dec(-21.2345678) );
                            alert( round3dec(0.002 5555) );
                            alert( round3dec(-0.0025555) );

                            </script>


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

                            Comment

                            • Conrad Lender

                              #15
                              Re: JavaScript Math vs Excel

                              On 2008-10-30 21:59, Evertjan. wrote:
                              Wow, not that easy in regex, try:
                              >
                              <script type='text/javascript'>
                              >
                              function round3dec(x) {
                              x = Math.floor(x*10 00+.5)+'';
                              var s = x.replace(/(^\-?).*/,'$1');
                              x = x.replace(/^\-?/,'0000');
                              return s + (1*x.replace(/\d\d\d$/,'')) + '.' +
                              x.replace(/-?\d*(\d\d\d)$/,'$1');
                              };
                              >
                              alert( round3dec(21.23 45678) );
                              alert( round3dec(-21.2345678) );
                              alert( round3dec(0.002 5555) );
                              alert( round3dec(-0.0025555) );
                              >
                              </script>
                              (10000000000000 00000).toFixed( 3)
                              -100000000000000 0000.000

                              round3dec(10000 00000000000000)
                              -1e+21.00001e+21

                              :-)


                              - Conrad

                              Comment

                              Working...