Skip to content

Log: precision from the condition number, as in WMA - #2053

Merged
tombouctou merged 2 commits into
masterfrom
log-real-valued-precision
Oct 19, 2026
Merged

tombouctou merged 2 commits into
masterfrom
log-real-valued-precision

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

Part of step 6 of the outline in #2047. On top of #2052 (and #2051): this diff is against #2052's branch.

Log gets its precision from the condition number, as WMA does. Log[b, x] is Log[x] / Log[b], so the relative errors of b and x are multiplied by 1/Abs[Log[b]] and 1/Abs[Log[x]]. Log[x] goes through Log[E, x], and Log2, Log10 through Log[b, x]. The change itself is the condition_numbers of Log, as #2052 sets out.

Input before this PR WMA 15
Precision[Log[1.5`20]] 20. 19.608 19.608
Precision[Log[1.0000001`20]] 20. 13. 13.
Precision[Log[100.`20]] 20. 20.6632 20.6632
Precision[Log[2.`20, 1.5`30]] 20. 19.8408 19.8408
Precision[Log10[1.5`20]] 20. 19.608 19.608

Not changed: a Log that comes out as zero, like Log[1.`20], keeps the path it took before (0.`15.*^-15; WMA gives 020.``). The rule here needs a nonzero value.

Tests

test_log_real_valued_precision in test/builtin/atomic/test_numbers.py, with WMA 15 values. Full run on kimmy: doctests all pass. The pytest failures are the order-dependent ones; the failing file that touches numbers (test_diffeqns.py) passes on its own.

🤖 Generated with Claude Code

Comment thread mathics/builtin/numbers/exp.py Outdated
return lambda base, x: mpmath.log(x, base)

@staticmethod
def condition_numbers(base, x, value):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the return type annotation be tuple[float, float]? Can we say anything about the types of base, x, or value?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f4b3c83. The arguments and the value are mpmath.mpf: the helper converts the numbers at the working precision before it calls this. The result is tuple[mpmath.mpf, mpmath.mpf], not floats. The factors are only used through Log10, but they can be far below the float range: for Tanh[10.`20^6] the factor is about E^-2000000. As a float that would be 0., which reads as "this argument adds no error", so they stay mpmath numbers.

I typed the hook the same way in #2052 (Callable[..., tuple[mpmath.mpf, ...]], with that reason in its comment), and added the annotations to the formulas in #2054–#2056 too. mypy is clean on these files.

@tombouctou
tombouctou force-pushed the log-real-valued-precision branch from d6da960 to f7d8f32 Compare October 19, 2026 21:17
tombouctou and others added 2 commits October 20, 2026 01:18
Part of step 6 of #2047 (MATH-227), on top of the Power PR.

Log[b, x] is Log[x] / Log[b], so the relative errors of b and x are
multiplied by 1/Abs[Log[b]] and 1/Abs[Log[x]]. Log[x], Log2 and Log10
go through Log[b, x]; Exp[x] is E^x and is already covered by Power.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As asked in review: mpmath numbers in and out, as for Power.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rocky

rocky commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

LGTM. Feel free to merge when CI finishes.

Base automatically changed from power-real-valued-precision to master October 19, 2026 22:18
@tombouctou
tombouctou deleted the branch power-real-valued-precision October 19, 2026 22:18
@tombouctou tombouctou closed this Oct 19, 2026
@tombouctou tombouctou reopened this Oct 19, 2026
@tombouctou
tombouctou merged commit 3b04144 into master Oct 19, 2026
24 checks passed
@tombouctou
tombouctou deleted the log-real-valued-precision branch October 19, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants