Repository navigation
N[expr, p]: precision exactly p, with extra digits as needed, as in WMA - #2051
Conversation
Part of step 6 of #2047 (MATH-227). - N[expr, p] gives each arbitrary-precision number in the result precision p, as WMA does, also for a non-integer p: Precision[N[Pi, 20.3]] is 20.3 (was 20.). - When the computation loses digits, it is done again with more digits, up to $MaxExtraPrecision more: Precision[N[Pi E, 20]] is 20. again (it became 19.699 with #2050), and Precision[N[Pi - 314159/100000, 20]] is 20. (was 13.6). A number whose input has fewer digits keeps them. - New $MaxExtraPrecision (50. by default) and N::meprec, as in WMA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| summary_text = "settable global maximum precision bound variable" | ||
|
|
||
|
|
||
| class MaxExtraPrecision(Predefined): |
There was a problem hiding this comment.
When a new built-in is added, SYMBOLS_MANIFEST.txt should be updated.
You can run admin-tools/build_and_check_manifest.py to automatically update this. (When I just did, I also noticed that XMLXMLObjectImport` needs to be added.)
There was a problem hiding this comment.
Done in e16f1b4: I reran admin-tools/build_and_check_manifest.py, which adds $MaxExtraPrecision. It also adds PolynomialMod, which was missing on master, and puts TagUnset in order.
About XMLXMLObjectImport: it is already in the manifest, as its last line. The script printed the wrong name. In the loop over new symbols it printed name, a variable left over from the loop before, which ends on the last manifest entry. So each new symbol (here $MaxExtraPrecisionandPolynomialMod) was reported as XMLXMLObjectImport. The same commit makes it print the missing symbol.
As asked in review: rebuilt with admin-tools/build_and_check_manifest.py. PolynomialMod was missing on master too, and TagUnset is now in order. build_and_check_manifest.py printed the name of the last manifest entry (XML`XMLObjectImport) for each new symbol, instead of the new symbol itself: a variable left over from the loop before. It now prints the symbol that is missing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Part of step 6 of #2047 (MATH-227), on top of #2051. MPMathFunction gets an optional condition_numbers: for each argument, the factor Abs[x D[f, x] / f] by which its relative error is multiplied in the result. When the arguments are exact reals or nonzero PrecisionReals and the value is a nonzero real, these errors add up, as in WMA. Power gives Abs[e] for the base and Abs[e Log[b]] for the exponent; Sqrt[x] is x^(1/2). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
As before, I think this is awesome! |
|
LGTM |
Part of step 6 of #2047 (MATH-227), on top of #2051. MPMathFunction gets an optional condition_numbers: for each argument, the factor Abs[x D[f, x] / f] by which its relative error is multiplied in the result. When the arguments are exact reals or nonzero PrecisionReals and the value is a nonzero real, these errors add up, as in WMA. Power gives Abs[e] for the base and Abs[e Log[b]] for the exponent; Sqrt[x] is x^(1/2). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Part of step 6 of the outline in #2047, after #2050:
N[expr, p]gives precisionp, as WMA does.Precision[N[Pi, 20.3]]20.20.320.3Precision[N[Pi, 41/2]]20.20.520.5Precision[N[Pi E, 20]]19.69920.20.Precision[N[Pi - 314159/100000, 20]]13.625720.20.Precision[N[1.5`10, 20.3]]10.10.10.Precision /@ N[{Pi, 1.5`10, 2.5`30.2}, 20.3]{20., 10., 20.}{20.3, 10., 20.3}{20.3, 10., 20.3}$MaxExtraPrecision$MaxExtraPrecision50.50.N[Pi E, 20]giving19.699came in with #2050:NmakesPiandE20-digit numbers, andTimesnow adds their errors.Changes
N[expr, p](eval_NValues_to_precisioninmathics/eval/nevaluator.py) first evaluates atp, as before. If a nonzero arbitrary-precision number in the result has fewer thanpdigits, it evaluates again with the digits lost added, up to$MaxExtraPrecisionmore thanp. It goes on while some number short ofpdigits gains digits; one that gains nothing has an input with fewer thanpdigits, and keeps them. Then every number abovepis set to precisionp.N[a, 20] = 11,N[a Pi, 20]must not turn back intoa 3.14…at 21 digits. The first pass is still at exactlyp, so a rule given forpapplies as before.$MaxExtraPrecision, new,50.by default, no attributes. Assigning it anything other than a non-negative number orInfinitygives$MaxExtraPrecision::precset, as in WMA.N::meprecwhen the extra digits are not enough. As in WMA, there is no message when$MaxExtraPrecisionis0.N[expr, {p, a}]and the internaleval_Nare unchanged.Tests
test_n_real_valued_precisionandtest_max_extra_precisionintest/builtin/atomic/test_numbers.py, with WMA 15 values.$MaxExtraPrecision.RootSumdoctestN[Integrate[1/(x^5 + 11 x + 1), {x, 1, 3}], 50]expected…857550894. On master that number has precision 49.66, so its last digits were off. Now it is…857553407, as in WMA (…8575534094…;NIntegrateat 70 digits agrees).Full run on kimmy (Python 3,
pytest -n 4anddocpipeline.py), next to master 9f166b1:$MaxExtraPrecision).test_tensor.py,test_testing_expressions.py) pass when their files run on their own.Benchmark
kimmy, best of 7, both runs at the same time:
Total[Table[N[1/k, 50], {k, 2000}]]Times @@ Table[N[1 + 1/k, 50], {k, 2000}]Do[x = N[k, 50] + Pi, {k, 2000}]Total[Table[1./k, {k, 2000}]](machine reals, control)Do[N[Pi E + Sqrt[2], 30], {k, 300}]Do[N[Sin[k] + Cos[k], 30], {k, 300}]The benchmark from #2050 doesn't change. An
N[expr, p]whose first pass loses digits now costs about one more evaluation (the last two rows): that is the second pass.Still to do from step 6
PlusandTimes(Sqrt,Power,Exp, …): precision from the condition number.SetPrecision,SetAccuracy.Precision[1.5`20 - 1.4`2]).Precisionof a list with a machine number.🤖 Generated with Claude Code