Repository navigation
Add SetPrecision and SetAccuracy, as in WMA - #2057
Merged
Merged
Conversation
tombouctou
force-pushed
the
hyperbolic-real-valued-precision
branch
from
October 19, 2026 20:29
87d89dc to
5958913
Compare
tombouctou
force-pushed
the
set-precision
branch
from
October 19, 2026 20:29
6a739bc to
f87005c
Compare
tombouctou
force-pushed
the
hyperbolic-real-valued-precision
branch
from
October 19, 2026 21:17
5958913 to
79b3423
Compare
tombouctou
force-pushed
the
set-precision
branch
from
October 19, 2026 21:17
f87005c to
2ce29b5
Compare
tombouctou
force-pushed
the
hyperbolic-real-valued-precision
branch
from
October 19, 2026 21:19
79b3423 to
6eb4ce9
Compare
tombouctou
force-pushed
the
set-precision
branch
from
October 19, 2026 21:19
2ce29b5 to
0fa94b6
Compare
tombouctou
force-pushed
the
hyperbolic-real-valued-precision
branch
from
October 19, 2026 21:49
6eb4ce9 to
77e79f6
Compare
tombouctou
force-pushed
the
set-precision
branch
from
October 19, 2026 21:49
0fa94b6 to
a375705
Compare
rocky
reviewed
Oct 19, 2026
| # exact value), or None for MachinePrecision. | ||
| Target = Optional[float] | ||
|
|
||
| LOG10_2 = math.log10(2) |
Member
There was a problem hiding this comment.
Small nit: for constants like this, we've been using
from typing import Final
LOG10_2: Final[float] = math.log10(2)
Collaborator
Author
Part of step 6 of #2047 (MATH-227), on top of the hyperbolic PR. - SetPrecision[expr, p] sets every number in expr to precision p, and evaluates numeric constants such as Pi to it. An inexact number keeps its binary value: SetPrecision[0.1, 20] is 0.10000000000000000555. As in WMA, an exact exponent stays exact (SetPrecision[Sqrt[2], 20] has precision 20.301), and a machine or arbitrary-precision zero becomes 0. p can be MachinePrecision, or Infinity for the exact binary values. - SetAccuracy[expr, a] does the same with accuracy a; a number with no digit left becomes a zero of accuracy a. - Complex numbers: both parts get the same accuracy, so that the number has precision (or accuracy) p, as in WMA. - Messages SetPrecision::precbd, ::precsm, ::preclg and SetAccuracy::accbd, as in WMA. - Complex: the object cache was keyed on the parts' values and whole-bit precision, so two numbers differing only in a real-valued precision (#2050) were the same object. The key now has the parts' precisions. - number._get_float_inf becomes get_float_inf, for SetPrecision. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tombouctou
force-pushed
the
hyperbolic-real-valued-precision
branch
from
October 19, 2026 22:04
77e79f6 to
03d2771
Compare
tombouctou
force-pushed
the
set-precision
branch
from
October 19, 2026 22:04
a375705 to
537a396
Compare
Member
|
LGTM - if you want to address the nit, that's up to you. Merge when you feel ready. |
As asked in review, as other module constants are. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tombouctou
added a commit
that referenced
this pull request
Oct 19, 2026
As other module constants are (review on #2057). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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. On top of the hyperbolic PR, #2056 (and the PRs below it):
SetPrecisionevaluates after setting the numbers, soSetPrecision[Sin[2], 20]needsSin's rule.SetPrecisionandSetAccuracydid not exist; they came back unevaluated. They now work as in WMA 15:SetPrecision[1/3, 20]0.33333333333333333333SetPrecision[0.1, 20]0.10000000000000000555: an inexact number keeps its binary valueSetPrecision[f[Pi, x], 20]f[3.1415926535897932385, x]: numeric constants are evaluatedSetPrecision[0.1, Infinity]3602879701896397 / 36028797018963968SetPrecision[1.520, MachinePrecision]`1.5Precision[SetPrecision[Sin[2], 20]]20.0384: the numbers are set, then the expression is evaluatedPrecision[SetPrecision[Sqrt[2], 20]]20.301: an exact exponent stays exactSetPrecision[0., 20]0: a machine or arbitrary-precision zero becomes an exact0Precision /@ {Re[#], Im[#]} &[SetPrecision[1.5 + 2.5 I, 20.3]]{20.1619, 20.3837}: both parts get the same accuracy, so that the number has precision 20.3Precision[SetAccuracy[150.5, 20]]22.1775Accuracy[SetAccuracy[0.001, 2]]2.: no digit left, so a zeroSetPrecision[1.5, a]SetPrecision::precbdSetPrecision[1.5, -1]SetPrecision::precsm, then$MinPrecisionis usedSetAccuracy[1.5, a]SetAccuracy::accbdChanges
SetPrecisionandSetAccuracyinmathics/builtin/atomic/numbers.py, next toPrecisionandAccuracy. The work is in a new module,mathics/eval/numbers/precision.py.Complex: the object cache was keyed on the parts' values and whole-bit precision. Since PrecisionReal: keep a real-valued precision, as WMA does (#2047) #2050, two complex numbers that differ only in a real-valued precision were the same object:SetAccuracy[1.5 + 2.5 I, 20]returned the result ofSetPrecision[1.5 + 2.5 I, 20.3]evaluated just before it. The key now has the parts' precisions, asPrecisionReal's key does.number._get_float_infbecomesget_float_inf, used bySetPrecisionfor$MinPrecisionand$MaxPrecision.Tests
test_set_precision,test_set_accuracyandtest_set_precision_messagesintest/builtin/atomic/test_numbers.py, with WMA 15 values, and doctests. Full run on kimmy: doctests all pass. The pytest failures are the ones that depend on the order the tests run in; the failing files that touch numbers pass on their own.🤖 Generated with Claude Code