How to avoid this type of change of digits in computed numbers?












1














Is there a way to avoid the digits being changed?



In[117]:= v=124.58061;
v1=v-0.00024;
v2=v+0.00024;
In[120]:= FullForm[v]
FullForm[v1]
FullForm[v2]
Out[120]//FullForm= 124.58061`
Out[121]//FullForm= 124.58036999999999`
Out[122]//FullForm= 124.58085`
In[123]:= FullForm[Interval[{v1,v2}]]
Out[123]//FullForm= Interval[List[124.58036999999997`,124.58085000000001`]]


I just want 124.58037 and 124.58085 in these outputs. For some other values of v, the Out[122] would also get a small bit change.



How could I avoid the changes? Eventually I want to save these numbers (which is part of a larger structured expression) into files, what's the right way to do it without causing additional changes to the numbers?










share|improve this question






















  • 124.58036999999999 is exactly the same as 124.58037 - there is no change in the number. Evaluate SameQ[124.58036999999999, 124.58037] to see. If you are trying to write this number to a file and want it written in a certain way, then the question should clarify that.
    – Jason B.
    Dec 19 at 2:07










  • Do you realize that the actual numbers are stored as binary64 reals? The change, if you want to call it that, occurs when M displays the numbers as the closest 6-digit approximation. What you see in the Front End is not the actual number.
    – Michael E2
    Dec 19 at 2:09










  • If it is SameQ, why not using the more intuitive form for both display and writing?
    – qazwsx
    Dec 19 at 2:39










  • @qazwsx Because Mathematica is not consistent with how it displays numbers to the right of the decimal point. There are plans to make it more consistent in version 12.
    – Robert Jacobson
    Dec 19 at 3:13
















1














Is there a way to avoid the digits being changed?



In[117]:= v=124.58061;
v1=v-0.00024;
v2=v+0.00024;
In[120]:= FullForm[v]
FullForm[v1]
FullForm[v2]
Out[120]//FullForm= 124.58061`
Out[121]//FullForm= 124.58036999999999`
Out[122]//FullForm= 124.58085`
In[123]:= FullForm[Interval[{v1,v2}]]
Out[123]//FullForm= Interval[List[124.58036999999997`,124.58085000000001`]]


I just want 124.58037 and 124.58085 in these outputs. For some other values of v, the Out[122] would also get a small bit change.



How could I avoid the changes? Eventually I want to save these numbers (which is part of a larger structured expression) into files, what's the right way to do it without causing additional changes to the numbers?










share|improve this question






















  • 124.58036999999999 is exactly the same as 124.58037 - there is no change in the number. Evaluate SameQ[124.58036999999999, 124.58037] to see. If you are trying to write this number to a file and want it written in a certain way, then the question should clarify that.
    – Jason B.
    Dec 19 at 2:07










  • Do you realize that the actual numbers are stored as binary64 reals? The change, if you want to call it that, occurs when M displays the numbers as the closest 6-digit approximation. What you see in the Front End is not the actual number.
    – Michael E2
    Dec 19 at 2:09










  • If it is SameQ, why not using the more intuitive form for both display and writing?
    – qazwsx
    Dec 19 at 2:39










  • @qazwsx Because Mathematica is not consistent with how it displays numbers to the right of the decimal point. There are plans to make it more consistent in version 12.
    – Robert Jacobson
    Dec 19 at 3:13














1












1








1


1





Is there a way to avoid the digits being changed?



In[117]:= v=124.58061;
v1=v-0.00024;
v2=v+0.00024;
In[120]:= FullForm[v]
FullForm[v1]
FullForm[v2]
Out[120]//FullForm= 124.58061`
Out[121]//FullForm= 124.58036999999999`
Out[122]//FullForm= 124.58085`
In[123]:= FullForm[Interval[{v1,v2}]]
Out[123]//FullForm= Interval[List[124.58036999999997`,124.58085000000001`]]


I just want 124.58037 and 124.58085 in these outputs. For some other values of v, the Out[122] would also get a small bit change.



How could I avoid the changes? Eventually I want to save these numbers (which is part of a larger structured expression) into files, what's the right way to do it without causing additional changes to the numbers?










share|improve this question













Is there a way to avoid the digits being changed?



In[117]:= v=124.58061;
v1=v-0.00024;
v2=v+0.00024;
In[120]:= FullForm[v]
FullForm[v1]
FullForm[v2]
Out[120]//FullForm= 124.58061`
Out[121]//FullForm= 124.58036999999999`
Out[122]//FullForm= 124.58085`
In[123]:= FullForm[Interval[{v1,v2}]]
Out[123]//FullForm= Interval[List[124.58036999999997`,124.58085000000001`]]


I just want 124.58037 and 124.58085 in these outputs. For some other values of v, the Out[122] would also get a small bit change.



How could I avoid the changes? Eventually I want to save these numbers (which is part of a larger structured expression) into files, what's the right way to do it without causing additional changes to the numbers?







precision accuracy digits number-form






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 19 at 0:09









qazwsx

3,88612659




3,88612659












  • 124.58036999999999 is exactly the same as 124.58037 - there is no change in the number. Evaluate SameQ[124.58036999999999, 124.58037] to see. If you are trying to write this number to a file and want it written in a certain way, then the question should clarify that.
    – Jason B.
    Dec 19 at 2:07










  • Do you realize that the actual numbers are stored as binary64 reals? The change, if you want to call it that, occurs when M displays the numbers as the closest 6-digit approximation. What you see in the Front End is not the actual number.
    – Michael E2
    Dec 19 at 2:09










  • If it is SameQ, why not using the more intuitive form for both display and writing?
    – qazwsx
    Dec 19 at 2:39










  • @qazwsx Because Mathematica is not consistent with how it displays numbers to the right of the decimal point. There are plans to make it more consistent in version 12.
    – Robert Jacobson
    Dec 19 at 3:13


















  • 124.58036999999999 is exactly the same as 124.58037 - there is no change in the number. Evaluate SameQ[124.58036999999999, 124.58037] to see. If you are trying to write this number to a file and want it written in a certain way, then the question should clarify that.
    – Jason B.
    Dec 19 at 2:07










  • Do you realize that the actual numbers are stored as binary64 reals? The change, if you want to call it that, occurs when M displays the numbers as the closest 6-digit approximation. What you see in the Front End is not the actual number.
    – Michael E2
    Dec 19 at 2:09










  • If it is SameQ, why not using the more intuitive form for both display and writing?
    – qazwsx
    Dec 19 at 2:39










  • @qazwsx Because Mathematica is not consistent with how it displays numbers to the right of the decimal point. There are plans to make it more consistent in version 12.
    – Robert Jacobson
    Dec 19 at 3:13
















124.58036999999999 is exactly the same as 124.58037 - there is no change in the number. Evaluate SameQ[124.58036999999999, 124.58037] to see. If you are trying to write this number to a file and want it written in a certain way, then the question should clarify that.
– Jason B.
Dec 19 at 2:07




124.58036999999999 is exactly the same as 124.58037 - there is no change in the number. Evaluate SameQ[124.58036999999999, 124.58037] to see. If you are trying to write this number to a file and want it written in a certain way, then the question should clarify that.
– Jason B.
Dec 19 at 2:07












Do you realize that the actual numbers are stored as binary64 reals? The change, if you want to call it that, occurs when M displays the numbers as the closest 6-digit approximation. What you see in the Front End is not the actual number.
– Michael E2
Dec 19 at 2:09




Do you realize that the actual numbers are stored as binary64 reals? The change, if you want to call it that, occurs when M displays the numbers as the closest 6-digit approximation. What you see in the Front End is not the actual number.
– Michael E2
Dec 19 at 2:09












If it is SameQ, why not using the more intuitive form for both display and writing?
– qazwsx
Dec 19 at 2:39




If it is SameQ, why not using the more intuitive form for both display and writing?
– qazwsx
Dec 19 at 2:39












@qazwsx Because Mathematica is not consistent with how it displays numbers to the right of the decimal point. There are plans to make it more consistent in version 12.
– Robert Jacobson
Dec 19 at 3:13




@qazwsx Because Mathematica is not consistent with how it displays numbers to the right of the decimal point. There are plans to make it more consistent in version 12.
– Robert Jacobson
Dec 19 at 3:13










2 Answers
2






active

oldest

votes


















1














The Problem



There are two distinct issues at work:




  1. Why is the result of the simple arithmetic operation different from what you'd expect?

  2. How can we change the display (as opposed to the value) of a decimal number?


How "Real" numbers are represented internally



The answer to the first question is simple: Some numbers cannot be exactly represented in binary (base 2) with a finite number of digits, just as $frac{1}{3} = 0.overline{3}$ cannot be expressed in decimal (base 10) with a finite number of digits. Mathematica represents Real numbers internally as a single binary number called a floating point number, or float for short. On most systems, a 64-bit float only holds 53 binary digits for the mantissa (think scientific notation). You can see this for your numbers using RealDigits:



RealDigits[124.58061, 2, 54]



{{1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 
0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, Indeterminate}, 7}



That last Indeterminate is telling us there are no more known digits, and the 7 says how many digits are to the right of the decimal point (binary point?), a number Mathematica calls Accuracy. The bottom line is, converting from a finite number of decimal digits (what you entered) to a finite number of binary digits (internal representation) and then back again (for display) is not guaranteed to preserve the original input. This is fundamentally unavoidable when using floats.



How "Real" numbers are displayed



But with 53 binary digits at our disposal, you have ~14 decimal digits to the right of the decimal point to work with, which is more than enough for your desired 5 sig figs. Now your problem is how Mathematica is choosing to display the numbers to you. Mathematica is not consistent here. None of your three numbers has a terminating binary representation, but that's not apparent from the output of FullForm.



The Solution



Change the display



Since you have plenty of digits of precision to work with, it seems to me you want to change the display of decimal numbers. You can do this with DecimalForm. Here is the wrong way to use DecimalForm:



In[1]:= v = DecimalForm[124.58061, 9]
In[2]:= v1=v-0.00024



Out[1]//DecimalForm = 124.58061
Out[2]= -0.00024+124.58061



The number -0.00024 is displayed to 2 sig figs, just as it was entered, while v continues to be displayed with 9 sig figs. Here's the right way:



In[3]:= DecimalForm[v=124.58061 , 9]
DecimalForm[v1=v-0.00024, 9]



Out[3]//DecimalForm= 124.58061
Out[4]//DecimalForm= 124.58037



Notice the placement of the = (assignment) operator. Keep in mind this only changes the display of the numbers, not the internal representation.



Change the internal representation



Alternatively, you can avoid using Mathematica Real numbers altogether. The best way is to use rationals instead as Bill suggests in his answer.



In[5]:= v=Rationalize[124.58061,0]
v1=v-Rationalize[0.00024,0]
v2=v+Rationalize[0.00024,0]



Out[5]= 12458061/100000
Out[6]= 12458037/100000
Out[7]= 2491617/20000



In each case, Mathematica has just reduced the fraction $displaystyle frac{xcdot 10^5}{10^5}$. The N function will then give you as many decimal digits of precision as you'd like:



In[8]:= N[v2, 20]



Out[8]= 124.58085000000000000






share|improve this answer





























    2














    Try this



    v=Rationalize[124.58061,0];
    v1=v-Rationalize[0.00024,0];
    v2=v+Rationalize[0.00024,0];
    FullForm[v+0.]
    FullForm[v1+0.]
    FullForm[v2+0.]


    which shows me 124.58061, 124.58037 and 124.58085.



    But expecting floating point binary math to provide exact decimal results may be an ongoing source of frustration and problems.






    share|improve this answer





















      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: "387"
      };
      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: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188128%2fhow-to-avoid-this-type-of-change-of-digits-in-computed-numbers%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














      The Problem



      There are two distinct issues at work:




      1. Why is the result of the simple arithmetic operation different from what you'd expect?

      2. How can we change the display (as opposed to the value) of a decimal number?


      How "Real" numbers are represented internally



      The answer to the first question is simple: Some numbers cannot be exactly represented in binary (base 2) with a finite number of digits, just as $frac{1}{3} = 0.overline{3}$ cannot be expressed in decimal (base 10) with a finite number of digits. Mathematica represents Real numbers internally as a single binary number called a floating point number, or float for short. On most systems, a 64-bit float only holds 53 binary digits for the mantissa (think scientific notation). You can see this for your numbers using RealDigits:



      RealDigits[124.58061, 2, 54]



      {{1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 
      0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1,
      1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, Indeterminate}, 7}



      That last Indeterminate is telling us there are no more known digits, and the 7 says how many digits are to the right of the decimal point (binary point?), a number Mathematica calls Accuracy. The bottom line is, converting from a finite number of decimal digits (what you entered) to a finite number of binary digits (internal representation) and then back again (for display) is not guaranteed to preserve the original input. This is fundamentally unavoidable when using floats.



      How "Real" numbers are displayed



      But with 53 binary digits at our disposal, you have ~14 decimal digits to the right of the decimal point to work with, which is more than enough for your desired 5 sig figs. Now your problem is how Mathematica is choosing to display the numbers to you. Mathematica is not consistent here. None of your three numbers has a terminating binary representation, but that's not apparent from the output of FullForm.



      The Solution



      Change the display



      Since you have plenty of digits of precision to work with, it seems to me you want to change the display of decimal numbers. You can do this with DecimalForm. Here is the wrong way to use DecimalForm:



      In[1]:= v = DecimalForm[124.58061, 9]
      In[2]:= v1=v-0.00024



      Out[1]//DecimalForm = 124.58061
      Out[2]= -0.00024+124.58061



      The number -0.00024 is displayed to 2 sig figs, just as it was entered, while v continues to be displayed with 9 sig figs. Here's the right way:



      In[3]:= DecimalForm[v=124.58061 , 9]
      DecimalForm[v1=v-0.00024, 9]



      Out[3]//DecimalForm= 124.58061
      Out[4]//DecimalForm= 124.58037



      Notice the placement of the = (assignment) operator. Keep in mind this only changes the display of the numbers, not the internal representation.



      Change the internal representation



      Alternatively, you can avoid using Mathematica Real numbers altogether. The best way is to use rationals instead as Bill suggests in his answer.



      In[5]:= v=Rationalize[124.58061,0]
      v1=v-Rationalize[0.00024,0]
      v2=v+Rationalize[0.00024,0]



      Out[5]= 12458061/100000
      Out[6]= 12458037/100000
      Out[7]= 2491617/20000



      In each case, Mathematica has just reduced the fraction $displaystyle frac{xcdot 10^5}{10^5}$. The N function will then give you as many decimal digits of precision as you'd like:



      In[8]:= N[v2, 20]



      Out[8]= 124.58085000000000000






      share|improve this answer


























        1














        The Problem



        There are two distinct issues at work:




        1. Why is the result of the simple arithmetic operation different from what you'd expect?

        2. How can we change the display (as opposed to the value) of a decimal number?


        How "Real" numbers are represented internally



        The answer to the first question is simple: Some numbers cannot be exactly represented in binary (base 2) with a finite number of digits, just as $frac{1}{3} = 0.overline{3}$ cannot be expressed in decimal (base 10) with a finite number of digits. Mathematica represents Real numbers internally as a single binary number called a floating point number, or float for short. On most systems, a 64-bit float only holds 53 binary digits for the mantissa (think scientific notation). You can see this for your numbers using RealDigits:



        RealDigits[124.58061, 2, 54]



        {{1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 
        0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1,
        1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, Indeterminate}, 7}



        That last Indeterminate is telling us there are no more known digits, and the 7 says how many digits are to the right of the decimal point (binary point?), a number Mathematica calls Accuracy. The bottom line is, converting from a finite number of decimal digits (what you entered) to a finite number of binary digits (internal representation) and then back again (for display) is not guaranteed to preserve the original input. This is fundamentally unavoidable when using floats.



        How "Real" numbers are displayed



        But with 53 binary digits at our disposal, you have ~14 decimal digits to the right of the decimal point to work with, which is more than enough for your desired 5 sig figs. Now your problem is how Mathematica is choosing to display the numbers to you. Mathematica is not consistent here. None of your three numbers has a terminating binary representation, but that's not apparent from the output of FullForm.



        The Solution



        Change the display



        Since you have plenty of digits of precision to work with, it seems to me you want to change the display of decimal numbers. You can do this with DecimalForm. Here is the wrong way to use DecimalForm:



        In[1]:= v = DecimalForm[124.58061, 9]
        In[2]:= v1=v-0.00024



        Out[1]//DecimalForm = 124.58061
        Out[2]= -0.00024+124.58061



        The number -0.00024 is displayed to 2 sig figs, just as it was entered, while v continues to be displayed with 9 sig figs. Here's the right way:



        In[3]:= DecimalForm[v=124.58061 , 9]
        DecimalForm[v1=v-0.00024, 9]



        Out[3]//DecimalForm= 124.58061
        Out[4]//DecimalForm= 124.58037



        Notice the placement of the = (assignment) operator. Keep in mind this only changes the display of the numbers, not the internal representation.



        Change the internal representation



        Alternatively, you can avoid using Mathematica Real numbers altogether. The best way is to use rationals instead as Bill suggests in his answer.



        In[5]:= v=Rationalize[124.58061,0]
        v1=v-Rationalize[0.00024,0]
        v2=v+Rationalize[0.00024,0]



        Out[5]= 12458061/100000
        Out[6]= 12458037/100000
        Out[7]= 2491617/20000



        In each case, Mathematica has just reduced the fraction $displaystyle frac{xcdot 10^5}{10^5}$. The N function will then give you as many decimal digits of precision as you'd like:



        In[8]:= N[v2, 20]



        Out[8]= 124.58085000000000000






        share|improve this answer
























          1












          1








          1






          The Problem



          There are two distinct issues at work:




          1. Why is the result of the simple arithmetic operation different from what you'd expect?

          2. How can we change the display (as opposed to the value) of a decimal number?


          How "Real" numbers are represented internally



          The answer to the first question is simple: Some numbers cannot be exactly represented in binary (base 2) with a finite number of digits, just as $frac{1}{3} = 0.overline{3}$ cannot be expressed in decimal (base 10) with a finite number of digits. Mathematica represents Real numbers internally as a single binary number called a floating point number, or float for short. On most systems, a 64-bit float only holds 53 binary digits for the mantissa (think scientific notation). You can see this for your numbers using RealDigits:



          RealDigits[124.58061, 2, 54]



          {{1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 
          0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1,
          1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, Indeterminate}, 7}



          That last Indeterminate is telling us there are no more known digits, and the 7 says how many digits are to the right of the decimal point (binary point?), a number Mathematica calls Accuracy. The bottom line is, converting from a finite number of decimal digits (what you entered) to a finite number of binary digits (internal representation) and then back again (for display) is not guaranteed to preserve the original input. This is fundamentally unavoidable when using floats.



          How "Real" numbers are displayed



          But with 53 binary digits at our disposal, you have ~14 decimal digits to the right of the decimal point to work with, which is more than enough for your desired 5 sig figs. Now your problem is how Mathematica is choosing to display the numbers to you. Mathematica is not consistent here. None of your three numbers has a terminating binary representation, but that's not apparent from the output of FullForm.



          The Solution



          Change the display



          Since you have plenty of digits of precision to work with, it seems to me you want to change the display of decimal numbers. You can do this with DecimalForm. Here is the wrong way to use DecimalForm:



          In[1]:= v = DecimalForm[124.58061, 9]
          In[2]:= v1=v-0.00024



          Out[1]//DecimalForm = 124.58061
          Out[2]= -0.00024+124.58061



          The number -0.00024 is displayed to 2 sig figs, just as it was entered, while v continues to be displayed with 9 sig figs. Here's the right way:



          In[3]:= DecimalForm[v=124.58061 , 9]
          DecimalForm[v1=v-0.00024, 9]



          Out[3]//DecimalForm= 124.58061
          Out[4]//DecimalForm= 124.58037



          Notice the placement of the = (assignment) operator. Keep in mind this only changes the display of the numbers, not the internal representation.



          Change the internal representation



          Alternatively, you can avoid using Mathematica Real numbers altogether. The best way is to use rationals instead as Bill suggests in his answer.



          In[5]:= v=Rationalize[124.58061,0]
          v1=v-Rationalize[0.00024,0]
          v2=v+Rationalize[0.00024,0]



          Out[5]= 12458061/100000
          Out[6]= 12458037/100000
          Out[7]= 2491617/20000



          In each case, Mathematica has just reduced the fraction $displaystyle frac{xcdot 10^5}{10^5}$. The N function will then give you as many decimal digits of precision as you'd like:



          In[8]:= N[v2, 20]



          Out[8]= 124.58085000000000000






          share|improve this answer












          The Problem



          There are two distinct issues at work:




          1. Why is the result of the simple arithmetic operation different from what you'd expect?

          2. How can we change the display (as opposed to the value) of a decimal number?


          How "Real" numbers are represented internally



          The answer to the first question is simple: Some numbers cannot be exactly represented in binary (base 2) with a finite number of digits, just as $frac{1}{3} = 0.overline{3}$ cannot be expressed in decimal (base 10) with a finite number of digits. Mathematica represents Real numbers internally as a single binary number called a floating point number, or float for short. On most systems, a 64-bit float only holds 53 binary digits for the mantissa (think scientific notation). You can see this for your numbers using RealDigits:



          RealDigits[124.58061, 2, 54]



          {{1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 
          0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1,
          1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, Indeterminate}, 7}



          That last Indeterminate is telling us there are no more known digits, and the 7 says how many digits are to the right of the decimal point (binary point?), a number Mathematica calls Accuracy. The bottom line is, converting from a finite number of decimal digits (what you entered) to a finite number of binary digits (internal representation) and then back again (for display) is not guaranteed to preserve the original input. This is fundamentally unavoidable when using floats.



          How "Real" numbers are displayed



          But with 53 binary digits at our disposal, you have ~14 decimal digits to the right of the decimal point to work with, which is more than enough for your desired 5 sig figs. Now your problem is how Mathematica is choosing to display the numbers to you. Mathematica is not consistent here. None of your three numbers has a terminating binary representation, but that's not apparent from the output of FullForm.



          The Solution



          Change the display



          Since you have plenty of digits of precision to work with, it seems to me you want to change the display of decimal numbers. You can do this with DecimalForm. Here is the wrong way to use DecimalForm:



          In[1]:= v = DecimalForm[124.58061, 9]
          In[2]:= v1=v-0.00024



          Out[1]//DecimalForm = 124.58061
          Out[2]= -0.00024+124.58061



          The number -0.00024 is displayed to 2 sig figs, just as it was entered, while v continues to be displayed with 9 sig figs. Here's the right way:



          In[3]:= DecimalForm[v=124.58061 , 9]
          DecimalForm[v1=v-0.00024, 9]



          Out[3]//DecimalForm= 124.58061
          Out[4]//DecimalForm= 124.58037



          Notice the placement of the = (assignment) operator. Keep in mind this only changes the display of the numbers, not the internal representation.



          Change the internal representation



          Alternatively, you can avoid using Mathematica Real numbers altogether. The best way is to use rationals instead as Bill suggests in his answer.



          In[5]:= v=Rationalize[124.58061,0]
          v1=v-Rationalize[0.00024,0]
          v2=v+Rationalize[0.00024,0]



          Out[5]= 12458061/100000
          Out[6]= 12458037/100000
          Out[7]= 2491617/20000



          In each case, Mathematica has just reduced the fraction $displaystyle frac{xcdot 10^5}{10^5}$. The N function will then give you as many decimal digits of precision as you'd like:



          In[8]:= N[v2, 20]



          Out[8]= 124.58085000000000000







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 19 at 3:11









          Robert Jacobson

          680613




          680613























              2














              Try this



              v=Rationalize[124.58061,0];
              v1=v-Rationalize[0.00024,0];
              v2=v+Rationalize[0.00024,0];
              FullForm[v+0.]
              FullForm[v1+0.]
              FullForm[v2+0.]


              which shows me 124.58061, 124.58037 and 124.58085.



              But expecting floating point binary math to provide exact decimal results may be an ongoing source of frustration and problems.






              share|improve this answer


























                2














                Try this



                v=Rationalize[124.58061,0];
                v1=v-Rationalize[0.00024,0];
                v2=v+Rationalize[0.00024,0];
                FullForm[v+0.]
                FullForm[v1+0.]
                FullForm[v2+0.]


                which shows me 124.58061, 124.58037 and 124.58085.



                But expecting floating point binary math to provide exact decimal results may be an ongoing source of frustration and problems.






                share|improve this answer
























                  2












                  2








                  2






                  Try this



                  v=Rationalize[124.58061,0];
                  v1=v-Rationalize[0.00024,0];
                  v2=v+Rationalize[0.00024,0];
                  FullForm[v+0.]
                  FullForm[v1+0.]
                  FullForm[v2+0.]


                  which shows me 124.58061, 124.58037 and 124.58085.



                  But expecting floating point binary math to provide exact decimal results may be an ongoing source of frustration and problems.






                  share|improve this answer












                  Try this



                  v=Rationalize[124.58061,0];
                  v1=v-Rationalize[0.00024,0];
                  v2=v+Rationalize[0.00024,0];
                  FullForm[v+0.]
                  FullForm[v1+0.]
                  FullForm[v2+0.]


                  which shows me 124.58061, 124.58037 and 124.58085.



                  But expecting floating point binary math to provide exact decimal results may be an ongoing source of frustration and problems.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 at 0:42









                  Bill

                  5,52569




                  5,52569






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Mathematica 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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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.


                      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%2fmathematica.stackexchange.com%2fquestions%2f188128%2fhow-to-avoid-this-type-of-change-of-digits-in-computed-numbers%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

                      How did Captain America manage to do this?

                      迪纳利

                      南乌拉尔铁路局