Reputation: 353
Consider the following terminating decimal numbers.
3.1^2 = 9.61
3.1^4 = 92.3521
3.1^8 = 8528.91037441
The following shows how Mathematica treats these expressions
In[1]:= 3.1^2
Out[1]= 9.61
In[2]:= 3.1^4
Out[2]= 92.352
So far so good, but
In[3]:= 3.1^8
Out[3]= 8528.91
doesn't provide enough precision.
So let's try N[], NumberForm[], and DecimalForm[] with a precision of 12
In[4]:= N[3.1^8,12]
Out[4]= 8528.91
In[5]:= NumberForm[3.1^8,12]
Out[5]= 8528.91037441
In[6]:= DecimalForm[3.1^8,12]
Out[6]= 8528.91037441
In this case DecimialForm[] and NumberForm[] work as expected, but N[] only provided the default precision of 6, even though I asked for 12. So DecimalForm[] or NumberForm[] seem to be the way to go if you want exact results when the inputs are terminating decimals.
Next consider rational numbers with infinite repeating decimals like 1/3.
In[7]:= N[1/3,20]
Out[7]= 0.33333333333333333333
In[9]:= NumberForm[1/3, 20]
Out[9]=
1/3
In[9]:= DecimalForm[1/3, 20]
Out[9]=
1/3
Unlike the previous case, N[] seems to be the proper way to go here, whereas NumberForm[] and DecimalForm[] do not respect precisions.
Finally consider irrational numbers like Sqrt[2] and Pi.
In[10]:= N[Sqrt[2],20]
Out[10]= 1.4142135623730950488
In[11]:= NumberForm[Sqrt[2], 20]
Out[11]=
sqrt(2)
In[12]:= DecimalForm[Sqrt[2], 20]
Out[12]=
sqrt(2)
In[13]:= N[π^12,30]
Out[13]= 924269.181523374186222579170358
In[14]:= NumberForm[Pi^12,30]
Out[14]=
π^12
In[15]:= DecimalForm[Pi^12,30]
Out[15]=
π^12
In these cases N[] works, but NumberForm[] and DecimalForm[] do not. However, note that N[] switches to scientific notation at π^13, even with a larger precision. Is there a way to avoid this switch?
In[16]:= N[π^13,40]
Out[16]= 2.903677270613283404988596199487803130470*10^6
So there doesn't seem to be a consistent way of formulating how to get decimal numbers with requested precisions and at the same time avoiding scientific notation. Sometimes N[] works, othertimes DecimalForm[] or NumberForm[] works, and at othertimes nothing seems to work.
Have I missed something or are there bugs in the system?
Upvotes: 2
Views: 2218
Reputation: 594
It isn't a bug because it is designed purposefully to behave this way. Precision is limited by the precision of your machine, your configuration of Mathematica, and the algorithm and performance constraints of the calculation.
The documentation for N[expr, n]
states it attempts to give a result with n‐digit precision
. When it cannot give the requested precision it gets as close as it can. DecimalForm
and NumberForm
work the same way.
https://reference.wolfram.com/language/ref/N.html explains the various cases behind this:
expr
are exact, or of sufficiently high precision, N[expr,n]
may not be able to give results with n
‐digit precision.N[expr,n]
may internally do computations to more than n
digits of precision.
$MaxExtraPrecision
specifies the maximum number of extra digits of precision that will ever be used internally.n
is given in decimal digits; it need not be an integer.n
must lie between $MinPrecision
and $MaxPrecision
. $MaxPrecision
can be set to Infinity
.n
can be smaller than $MachinePrecision
.N[expr]
gives a machine‐precision number, so long as its magnitude is between $MinMachineNumber
and $MaxMachineNumber
.N[expr]
is equivalent to N[expr,MachinePrecision]
.N[0]
gives the number 0. with machine precision.N
converts all nonzero numbers to Real
or Complex
form.N
converts each successive argument of any function it encounters to numerical form, unless the head of the function has an attribute such as NHoldAll
.N[f[args]]:=value
and N[f[args],n]:=value
.N[expr,{p,a}]
attempts to generate a result with precision at most p
and accuracy at most a
.N[expr,{Infinity,a}]
attempts to generate a result with accuracy a
.N[expr,{Infinity,1}]
attempts to find a numerical approximation to the integer part of expr
.Upvotes: 4