• No results found

Machine-Precision Numbers

In document Mathematica Book.pdf (Page 31-35)

Whenever machine-precision numbers appear in a calculation, the whole calculation is typically done in machine precision. Mathematica will then give machine-precision numbers as the result.

Whenever the input contains any machine-precision numbers, Mathematica does the computa-tion to machine precision.

In[1]:= 1.4444444444444444444 ^ 5.7

Out[1]= 8.13382

[email protected] yields a machine-precision result, so the N is irrelevant.

In[2]:= N@[email protected], 30D

Out[2]= 1.02338

This gives a higher-precision result.

In[3]:= N@Zeta@56 ê 10D, 30D

Out[3]= 1.02337547922702991086041788103

When you do calculations with arbitrary-precision numbers, as discussed in "Arbitrary-Precision Numbers", Mathematica always keeps track of the precision of your results, and gives only those digits which are known to be correct, given the precision of your input. When you do calculations with machine-precision numbers, however, Mathematica always gives you a machine-precision result, whether or not all the digits in the result can, in fact, be determined to be correct on the basis of your input.

When you do calculations with arbitrary-precision numbers, as discussed in "Arbitrary-Precision Numbers", Mathematica always keeps track of the precision of your results, and gives only those digits which are known to be correct, given the precision of your input. When you do calculations with machine-precision numbers, however, Mathematica always gives you a machine-precision result, whether or not all the digits in the result can, in fact, be determined to be correct on the basis of your input.

This subtracts two machine-precision numbers.

In[4]:= diff = 1.11111111 - 1.11111000

Out[4]= 1.11 µ 10-6

The result is taken to have machine precision.

In[5]:= Precision@diffD

Out[5]= MachinePrecision

Here are all the digits in the result.

In[6]:= InputForm@diffD

Out[6]//InputForm= 1.1099999999153454`*^-6

The fact that you can get spurious digits in machine-precision numerical calculations with Mathe-matica is in many respects quite unsatisfactory. The ultimate reason, however, that Mathemat-ica uses fixed precision for these calculations is a matter of computational efficiency.

Mathematica is usually set up to insulate you as much as possible from the details of the com-puter system you are using. In dealing with machine-precision numbers, you would lose too much, however, if Mathematica did not make use of some specific features of your computer.

The important point is that almost all computers have special hardware or microcode for doing floating-point calculations to a particular fixed precision. Mathematica makes use of these features when doing machine-precision numerical calculations.

The typical arrangement is that all machine-precision numbers in Mathematica are represented as "double-precision floating-point numbers" in the underlying computer system. On most current computers, such numbers contain a total of 64 binary bits, typically yielding 16 decimal digits of mantissa.

The main advantage of using the built-in floating-point capabilities of your computer is speed.

Arbitrary-precision numerical calculations, which do not make such direct use of these capabili-ties, are usually many times slower than machine-precision calculations.

The main advantage of using the built-in floating-point capabilities of your computer is speed.

Arbitrary-precision numerical calculations, which do not make such direct use of these capabili-ties, are usually many times slower than machine-precision calculations.

There are several disadvantages of using built-in floating-point capabilities. One already mentioned is that it forces all numbers to have a fixed precision, independent of what precision can be justified for them.

A second disadvantage is that the treatment of machine-precision numbers can vary slightly from one computer system to another. In working with machine-precision numbers, Mathemat-ica is at the mercy of the floating-point arithmetic system of each particular computer. If float-ing-point arithmetic is done differently on two computers, you may get slightly different results for machine-precision Mathematica calculations on those computers.

$MachinePrecision the number of decimal digits of precision

$MachineEpsilon the minimum positive machine-precision number which can be added to 1.0 to give a result distinguishable from 1.0

$MaxMachineNumber the maximum machine-precision number

$MinMachineNumber the minimum positive machine-precision number

$MaxNumber the maximum magnitude of an arbitrary-precision number

$MinNumber the minimum magnitude of a positive arbitrary-precision number

Properties of numbers on a particular computer system.

Since machine-precision numbers on any particular computer system are represented by a definite number of binary bits, numbers which are too close together will have the same bit pattern, and so cannot be distinguished. The parameter $MachineEpsilon gives the distance between 1.0 and the closest number which has a distinct binary representation.

This gives the value of $MachineEpsilon for the computer system on which these examples are run.

In[7]:= $MachineEpsilon

Out[7]= 2.22045 µ 10-16

Although this prints as 1., Mathematica knows that the result is larger than 1.

In[8]:= 1. + $MachineEpsilon

Out[8]= 1.

InputForm reveals that the result is not exactly 1.

InputForm reveals that the result is not exactly 1.

In[9]:= % êê InputForm

Out[9]//InputForm= 1.0000000000000002

Subtracting 1 gives $MachineEpsilon.

In[10]:= % - 1.

Out[10]= 2.22045 µ 10-16

This prints as 1. also.

In[11]:= 1. + $MachineEpsilon ê 2

Out[11]= 1.

In this case, however, the result is not distinguished from 1. to machine precision.

In[12]:= % êê InputForm

Out[12]//InputForm= 1.

Subtracting 1 from the result yields 0.

In[13]:= % - 1.

Out[13]= 0.

Machine numbers have not only limited precision, but also limited magnitude. If you generate a number which lies outside the range specified by $MinMachineNumber and $MaxMachineNumber, Mathematica will automatically convert the number to arbitrary-precision form.

This is the maximum machine-precision number which can be handled on the computer system used for this example.

In[14]:= $MaxMachineNumber

Out[14]= 1.79769 µ 10308

Mathematica automatically converts any result larger than $MaxMachineNumber to arbitrary precision.

In[15]:= 2 $MaxMachineNumber

Out[15]= 3.595386269724631 µ 10308

Here is another computation whose result is outside the range of machine-precision numbers.

In[16]:= [email protected]

Out[16]= 1.970071114017 µ 10434

In document Mathematica Book.pdf (Page 31-35)