Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Round[2.75,0.1] return 2.800000000003?

Mathematica 8.0.1

Any one could explain what would be the logic behind this result

In[24]:= Round[10.75, .1]

Out[24]= 10.8

In[29]:= Round[2.75, .1]

Out[29]= 2.8000000000000003

I have expected the second result above to be 2.8?

EDIT 1:

I was trying to do the above for formatting purposes only to make the number fit in the space. I ended up doing the following to get the result I want:

In[41]:= NumberForm[2.75,2]
Out[41]   2.8

I wish Mathematica has printf() like formatting function. I find formatting numbers in Mathematica for exact field width and form a little awkward compared to using printf() formatting rules.

EDIT 2: I tried $MaxExtraPrecision=1000 on some number I was trying for format/round, but it did not work, that is why I posted this question. Here it is

In[42]:= $MaxExtraPrecision=1000;
Round[2035.7520395261859,.1]

Out[43]= 2035.8000000000002


In[46]:= $MaxExtraPrecision=50;
Round[2.75,.1]

Out[47]= 2.8000000000000003

EDIT 3:

I found this way, to format a number to one decimal point only. Use Numberform, but first need to find what n-digit precision to use by counting the number of digits to the left of the decimal point, then adding 1.

In[56]:= x=2035.7520395261859;
NumberForm[x,IntegerLength[Round@x]+1]

Out[57]//NumberForm= 2035.8

EDIT 4:

The above (Edit 3) did not work for numbers such as

a=2.67301785 10^7

After some trials, I found Accounting Form to do what I want. AccountingForm gets rid of the 10^n form which NumberForm did not:

In[76]:= x=2035.7520395261859;
AccountingForm[x,IntegerLength[Round@x]+1]

Out[77]//AccountingForm= 2035.8

In[78]:= x=2.67301785 10^7;
AccountingForm[x,IntegerLength[Round@x]+1]

Out[79]//AccountingForm= 26730178.5

For formatting numerical values, the best language I found was Fortran, followed COBOL and also by those languages that use or support printf() standard formatting. With Mathematica, one can do such formatting I am sure, but it sure seems too complicated to me. I never understood why Mathematics does not have Printf[].

like image 719
Nasser Avatar asked Jun 13 '11 13:06

Nasser


2 Answers

Not all decimal (base 10) numbers with a finite number of digits are representable in binary (base 2) with a finite number of digits. E.g. 0.1 is not representable in binary, just like 1/3 ~= 0.33333... is not representable in decimal. Mathematica (and other software) will only use a limited number of decimal digits when showing the number to hide this effect. However, occasionally it might happen that enough decimal digits are shown that the mismatch becomes visible.

http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding

EDIT

This command will show you what happens when you find the closes binary representation of 0.1 using 20 binary digits, then convert it back to decimal:

RealDigits[FromDigits[RealDigits[1/10, 2, 20], 2], 10]
like image 199
Szabolcs Avatar answered Nov 04 '22 18:11

Szabolcs


The number is stored in base 2, rather than base 10 (decimal). It's impossible to represent 2.8 in base 2, so it uses the closest value: 2.8000000000000003

like image 34
bradley.ayers Avatar answered Nov 04 '22 19:11

bradley.ayers