Numerical Accuracy: How many digits are correct?












0












$begingroup$


I'm really not too sure how to go on about this:



Problem. Julia (or take MatLab etc. (I guess.)) computes



In [1]: exp(pi*sqrt(67)/6) - sqrt(5280)
Out[1]: 6.121204876308184e-8


Only by looking closely: How many digits are correct? (What could you do to compute $16$ decimals correctly?)



So I know that subtraction is somewhat dangerous numerically - though how do I solve this problem in a precise manner?










share|cite|improve this question











$endgroup$












  • $begingroup$
    Try writing it as $$frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}$$
    $endgroup$
    – Andrei
    Jan 3 at 19:19


















0












$begingroup$


I'm really not too sure how to go on about this:



Problem. Julia (or take MatLab etc. (I guess.)) computes



In [1]: exp(pi*sqrt(67)/6) - sqrt(5280)
Out[1]: 6.121204876308184e-8


Only by looking closely: How many digits are correct? (What could you do to compute $16$ decimals correctly?)



So I know that subtraction is somewhat dangerous numerically - though how do I solve this problem in a precise manner?










share|cite|improve this question











$endgroup$












  • $begingroup$
    Try writing it as $$frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}$$
    $endgroup$
    – Andrei
    Jan 3 at 19:19
















0












0








0





$begingroup$


I'm really not too sure how to go on about this:



Problem. Julia (or take MatLab etc. (I guess.)) computes



In [1]: exp(pi*sqrt(67)/6) - sqrt(5280)
Out[1]: 6.121204876308184e-8


Only by looking closely: How many digits are correct? (What could you do to compute $16$ decimals correctly?)



So I know that subtraction is somewhat dangerous numerically - though how do I solve this problem in a precise manner?










share|cite|improve this question











$endgroup$




I'm really not too sure how to go on about this:



Problem. Julia (or take MatLab etc. (I guess.)) computes



In [1]: exp(pi*sqrt(67)/6) - sqrt(5280)
Out[1]: 6.121204876308184e-8


Only by looking closely: How many digits are correct? (What could you do to compute $16$ decimals correctly?)



So I know that subtraction is somewhat dangerous numerically - though how do I solve this problem in a precise manner?







numerical-methods floating-point






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Jan 3 at 20:08









LutzL

57.2k42054




57.2k42054










asked Jan 3 at 19:12









KezerKezer

1,313421




1,313421












  • $begingroup$
    Try writing it as $$frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}$$
    $endgroup$
    – Andrei
    Jan 3 at 19:19




















  • $begingroup$
    Try writing it as $$frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}$$
    $endgroup$
    – Andrei
    Jan 3 at 19:19


















$begingroup$
Try writing it as $$frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}$$
$endgroup$
– Andrei
Jan 3 at 19:19






$begingroup$
Try writing it as $$frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}$$
$endgroup$
– Andrei
Jan 3 at 19:19












2 Answers
2






active

oldest

votes


















1












$begingroup$

Using a multi-precision package like the CAS Magma you can compute a value where perhaps the last 10 of 200 digits are polluted by truncation and rounding errors. The loop using different target digit numbers



for N in [0..5] do 
RR := RealField(40+32*N);
Exp(Pi(RR)*(RR!67)^(1/2)/6) - (RR!5280)^(1/2);
end for;


gives the result



0.000000061212043348401464497838533606
0.00000006121204334840146449783853360999610568722086907308823071985234
0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797951
0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385241
0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385238281932550279412177187787689095831
0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797954757619200941271014131921438523828193255027941217718778768909589395497833016775104278900532217412


This shows empirically that all but the last 5 digits are correct. Compared to your floating point result



0.000000061212048763081839


we see that 7 digits are correct. Using the modified formula by Andrei
$$
frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}
$$

gives the floating point result



0.000000061212049284920693 


which is further away from the true result, it does not cure the catastrophic cancellation.





If you have two numbers $a,b$ that coincide in $d$ leading digits, then for any smooth function $f$ you will also get coincidence in about $d$ leading digits of $f(a)$ and $f(b)$. This means that the difference $f(a)-f(b)$ has $15-d$ correct digits. There is no way to force more precision, as that is not present in the original floating point values.



Note that this is different from the situation where you want to compute $f(a+h)-f(a)$ where $h$ is explicitly known. Then you can apply Taylor formulas or just a mean value argument like $f(a+h)-f(a)approx f(a+h/2)cdot h$. This will have almost as many correct digits as $h$ has for $|h|<10^{-5}cdot |a|$.






share|cite|improve this answer











$endgroup$





















    1












    $begingroup$

    To assess the accuracy of your computation analytically you need to understand the errors in each piece. The subtraction is a problem because the numbers are represented by a fixed number of bits of mantissa. This represents a fractional error in the number of about $2^{-n}$ where $n$ is the number of bits. If we know $n$ for the computer in use we know what is left after the subtraction. The math functions should be accurate to (just about) the same number of bits.



    We can compute $sqrt{5280} approx 72.6636$ so the size of the difference is about $frac {6.212cdot 10^{-8}}{72.6636}approx 8.5cdot 10^{-10}$ of the numbers themselves. The base $2$ log of this is about $-30$ so you are losing $30$ bits to the subtraction. If you use $64$ bit floats according to the IEEE specification, that leaves $23$ bits of accuracy. As $2^{-23} approx 10^{-7}$ you have about seven decimal digits of accuracy.






    share|cite|improve this answer









    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "69"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      noCode: true, onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3060893%2fnumerical-accuracy-how-many-digits-are-correct%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1












      $begingroup$

      Using a multi-precision package like the CAS Magma you can compute a value where perhaps the last 10 of 200 digits are polluted by truncation and rounding errors. The loop using different target digit numbers



      for N in [0..5] do 
      RR := RealField(40+32*N);
      Exp(Pi(RR)*(RR!67)^(1/2)/6) - (RR!5280)^(1/2);
      end for;


      gives the result



      0.000000061212043348401464497838533606
      0.00000006121204334840146449783853360999610568722086907308823071985234
      0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797951
      0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385241
      0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385238281932550279412177187787689095831
      0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797954757619200941271014131921438523828193255027941217718778768909589395497833016775104278900532217412


      This shows empirically that all but the last 5 digits are correct. Compared to your floating point result



      0.000000061212048763081839


      we see that 7 digits are correct. Using the modified formula by Andrei
      $$
      frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}
      $$

      gives the floating point result



      0.000000061212049284920693 


      which is further away from the true result, it does not cure the catastrophic cancellation.





      If you have two numbers $a,b$ that coincide in $d$ leading digits, then for any smooth function $f$ you will also get coincidence in about $d$ leading digits of $f(a)$ and $f(b)$. This means that the difference $f(a)-f(b)$ has $15-d$ correct digits. There is no way to force more precision, as that is not present in the original floating point values.



      Note that this is different from the situation where you want to compute $f(a+h)-f(a)$ where $h$ is explicitly known. Then you can apply Taylor formulas or just a mean value argument like $f(a+h)-f(a)approx f(a+h/2)cdot h$. This will have almost as many correct digits as $h$ has for $|h|<10^{-5}cdot |a|$.






      share|cite|improve this answer











      $endgroup$


















        1












        $begingroup$

        Using a multi-precision package like the CAS Magma you can compute a value where perhaps the last 10 of 200 digits are polluted by truncation and rounding errors. The loop using different target digit numbers



        for N in [0..5] do 
        RR := RealField(40+32*N);
        Exp(Pi(RR)*(RR!67)^(1/2)/6) - (RR!5280)^(1/2);
        end for;


        gives the result



        0.000000061212043348401464497838533606
        0.00000006121204334840146449783853360999610568722086907308823071985234
        0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797951
        0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385241
        0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385238281932550279412177187787689095831
        0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797954757619200941271014131921438523828193255027941217718778768909589395497833016775104278900532217412


        This shows empirically that all but the last 5 digits are correct. Compared to your floating point result



        0.000000061212048763081839


        we see that 7 digits are correct. Using the modified formula by Andrei
        $$
        frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}
        $$

        gives the floating point result



        0.000000061212049284920693 


        which is further away from the true result, it does not cure the catastrophic cancellation.





        If you have two numbers $a,b$ that coincide in $d$ leading digits, then for any smooth function $f$ you will also get coincidence in about $d$ leading digits of $f(a)$ and $f(b)$. This means that the difference $f(a)-f(b)$ has $15-d$ correct digits. There is no way to force more precision, as that is not present in the original floating point values.



        Note that this is different from the situation where you want to compute $f(a+h)-f(a)$ where $h$ is explicitly known. Then you can apply Taylor formulas or just a mean value argument like $f(a+h)-f(a)approx f(a+h/2)cdot h$. This will have almost as many correct digits as $h$ has for $|h|<10^{-5}cdot |a|$.






        share|cite|improve this answer











        $endgroup$
















          1












          1








          1





          $begingroup$

          Using a multi-precision package like the CAS Magma you can compute a value where perhaps the last 10 of 200 digits are polluted by truncation and rounding errors. The loop using different target digit numbers



          for N in [0..5] do 
          RR := RealField(40+32*N);
          Exp(Pi(RR)*(RR!67)^(1/2)/6) - (RR!5280)^(1/2);
          end for;


          gives the result



          0.000000061212043348401464497838533606
          0.00000006121204334840146449783853360999610568722086907308823071985234
          0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797951
          0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385241
          0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385238281932550279412177187787689095831
          0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797954757619200941271014131921438523828193255027941217718778768909589395497833016775104278900532217412


          This shows empirically that all but the last 5 digits are correct. Compared to your floating point result



          0.000000061212048763081839


          we see that 7 digits are correct. Using the modified formula by Andrei
          $$
          frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}
          $$

          gives the floating point result



          0.000000061212049284920693 


          which is further away from the true result, it does not cure the catastrophic cancellation.





          If you have two numbers $a,b$ that coincide in $d$ leading digits, then for any smooth function $f$ you will also get coincidence in about $d$ leading digits of $f(a)$ and $f(b)$. This means that the difference $f(a)-f(b)$ has $15-d$ correct digits. There is no way to force more precision, as that is not present in the original floating point values.



          Note that this is different from the situation where you want to compute $f(a+h)-f(a)$ where $h$ is explicitly known. Then you can apply Taylor formulas or just a mean value argument like $f(a+h)-f(a)approx f(a+h/2)cdot h$. This will have almost as many correct digits as $h$ has for $|h|<10^{-5}cdot |a|$.






          share|cite|improve this answer











          $endgroup$



          Using a multi-precision package like the CAS Magma you can compute a value where perhaps the last 10 of 200 digits are polluted by truncation and rounding errors. The loop using different target digit numbers



          for N in [0..5] do 
          RR := RealField(40+32*N);
          Exp(Pi(RR)*(RR!67)^(1/2)/6) - (RR!5280)^(1/2);
          end for;


          gives the result



          0.000000061212043348401464497838533606
          0.00000006121204334840146449783853360999610568722086907308823071985234
          0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797951
          0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385241
          0.00000006121204334840146449783853360999610568722086907308823071985202814147609091678626333360127979547576192009412710141319214385238281932550279412177187787689095831
          0.0000000612120433484014644978385336099961056872208690730882307198520281414760909167862633336012797954757619200941271014131921438523828193255027941217718778768909589395497833016775104278900532217412


          This shows empirically that all but the last 5 digits are correct. Compared to your floating point result



          0.000000061212048763081839


          we see that 7 digits are correct. Using the modified formula by Andrei
          $$
          frac{e^{2pisqrt{67}/6}-5280}{e^{pisqrt{67}/6}+sqrt{5280}}
          $$

          gives the floating point result



          0.000000061212049284920693 


          which is further away from the true result, it does not cure the catastrophic cancellation.





          If you have two numbers $a,b$ that coincide in $d$ leading digits, then for any smooth function $f$ you will also get coincidence in about $d$ leading digits of $f(a)$ and $f(b)$. This means that the difference $f(a)-f(b)$ has $15-d$ correct digits. There is no way to force more precision, as that is not present in the original floating point values.



          Note that this is different from the situation where you want to compute $f(a+h)-f(a)$ where $h$ is explicitly known. Then you can apply Taylor formulas or just a mean value argument like $f(a+h)-f(a)approx f(a+h/2)cdot h$. This will have almost as many correct digits as $h$ has for $|h|<10^{-5}cdot |a|$.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Jan 3 at 20:05

























          answered Jan 3 at 19:51









          LutzLLutzL

          57.2k42054




          57.2k42054























              1












              $begingroup$

              To assess the accuracy of your computation analytically you need to understand the errors in each piece. The subtraction is a problem because the numbers are represented by a fixed number of bits of mantissa. This represents a fractional error in the number of about $2^{-n}$ where $n$ is the number of bits. If we know $n$ for the computer in use we know what is left after the subtraction. The math functions should be accurate to (just about) the same number of bits.



              We can compute $sqrt{5280} approx 72.6636$ so the size of the difference is about $frac {6.212cdot 10^{-8}}{72.6636}approx 8.5cdot 10^{-10}$ of the numbers themselves. The base $2$ log of this is about $-30$ so you are losing $30$ bits to the subtraction. If you use $64$ bit floats according to the IEEE specification, that leaves $23$ bits of accuracy. As $2^{-23} approx 10^{-7}$ you have about seven decimal digits of accuracy.






              share|cite|improve this answer









              $endgroup$


















                1












                $begingroup$

                To assess the accuracy of your computation analytically you need to understand the errors in each piece. The subtraction is a problem because the numbers are represented by a fixed number of bits of mantissa. This represents a fractional error in the number of about $2^{-n}$ where $n$ is the number of bits. If we know $n$ for the computer in use we know what is left after the subtraction. The math functions should be accurate to (just about) the same number of bits.



                We can compute $sqrt{5280} approx 72.6636$ so the size of the difference is about $frac {6.212cdot 10^{-8}}{72.6636}approx 8.5cdot 10^{-10}$ of the numbers themselves. The base $2$ log of this is about $-30$ so you are losing $30$ bits to the subtraction. If you use $64$ bit floats according to the IEEE specification, that leaves $23$ bits of accuracy. As $2^{-23} approx 10^{-7}$ you have about seven decimal digits of accuracy.






                share|cite|improve this answer









                $endgroup$
















                  1












                  1








                  1





                  $begingroup$

                  To assess the accuracy of your computation analytically you need to understand the errors in each piece. The subtraction is a problem because the numbers are represented by a fixed number of bits of mantissa. This represents a fractional error in the number of about $2^{-n}$ where $n$ is the number of bits. If we know $n$ for the computer in use we know what is left after the subtraction. The math functions should be accurate to (just about) the same number of bits.



                  We can compute $sqrt{5280} approx 72.6636$ so the size of the difference is about $frac {6.212cdot 10^{-8}}{72.6636}approx 8.5cdot 10^{-10}$ of the numbers themselves. The base $2$ log of this is about $-30$ so you are losing $30$ bits to the subtraction. If you use $64$ bit floats according to the IEEE specification, that leaves $23$ bits of accuracy. As $2^{-23} approx 10^{-7}$ you have about seven decimal digits of accuracy.






                  share|cite|improve this answer









                  $endgroup$



                  To assess the accuracy of your computation analytically you need to understand the errors in each piece. The subtraction is a problem because the numbers are represented by a fixed number of bits of mantissa. This represents a fractional error in the number of about $2^{-n}$ where $n$ is the number of bits. If we know $n$ for the computer in use we know what is left after the subtraction. The math functions should be accurate to (just about) the same number of bits.



                  We can compute $sqrt{5280} approx 72.6636$ so the size of the difference is about $frac {6.212cdot 10^{-8}}{72.6636}approx 8.5cdot 10^{-10}$ of the numbers themselves. The base $2$ log of this is about $-30$ so you are losing $30$ bits to the subtraction. If you use $64$ bit floats according to the IEEE specification, that leaves $23$ bits of accuracy. As $2^{-23} approx 10^{-7}$ you have about seven decimal digits of accuracy.







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered Jan 3 at 20:19









                  Ross MillikanRoss Millikan

                  293k23197371




                  293k23197371






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Mathematics Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3060893%2fnumerical-accuracy-how-many-digits-are-correct%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith