Numeric Precision in Mathcad
by Steve Teixeira
|
Numeric precision in Mathcad can be a bit difficult to understand. Consider a simple definition: |
|
When you evaluate x, you may be surprised by the result: |
|
Does Mathcad really represent x to be 0? |
|
The answer is no. Mathcad represents x to be as you defined it: |
|
Unfortunately, you've just discovered how Mathcad's ability to format results for display can appear to yield an incorrect result. See Zero Threshold below for the display difference between the two results above. |
|
Internal versus Display Precision |
|
The concept of precision in Mathcad actually involves two different types of precision: |
|
Internal precision, which affects how well Mathcad represents the number, and
Display precision, which controls how results appear in your worksheets. |
|
Internally, Mathcad maintains all numbers to double precision (64-bit) floating point format, as described in IEEE (Institute of Electrical and Electronics Engineers, Inc.) Standard 754, which most computer systems use. Floating point representation represents real numbers in scientific notation, as shown below. The advantage of floating point representation of numbers versus fixed point representation is that very large numbers and very small numbers can be represented more precisely. Read from the left, the first bit represents the sign of the number (negative or positive), the next 11 bits represent the exponent, and the remaining 52 bits represent the mantissa, or coefficient. S, E, and M are all either 0 or 1, because all the numbers are in Base 2. |
|
S EEEEEEEEEEE MMMMMMMMMMM...MMMMMMMMMM |
|
Mathcad performs all its numeric calculations to this standard, which does have several limitations. Binary floating point representation can not represent every number in the decimal system, so there is some rounding that occurs at very small numbers generally beyond the 12th decimal place of a number. This limitation is common to most computer applications.
The real number system has infinitely many numbers between any two numbers, while the binary computer system can only represent a finite amount of numbers, although this finite number is very large. So when you ask a computer system such as Mathcad to represent a number, it rounds it to the closest available number that it can represent. This difference as stated above is generally beyond the 12th decimal place of a number. |
|
Significant Digits
Mathcad maintains 17 significant digits for numbers. |
|
Consider the following two definitions: |
|
When evaluated, they yield the same result, because Mathcad does not maintain the 18th significant digit. |
|
Note that Mathcad didn't round the value of b. Since Mathcad cannot store an 18th significant digit, it doesn't know how the 17th significant digit should be rounded for display. |
|
The maximum and minimum values allowed by IEEE 754 are: |
|
Closest to zero (smallest absolute value): ±2− 1022, roughly: |
|
Farthest from zero (largest absolute value): ±((1-(1/2)53)*21024), roughly: |
|
Values are from the Wikipedia topic "IEEE 754", accessed July 11, 2007. |
|
Mathcad allows you to format your results in several ways, through the Number Format and Tolerance tabs of the Result Format dialog. While these options can significantly affect how Mathcad displays a result, none of the changes you make to these tabs affects how Mathcad stores the result. |
|
You can click in a blank area of your worksheet, and choose Result from the Format Menu, to set all the results in the worksheet. A good practice is to set global results in a template that you can start with for each new worksheet.
Note that global results won't override any individual results that you have set previously. If a global setting doesn't change a specific result, check that individual settings have not been made for that result. If so, you can reset them. |
|
Setting Individual Results |
|
If you want to set an individual result for an equation, then just click on the result, then choose Result from the Format menu. The individual setting overrides the global setting. |
|
Number of decimal places — the number of digits Mathcad displays for a result. Results with greater precision are rounded for display only. |
|
For the numbers , this is the maximum number of digits to the right of the decimal point that Mathcad displays: |
|
For all other numbers, the maximum number of digits to the right of the decimal point that Mathcad displays is N-n, where N is the specified number of decimal places and n is the number of digits to the left of the decimal point: |
|
In this last case, the 3 digits to the left of the decimal point count against the 17 places allowed. Thus Mathcad only displays 14 (17-3) digits to the right of the decimal point. |
|
Zero threshold — how close to zero a number must be before Mathcad displays it as zero. |
|
This option can cause you to misinterpret small results as zero: |
|
Other Result Format dialog settings |
|
The Result Format Dialog provides several other options for customizing the display of results in Mathcad, though these options do not affect the apparent result of an expression. More information on these options can be found in online Help under "Formatting Results" and the Mathcad tutorial "Numbers and Precision", both available from the Help menu in Mathcad. |
|
Display Format - Global versus Local |
|
Mathcad supports the following default and maximum values for these options: |
|
You can change these and other Result Format default values either for your entire worksheet or for a particular result. (The ability to set the zero tolerance for a specific result, rather than all results, is an enhancement in Mathcad 14.) |
|
Issues to remember when copying results: |
|
Results are copied as displayed. |
|
When you copy a result, Mathcad only copies the displayed digits, rather than its true, internal value. Consider a number with 5 decimal digits:
|
|
Result Format set to 3 decimal places |
|
By default, Mathcad displays the result with only 3 decimal digits. If you copy this rounded result and paste it elsewhere, the pasted region does not retain any knowledge of the extra digits, even if you try to display them: |
|
Copied and pasted result set to 5 decimal places |
|
For this reason, you should refer to the variable whenever possible, rather than copy its result. |
|
Variable Result Format set to 5 decimal places |
|
Results retain all associated formatting, overriding any worksheet-level options.
|
Right-click, choose Save Target As, and change the extension to XMCD and File Type to All to download Mathcad file (version Mathcad 14).
|
Was this article interesting? Let us know.
|
[PRINTER FRIENDLY VERSION]
|
|
|  |
 |
 |
|