Author Notes
In general, the
Precision of an approximate real number is the effective number of decimal digits in it which are treated as significant for computations. In contrast to the other types of approximate numbers (the arbitrary‐precision numbers), machine numbers contain a fixed number of digits (
MachinePrecision). Since they maintain no information on their precision, all their digits are treated as significant during computations.
In rare occasions one wants to prescribe the effective number of digits in a machine number, putting zeros into the trail and perhaps modifying the last significant digit. In everyday life, this operation is called rounding, but the Wolfram Language function
Round is not designed specifically for machine numbers. It is not straightforward to use
Round to set explicitly the number of significant digits in the decimal representation of a machine number.
The resource function DecimalRound takes a machine number and the number of required digits and attempts to provide the result as a machine number with exactly the requested number of significant digits.
In the following example, numlist consists of five numbers, originally having wildly spread precisions. For instance, the first number is the AvogadroConstant in SI which is perfectly defined by an international standard. On the other hand, the fourth number is the result of a calculation to approximate π via dividing 22 by 7 and the indicated precision is itself the result of an approximate calculation. Here is the list:
When we create the list of machine numbers, we lose some information on precision (for instance our approximate π will look like a 16-digit good approximation), but computations and storage will be more efficient in the world of machine numbers:
Interestingly, some information is preserved, for instance the Avogadro number will still show that it has nine "good" digits, albeit only implicitly, by the fact that seven trailing digits are zeros. If we want to convey our knowledge regarding the approximate π, we should round it to, say, three digits, as we would do it in a hand calculation. DecimalRound can do this.
To be more general, in this example we create a variable which contains possible requirements for the number of significant digits:
Then DecimalRound is called with various combinations of the two arguments:
In the output the column heading shows the rounded machine numbers. The row heading shows the requested digit counts. We use
FullForm to see what we are really having in the result matrix:
Since the function is
Listable, we can easily get a whole row of the matrix as well as as a whole column: