Skip to content

Commit a0f456b

Browse files
authored
Merge pull request #553 from Mathics3/accuracy-and-precision-doc
Go over docs for Accuracy and Precision
2 parents 4302237 + 9be4fc5 commit a0f456b

File tree

3 files changed

+98
-48
lines changed

3 files changed

+98
-48
lines changed

mathics/builtin/atomic/numbers.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,20 @@ def convert_float(x, base, exponents):
148148

149149
class Accuracy(Builtin):
150150
"""
151+
<url>:Accuracy: https://en.wikipedia.org/wiki/Accuracy_and_precision</url> (WMA <url>:Accuracy: https://reference.wolfram.com/language/ref/Accuracy.html</url>)
152+
151153
<dl>
152154
<dt>'Accuracy[$x$]'
153155
<dd>examines the number of significant digits of $expr$ after the decimal point in the number x.
154156
</dl>
155-
This is rather a proof-of-concept than a full implementation.
156-
157+
<i>This is rather a proof-of-concept than a full implementation.</i>
157158
158159
Accuracy of a real number is estimated from its value and its precision:
159160
160161
>> Accuracy[3.1416`2]
161162
= 1.50298
162163
163-
Notice that the value is not exactly equal to the obtained in WMA: This is due to the different way in which
164-
Precision is handled in SymPy.
164+
Notice that the value is not exactly equal to the obtained in WMA: This is due to the different way in which 'Precision' is handled in SymPy.
165165
166166
Accuracy for exact atoms is $Infinity$:
167167
>> Accuracy[1]
@@ -176,9 +176,20 @@ class Accuracy(Builtin):
176176
Accuracy of expressions is given by the minimum accuracy of its elements:
177177
>> Accuracy[F[1, Pi, A]]
178178
= Infinity
179+
179180
>> Accuracy[F[1.3, Pi, A]]
180181
= 14.8861
181182
183+
'Accuracy' for the value 0 is a fixed-precision Real number:
184+
>> 0``2
185+
= 0.00
186+
187+
In compound expressions, the 'Accuracy' is fixed by the number with
188+
the lowest 'Accuracy':
189+
>> Accuracy[{{1, 1.`},{1.``5, 1.``10}}]
190+
= 5.
191+
192+
See also <url>:'Precision': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/precision/</url>.
182193
"""
183194

184195
summary_text = "find the accuracy of a number"
@@ -954,40 +965,41 @@ def apply(self, expr, evaluation):
954965

955966
class Precision(Builtin):
956967
"""
968+
<url>:Precision: https://en.wikipedia.org/wiki/Accuracy_and_precision</url> (WMA <url>:Precision: https://reference.wolfram.com/language/ref/Precision.html</url>)
969+
957970
<dl>
958971
<dt>'Precision[$expr$]'
959972
<dd>examines the number of significant digits of $expr$.
960973
</dl>
961-
This is rather a proof-of-concept than a full implementation.
962-
Precision of compound expression is not supported yet.
974+
975+
<i>This is rather a proof-of-concept than a full implementation.</i>
976+
977+
The precision of an exact number, e.g. an Integer, is 'Infinity':
978+
963979
>> Precision[1]
964980
= Infinity
981+
982+
A fraction is an exact number too, so its Precision is 'Infinity':
983+
965984
>> Precision[1/2]
966985
= Infinity
967-
>> Precision[0.5]
968-
= MachinePrecision
969986
970-
#> Precision[0.0]
971-
= MachinePrecision
972-
#> Precision[0.000000000000000000000000000000000000]
973-
= 0.
974-
#> Precision[-0.0]
975-
= MachinePrecision
976-
#> Precision[-0.000000000000000000000000000000000000]
977-
= 0.
987+
Numbers entered in the form $digits$`$p$ are taken to have precision $p$:
978988
979-
#> 1.0000000000000000 // Precision
980-
= MachinePrecision
981-
#> 1.00000000000000000 // Precision
982-
= 17.
989+
>> Precision[1.23`10]
990+
= 10.
983991
984-
#> 0.4 + 2.4 I // Precision
992+
Precision of a machine‐precision number is 'MachinePrecision':
993+
>> Precision[0.5]
985994
= MachinePrecision
986-
#> Precision[2 + 3 I]
987-
= Infinity
988995
989-
#> Precision["abc"]
990-
= Infinity
996+
In compound expressions, the 'Precision' is fixed by the number with
997+
the lowest 'Precision':
998+
>> Precision[{{1, 1.`},{1.`5, 1.`10}}]
999+
= 5.
1000+
1001+
1002+
See also <url>:'Accuracy': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/accuracy/</url>.
9911003
"""
9921004

9931005
rules = {

mathics/doc/documentation/1-Manual.mdoc

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -196,50 +196,46 @@ The result of the previous query to \Mathics can be accessed by '%':
196196

197197
<section title="Precision and Accuracy">
198198

199-
\Mathics handles relative ('Precision') and absolute ('Accuracy') uncertanties in numerical quantities. 'Precision' is set by adding a single reversed quote $`$
200-
and the numerical value of the precision right after the last digit of the mantissa. For example,
199+
\Mathics handles relative and absolute uncertanties in numerical quantities. The <em>precision</em> or relative accuracy, is set by adding a RawBackquote character ('`') and the number of digits of precision in the mantissa. For example:
201200

202-
>> a = 3.1416`3
201+
>> 3.1416`3
203202
= 3.14
204203

205-
set $a$ with a number having a relative uncertainty of $10^{-3}$, in a way that this number is numerically equivalent to $3.1413`4$:
204+
Above, two decimal places are shown in output after the decimal point, but three places of precision are stored.
206205

207-
>> a == 3.1413`4
206+
The relative uncertainty of '3.1416`3' is 10^-3. It is numerically equivalent, in three places after the decimal point, to 3.1413`4:
207+
208+
>> 3.1416`3 == 3.1413`4
208209
= True
209210

210-
We can recover the precision of the number by using 'Precision'
211+
We can get the precision of the number by using the \Mathics <url>:'Precision': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/precision/</url> function:
211212

212-
>> Precision[a]
213-
= 3.
213+
>> Precision[3.1413`4]
214+
= 4.
214215

215-
Up to the precision, $a$ is equivalent to $\pi$, so
216+
While 3.1419 not the closest approximation to Pi in 4 digits after the decimal point (or with precision 4), for 3 digits of precision it is:
216217

217-
>> Pi - a
218-
= 0.
219-
>> Pi == a
218+
>> Pi == 3.141987654321`3
220219
= True
221220

222-
In a similar way, 'Accuracy' is set by adding a double reversed quote $``$
223-
and the numerical value of the accuracy right after the last digit of the mantissa. For example,
221+
<url>The absolute accuracy of a number, is set by adding a two RawBackquotes '``' and the number digits.
222+
223+
For example:
224224

225-
>> a = 13.1416``4
225+
>> 13.1416``4
226226
= 13.142
227227

228-
set $a$ with a number having a absolute uncertainty of $10^{-4}$, in a way that this number is numerically equivalent to $13.1413``4$:
228+
is a number having a absolute uncertainty of 10^-4. This number is numerically equivalent to '13.1413``4':
229229

230-
>> a == 13.1413``4
230+
>> 13.1416``4 == 13.1413``4
231231
= True
232232

233-
For $0$, 'Precision' is ignored, since corresponds to a zero uncertainty:
234-
235-
>> 0`4
236-
= 0
237-
238-
while 'Accuracy' is taken into account by storing the value as a fixed precision Real number:
233+
The absolute accuracy for the value 0 is a fixed-precision Real number:
239234

240235
>> 0``4
241236
= 0.0000
242237

238+
See also <url>:Accuracy and precision: https://en.wikipedia.org/wiki/Accuracy_and_precision</url>.
243239
</section>
244240

245241

test/builtin/atomic/test_numbers.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,45 @@ def test_n():
175175
("N[1.01234567890123456789`, 2] // Precision", "MachinePrecision"),
176176
):
177177
check_evaluation(str_expr, str_expected)
178+
179+
180+
def test_accuracy():
181+
for str_expr, str_expected in (
182+
("0`4", "0"),
183+
("Accuracy[0.0]", "15."),
184+
("Accuracy[0.000000000000000000000000000000000000]", "36."),
185+
("Accuracy[-0.0]", "15."),
186+
# In WMA, this gives 36. Seems to be a rounding issue
187+
# ("Accuracy[-0.000000000000000000000000000000000000]", "36."),
188+
("1.0000000000000000 // Accuracy", "15."),
189+
("1.00000000000000000 // Accuracy", "17."),
190+
# Returns the accuracy of ```2.4```
191+
(" 0.4 + 2.4 I // Accuracy", "14.6198"),
192+
("Accuracy[2 + 3 I]", "Infinity"),
193+
('Accuracy["abc"]', "Infinity"),
194+
# Returns the accuracy of ``` 3.2`3 ```
195+
('Accuracy[F["a", 2, 3.2`3]]', "2.49482"),
196+
('Accuracy[{{a, 2, 3.2`},{2.1`5, 3.2`3, "a"}}]', "2.49482"),
197+
# Another case of issues with rounding. In Mathics, this returns
198+
# 2.67776
199+
# ('Accuracy[{{a, 2, 3.2`},{2.1``3, 3.2``5, "a"}}]', '3.'),
200+
):
201+
check_evaluation(str_expr, str_expected)
202+
203+
204+
def test_precision():
205+
for str_expr, str_expected in (
206+
("0`4", "0"),
207+
("Precision[0.0]", "MachinePrecision"),
208+
("Precision[0.000000000000000000000000000000000000]", "0."),
209+
("Precision[-0.0]", "MachinePrecision"),
210+
("Precision[-0.000000000000000000000000000000000000]", "0."),
211+
("1.0000000000000000 // Precision", "MachinePrecision"),
212+
("1.00000000000000000 // Precision", "17."),
213+
(" 0.4 + 2.4 I // Precision", "MachinePrecision"),
214+
("Precision[2 + 3 I]", "Infinity"),
215+
('Precision["abc"]', "Infinity"),
216+
('Precision[F["a", 2, 3.2`3]]', "3."),
217+
('Precision[{{a,2,3.2`},{2.1`5, 2.`3, "a"}}]', "3."),
218+
):
219+
check_evaluation(str_expr, str_expected)

0 commit comments

Comments
 (0)