PrecisionConstant
PrecisionConstant
PrecisionConstant[ value, n ] represents a number rounded to n decimals, which also stands for all values that round to it.
PrecisionConstant[ value, n ]
PrecisionConstant[ value, n ] is displayed as value with exactly n decimals. It represents every number that rounds to that value, so it can be used for measured or rounded values. Calculations keep track of the precision, and when an answer is compared with a PrecisionConstant, any value within its precision matches. Approximate creates a PrecisionConstant from any numerical value.
| Example | in | displayed |
|---|---|---|
Two decimals: 3.1 is displayed as 3.10. |
PrecisionConstant[ 3.1, 2 ] |
|
The value is rounded to n decimals for display. |
PrecisionConstant[ 3.14159, 2 ] |
|
| Calculations keep the precision. | CalcPlus[ PrecisionConstant[3.14, 2], 1 ] |
| Example | in | out |
|---|---|---|
3.1415 rounds to 3.14, so it matches. |
IsMatch[ PrecisionConstant[3.14, 2], 3.1415, Method=>accuracy ] |
True |
LowerBound and UpperBound
Set the range of values that the number represents explicitly, instead of deriving it from the rounding. The upper bound is not included.
| Syntax | Example | out |
|---|---|---|
PrecisionConstant[ value, n, LowerBound=>a, UpperBound=>b ] |
IsMatch[ PrecisionConstant[2.2, 1, LowerBound=>2.1, UpperBound=>2.3], 2.25, Method=>accuracy ] |
True |
IsMatch[ PrecisionConstant[2.2, 1, LowerBound=>2.1, UpperBound=>2.3], 2.35, Method=>accuracy ] |
False |
Precision
Give the number of significant digits instead of the number of decimals; the second argument is then left out.
| Syntax | Example | displayed |
|---|---|---|
PrecisionConstant[ value, Precision=>p ] |
PrecisionConstant[ 1234, Precision=>2 ] |
- Without
n:PrecisionConstant[ 3.14 ]is just the number3.14. valuemust be a number:PrecisionConstant[ x, 2 ]is left unevaluated.
| Invalid input | Result |
|---|---|
PrecisionConstant[ 3.14, 2, Foo=>1 ] |
Error: Invalid associative argument for PrecisionConstant: Foo |
== does not consider a PrecisionConstant equal to an ordinary number: PrecisionConstant[3.14159, 2] == 3.14 is False. Use IsMatch with Method=>accuracy to check whether a value is within the precision.
See also: Approximate, Precision, IsMatch, Numerical