Repository navigation
Power, Sqrt: precision from the condition number, as in WMA - #2052
Merged
Merged
Conversation
This was referenced Oct 19, 2026
tombouctou
force-pushed
the
power-real-valued-precision
branch
from
October 19, 2026 20:29
a1e9eb3 to
adf3b06
Compare
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>
eval_N is documented as the same as N[expr, p], but it kept whatever precision the computation gave. With Sqrt adding Log10[2], and the 1/2 in Sqrt[2] made inexact by N, eval_N[Sqrt[2], $MachinePrecision] came out with precision 15.07, while N gives 15.9546, as WMA does. It now uses the same evaluation as N, without the message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As asked in review: the arguments, the value and the factors are mpmath numbers. They stay so, rather than floats: a tiny factor such as that of Tanh[10.`20^6] would underflow to 0. as a float. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tombouctou
force-pushed
the
power-real-valued-precision
branch
from
October 19, 2026 21:18
7a6e2b4 to
5ff0c56
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of step 6 of the outline in #2047, after #2051 (merged). The PRs for
Log(#2053), the trigonometric and hyperbolic functions (#2054–#2056),SetPrecision(#2057) and low-precision values (#2060) are stacked on this one.PowerandSqrtget their precision from the condition number, as WMA does. The relative error ofb^eisAbs[e]times that ofb, plusAbs[e Log[b]]times that ofe.Precision[Sqrt[1.5`20]]20.20.30120.301Precision[1.5`20^3]20.19.522919.5229Precision[1.5`20^1000]20.17.17.Precision[1.5`20^(10^-5)]20.25.25.Precision[2^1.5`20]20.19.983119.9831Precision[10^100.`20]20.17.637817.6378Precision[1.5`20^2.5`20]20.19.454219.4542Precision[Exp[1.5`20]]20.19.823919.8239Changes
MPMathFunction.condition_numbers(mathics/core/builtin.py), new and optional. Given the arguments and the value as mpmath numbers, it returns the factorAbs[x D[f, x] / f]for each argument. The follow-up PRs give it toLog, the trigonometric and the hyperbolic functions; each of those PRs only adds the formulas.eval_mpmath_function_by_condition(mathics/eval/numeric.py) uses it when the arguments are exact reals or nonzeroPrecisionReals and the value is a nonzero real. The relative errors add up, as inTimesfrom PrecisionReal: keep a real-valued precision, as WMA does (#2047) #2050. When the result has more digits than the arguments (Sqrt,x^(10^-5)), the value is computed again with enough bits. Everything else (complex results, machine numbers, zeros) takes the path it took before.Powergives(Abs[e], Abs[e Log[b]]). The factors are typed asmpmath.mpf, not floats: a tiny one would underflow to0..Sqrt[x]isx^(1/2), andExp[x]isE^x.eval_N(the internalN) now evaluates as theNbuiltin does since N[expr, p]: precision exactly p, with extra digits as needed, as in WMA #2051, without the message. InsideN, the1/2ofSqrt[2]becomes0.5with the requested digits. With this PR its error counts, soeval_N[Sqrt[2], $MachinePrecision]came out with precision15.07.Ngives15.9546, as WMA does. Two cases intest_evaluators.pynow expect the WMA value ofN[Sqrt[2], $MachinePrecision]andN[Pi, $MachinePrecision].Not changed: WMA counts an error for
PiinPi^1.5`20(19.4925), but not forEinE^1.5`20. Since #2050, exact constants add no error, so this PR gives19.7652.Tests
test_power_real_valued_precisionintest/builtin/atomic/test_numbers.py, with WMA 15 values, and the digits of two results.Full run on kimmy (
pytest -n 4anddocpipeline.py):test_gamma.py) pass on their own.Benchmark
kimmy, best of 7, this PR next to #2051, 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)Do[Sqrt[N[k, 50]], {k, 2000}]Do[N[k, 50]^3, {k, 2000}]Do[2^N[k, 50], {k, 2000}]No slowdown.
🤖 Generated with Claude Code