Skip to content

Keep the digits of log, log10 and log2 near 1 - #1468

Merged
mborland merged 2 commits into
boostorg:developfrom
ibmibmibm:worktree-log-near-one
Sep 25, 2026
Merged

mborland merged 2 commits into
boostorg:developfrom
ibmibmibm:worktree-log-near-one

Conversation

@ibmibmibm

@ibmibmibm ibmibmibm commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor
  • Near 1, log, log10 and log2 lost most of their digits. Above 1, log took
    log10(x) * ln10, and the reduction of log10 subtracts terms near 1. Below 1, the
    functions took the value of 1/x, which rounds.
  • For x in [0.5, 1.5], x - 1 is exact. log now takes log1p(x - 1) there.
  • log10 and log2 scale the two parts of the log1p series with 2/ln(10) and
    2/ln(2), each held in two parts. The result rounds only once.
  • The log1p series uses only the terms that can change the result. Each term that it
    drops is less than 0.01 of the last digit.
  • github_issue_1110 expected the old value 4.4e-33. It now expects the correctly
    rounded value.
  • The new test github_issue_1467.cpp compares the three functions with MPFR values for
    the six types.

Near 1 the three functions are inside 0.5 units in the last digit, and they need 16 to
51 percent less time than before. Over a wide range the time changes by less than 5
percent. Issue #1467 gives the values, the MPFR sweep and the full benchmark.

Fixes #1467

- Above 1, log took log10(x) * ln10, and the reduction of log10 subtracts terms near 1.
  Below 1, both took the value of 1/x, which rounds. Near 1 the two lose most digits,
  and log(0.9999999_DF) gives -0.
- log2 calls log, so it lost the same digits.
- For x in [0.5, 1.5], x - 1 is exact, and log1p takes its series branch there, which
  does not call log. log and log10 now take log1p(x - 1) in this range.
- log keeps x equal to 1 out of the new branch, so the downward mode still gives +0.
- github_issue_1110 expected the old log10 value 4.4e-33, and now expects the correctly
  rounded value.
- log10 divided the rounded log1p by ln10, and log2 divided the rounded log by ln2. Near 1
  these two more roundings gave up to 4 units in the last digit.
- log1p_parts gives the two parts of the series of log1p before its last rounding, and
  log1p itself uses it as before.
- log10 and log2 now scale the two parts with 2/ln(10) and 2/ln(2) in two parts each, and
  round only once.
- The series of log1p used all terms of its table for each argument, thus log, log10 and
  log2 near 1 became slower than the old path. log1p_series_sum now uses only the terms
  which can change the result. It gets their number from the exponent and the leading
  digit of w^2, and each term that it drops is less than 0.01 of the last digit.

Fixes boostorg#1467
@ibmibmibm
ibmibmibm force-pushed the worktree-log-near-one branch from 79832a1 to fb2360f Compare September 25, 2026 17:50

@mborland mborland left a comment

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.

Looks reasonable to me. Will merge once green. Thanks

@codecov

codecov Bot commented Sep 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.6%. Comparing base (ea218b6) to head (fb2360f).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           develop   #1468     +/-   ##
=========================================
+ Coverage     98.6%   98.6%   +0.1%     
=========================================
  Files          306     307      +1     
  Lines        25620   25705     +85     
  Branches      2202    2203      +1     
=========================================
+ Hits         25256   25340     +84     
- Misses         364     365      +1     
Files with missing lines Coverage Δ
...ude/boost/decimal/detail/cmath/impl/log1p_impl.hpp 100.0% <100.0%> (ø)
include/boost/decimal/detail/cmath/log.hpp 100.0% <100.0%> (ø)
include/boost/decimal/detail/cmath/log10.hpp 100.0% <100.0%> (ø)
include/boost/decimal/detail/cmath/log1p.hpp 100.0% <100.0%> (ø)
include/boost/decimal/detail/cmath/log2.hpp 100.0% <100.0%> (ø)
test/github_issue_1110.cpp 100.0% <100.0%> (ø)
test/github_issue_1467.cpp 100.0% <100.0%> (ø)

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ea218b6...fb2360f. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mborland
mborland merged commit f694b0e into boostorg:develop Sep 25, 2026
75 checks passed
@ibmibmibm
ibmibmibm deleted the worktree-log-near-one branch September 26, 2026 04:42
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.

log, log10 and log2 lose most of their digits for an argument near 1

2 participants