Approximate
Approximate
Approximate[ expr, n ] rounds a numerical value to n decimals, and keeps track of the rounding.
Approximate[ expr, n ]
Approximate[ expr, n ] calculates the value of expr and rounds it to n decimals. The result is a PrecisionConstant: a number that remembers its precision. Later calculations use the exact value, and when an answer is checked, any value that rounds to the same number matches. Use Numerical instead if you only need a decimal number.
| Example | in | displayed |
|---|---|---|
π to 2 decimals. |
Approximate[ Pi, 2 ] |
|
| A fraction. | Approximate[ 1/3, 2 ] |
|
| An expression with a numerical value. | Approximate[ 2*Pi + 1, 2 ] |
|
| Halves are rounded up. | Approximate[ 2.45, 1 ] |
|
| Each element of a list. | Approximate[ {Pi, EulerE}, 2 ] |
Approximate[ expr ]
Without n, the value is calculated as a decimal number, as with Numerical.
| Example | in | out |
|---|---|---|
The value of π. |
Approximate[ Pi ] |
3.14159265359 |
RoundingMode
How to round: 'Default' (halves up), 'Floor' (always down) or 'Ceil' (always up).
| Syntax | Example | displayed |
|---|---|---|
Approximate[ expr, n, RoundingMode=>'Floor' ] |
Approximate[ 2.45, 1, RoundingMode=>'Floor' ] |
|
Approximate[ expr, n, RoundingMode=>'Ceil' ] |
Approximate[ 2.41, 1, RoundingMode=>'Ceil' ] |
OmitTrailingZeroes
If set to False, the result is displayed with exactly n decimals. Default: True.
| Syntax | Example | displayed |
|---|---|---|
Approximate[ expr, n, OmitTrailingZeroes=>False ] |
Approximate[ 2.5, 3, OmitTrailingZeroes=>False ] |
RoundInteger
If set to True, integers are also given a precision. Default: False, so integers are not changed: Approximate[ 2, 1 ] gives 2.
| Syntax | Example | displayed |
|---|---|---|
Approximate[ expr, n, RoundInteger=>True ] |
Approximate[ 2, 1, RoundInteger=>True ] |
- Variables: an expression with variables is not changed:
Approximate[ x + Pi, 2 ]givesx+Pi. UseNumerical[ x + Pi, 2 ]to round the numbers in it.
| Invalid input | Result |
|---|---|
Approximate[ 2.45, 1, RoundingMode=>'Up' ] |
Error: Invalid value for RoundingMode: 'Up' |
See also: PrecisionConstant, Numerical, Round, Precision