Skip to content

Compute sinh, cosh, tanh, expm1 and exp2 in fixed point and round once - #1562

Merged
mborland merged 4 commits into
boostorg:developfrom
ibmibmibm:fixed-point-hyperbolic
Nov 25, 2026
Merged

mborland merged 4 commits into
boostorg:developfrom
ibmibmibm:fixed-point-hyperbolic

Conversation

@ibmibmibm

@ibmibmibm ibmibmibm commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor
  • sinh and cosh compute exp(|x|) in the fixed point of exp, and
    exp(-|x|) from the reciprocal of its mantissa. The two parts are
    scaled to the same decimal exponent, then added or subtracted. For
    |x| < 1/16, they use the Taylor series in x^2. The result rounds
    once in the current mode.
  • tanh computes (1 - t) / (1 + t) with t = exp(-2|x|). Where t is
    below the last bit, the result is a value just below 1. For
    |x| < 1/16, it divides the two Taylor series. The result rounds
    once in the current mode.
  • expm1 computes exp(x) - 1 for x >= 1/16, with the 1 scaled to the
    decimal exponent of exp(x), and 1 - exp(x) for x <= -1/16.
    Where exp(x) is below the last bit, the result is a value just
    above -1. For |x| < 1/16, it uses the Taylor series of
    sinh(x) + cosh(x) - 1. The result rounds once in the current mode.
  • exp2 uses the fixed point of exp with log10(2) in place of
    log10(e). 2^n for an integer n is exact. A result in the
    subnormal range is no longer 0, for example exp2(-1300_DD).
  • sinh and cosh do not overflow where exp(|x|) overflows but its half
    does not, for example sinh(887_DD).
  • A subnormal x now gives the correctly rounded result, not 0. A
    signaling NaN gives the quiet NaN with the sign and payload of x.
  • Where x^2 is below the last digit of the result, sinh, cosh and
    tanh need no series: the result rounds as a value next to |x|, or
    next to 1 for cosh. expm1 does the same for |x| < 10^-p, with p
    the precision of the type.
  • exp and the five functions first compute in fewer words: one word for
    decimal64_t and two words for decimal128_t. If the result cannot
    be next to a rounding boundary, it rounds once. If not, the full
    computation runs. For decimal128_t, tanh and expm1 have this first
    try for |x| < 1/16 and for |x| >= 1/2.
  • For decimal32_t and decimal64_t, the first try of exp, exp2,
    sinh, cosh and tanh takes the argument reduction from one product of
    the significand with log10(e) * 10^-k or log10(2) * 10^-k from a
    table. The product gives the decimal exponent and 128 bits of the
    fraction, with no division. sinh (for |x| >= 1) and cosh (for
    |x| >= 1/16) get exp(-|x|) from the same tables. tanh (for
    |x| >= 0.11) gets t = exp(-2|x|) this way and computes
    1 - 2t / (1 + t) with one division. The error bound of the 19
    digits is 2 units for exp and exp2, and 3 units for sinh, cosh and
    tanh, so few results need the full computation. This also makes exp
    faster for these two types.
  • For decimal32_t, this first try keeps 64 bits of the fraction and
    stops the series at z^3. The error bound is then 2^25 units of the
    19 digits, of which decimal32_t drops 12. sinh takes this try from
    |x| >= 0.1.
  • Where the first try does not round, decimal64_t exp and exp2 take
    the two-word series with more terms. Next to a tie,
    1 + x + x^2 / 2 for a small x needs 32 digits, and the shorter
    series gave 30: exp(-5.000000000000001E-17_DD) gave 1, and now
    gives 0.9999999999999999. develop has the same defect.
    test_cmath_bound_cases.cpp gets these inputs.
  • Next to a rounding boundary, decimal128_t computes the terms after
    the exact first term (x, or 1 for cosh, exp and exp2) in their own
    precision. This also corrects exp for decimal128_t next to a
    rounding tie, for example exp(-1e-17_DL) in round to nearest.
    github_issue_1557.cpp gets this input.
  • fx_recip and fx_div divide in fixed point. fx_shr, fx_horner,
    fx_int, fx_mul_shr and fx_extend are new helpers. For a
    significand of two words, fx_round divides one time and gets the
    rounding digit from the rest. test_fixed_point.cpp compares
    fx_recip with schoolbook arithmetic for one to five words.
  • The reciprocal in impl/tan_impl.hpp gets the name
    recip_below_one, so that its name is different from fx_recip.
  • The series in impl/sinh_impl.hpp, impl/cosh_impl.hpp,
    impl/tanh_impl.hpp and impl/expm1_impl.hpp have no more users and
    are removed.
  • The new tests github_issue_1561_sinh.cpp, github_issue_1561_tanh.cpp,
    github_issue_1561_expm1.cpp and github_issue_1561_exp2.cpp check hard cases
    in all five modes for each type.
  • test_cmath_bound_cases.cpp holds inputs that give a wrong result if
    an error bound of a first try gets smaller, in all rounding modes. A
    program checked the expected values against MPFR. The test needs no
    MPFR.
  • test_first_try_bounds.cpp compares the first tries of exp, exp2,
    sinh, cosh and tanh with three words, against the error bounds in
    the library.

Fixes #1561

Accuracy

The test takes 20000 random x per range, with a full significand and a random sign, for each function, each type and each of the five rounding modes. The ranges of |x| are those of the speed tables, and the subnormal range. MPFR gives the exact value. develop gives up to 100% of incorrect results in a range. The branch gives 0 incorrect results in all ranges, types and modes.

Speed

ns per call, best of 3 runs, GCC 16.2.1 -O2, AMD Ryzen 9 3900X. The inputs have a full significand and a random sign.

sinh:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 73 10 322 26 2013 96
[1e-3, 1) 106 24 388 44 1809 126
[1, 10) 96 26 154 39 314 142
[10, 100) 86 22 144 34 306 127
[100, overflow) 84 22 136 34 283 116

cosh:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 75 7 317 22 1954 91
[1e-3, 1) 106 22 373 33 1755 123
[1, 10) 103 26 161 38 318 147
[10, 100) 91 22 153 34 308 128
[100, overflow) 88 22 145 34 287 118

tanh:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 106 10 408 31 2632 137
[1e-3, 1) 123 32 385 54 1789 167
[1, 10) 110 30 169 48 356 137
[10, 100) 89 15 153 26 334 79
[100, overflow) 85 9 141 21 288 29

expm1:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 107 11 460 47 4057 125
[1e-3, 1) 152 29 443 48 2908 153
[1, 10) 67 34 115 48 232 129
[10, 100) 59 24 109 44 238 126
[100, overflow) 56 22 96 44 195 86

exp2:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 381 13 766 40 2759 109
[1e-3, 1) 396 27 775 45 2761 113
[1, 10) 411 26 797 43 2817 121
[10, 100) 410 26 801 43 2847 121
[100, overflow) 412 28 802 43 2842 122

exp:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 15 10 61 35 126 99
[1e-3, 1) 36 22 70 40 128 100
[1, 10) 37 22 72 39 138 113
[10, 100) 37 22 73 40 139 114
[100, overflow) 40 25 74 40 139 115

@ibmibmibm
ibmibmibm force-pushed the fixed-point-hyperbolic branch from 6410b07 to 5d15603 Compare November 16, 2026 01:30
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.16464% with 41 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.4%. Comparing base (f6ff635) to head (e7a1a79).

Files with missing lines Patch % Lines
test/test_cmath_bound_cases.cpp 41.4% 34 Missing ⚠️
test/test_first_try_bounds.cpp 97.0% 3 Missing ⚠️
...clude/boost/decimal/detail/cmath/impl/exp_impl.hpp 98.8% 2 Missing ⚠️
include/boost/decimal/detail/cmath/sinh.hpp 99.3% 1 Missing ⚠️
include/boost/decimal/detail/cmath/tanh.hpp 99.2% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           develop   #1562     +/-   ##
=========================================
- Coverage     98.6%   98.4%   -0.1%     
=========================================
  Files          344     346      +2     
  Lines        29218   30181    +963     
  Branches      2405    2495     +90     
=========================================
+ Hits         28782   29697    +915     
- Misses         436     484     +48     
Files with missing lines Coverage Δ
include/boost/decimal/detail/cmath/cosh.hpp 100.0% <100.0%> (ø)
include/boost/decimal/detail/cmath/exp.hpp 100.0% <ø> (ø)
include/boost/decimal/detail/cmath/exp2.hpp 100.0% <100.0%> (ø)
include/boost/decimal/detail/cmath/expm1.hpp 100.0% <100.0%> (ø)
...de/boost/decimal/detail/cmath/impl/fixed_point.hpp 99.7% <100.0%> (+2.0%) ⬆️
...clude/boost/decimal/detail/cmath/impl/tan_impl.hpp 100.0% <100.0%> (ø)
test/github_issue_1557.cpp 100.0% <100.0%> (ø)
test/github_issue_1561_exp2.cpp 100.0% <100.0%> (ø)
test/github_issue_1561_expm1.cpp 100.0% <100.0%> (ø)
test/github_issue_1561_sinh.cpp 100.0% <100.0%> (ø)
... and 7 more

... and 3 files 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 f6ff635...e7a1a79. Read the comment docs.

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

@ibmibmibm
ibmibmibm force-pushed the fixed-point-hyperbolic branch from 5d15603 to 24e14c1 Compare November 16, 2026 08:45
@ibmibmibm
ibmibmibm marked this pull request as ready for review November 16, 2026 09:55
@ibmibmibm
ibmibmibm force-pushed the fixed-point-hyperbolic branch 8 times, most recently from 03c29dc to 225c71b Compare November 20, 2026 08:35
sinh and cosh rounded at each step in the decimal type, so the last
digit was often wrong. sinh(887) overflowed for decimal64_t, and a
subnormal x gave 0.

- Use the fixed point of exp: the Taylor series for |x| < 1/16, else
  exp(|x|) and its reciprocal.
- Round the result once in the current mode.
- For decimal128_t, try two words first, and use three words only
  next to a rounding boundary.
- Next to a boundary, compute the terms after the exact lead term in
  their own precision.
- Give the quiet NaN of a signaling NaN x.
- Add fx_recip and the helpers that the hyperbolic functions share.
- Remove impl/sinh_impl.hpp and impl/cosh_impl.hpp.
- Add test_cmath_bound_cases with inputs next to the error bounds.

Fixes boostorg#1561
tanh rounded at each step in the decimal type, so the last digit was
often wrong. A subnormal x gave 0.

- Use the fixed point of exp: the series of sinh and cosh for
  |x| < 1/16, else (1 - t) / (1 + t) with t = exp(-2|x|).
- Round the result once in the current mode.
- For decimal128_t, try two words first, and use three words only
  next to a rounding boundary.
- Next to a boundary, compute the terms after the exact lead term in
  their own precision. For a large |x|, do the same for the small
  term next to 1.
- Give the quiet NaN of a signaling NaN x.
- Add fx_div. Remove impl/tanh_impl.hpp.

Fixes boostorg#1561
expm1 rounded at each step in the decimal type, so the last digit was
often wrong. A subnormal x gave 0.

- Use the fixed point of exp: the series of sinh and cosh for
  |x| < 1/16, else exp(x) - 1 with the 1 at the scale of exp(x).
- Round the result once in the current mode.
- For decimal128_t and |x| < 1/16, try two words first, and use three
  words only next to a rounding boundary.
- Next to a boundary, compute the terms after the exact lead term in
  their own precision. For a large negative x, do the same for exp(x)
  next to -1.
- Give the quiet NaN of a signaling NaN x.
- Remove impl/expm1_impl.hpp.

Fixes boostorg#1561
exp2(x) was pow(2, x) in the decimal type, so the last digits were
wrong for a large |x|.

- Use the fixed point of exp with log10(2), and round once in the
  current mode. 2^n for an integer n is exact.
- For decimal32_t and decimal64_t, give exp, exp2, sinh, cosh, tanh
  and expm1 a first try in one word with an error bound. More words
  run only next to a rounding boundary.
- For decimal128_t, give the same functions a first try in two words.
- Next to a boundary for a small x, compute the terms after the exact
  1 in their own precision, also for decimal64_t.
- For decimal64_t, use a longer series of exp(-z) in two words, as
  exp(-5.000000000000001E-17) gave 1.
- Give the quiet NaN of a signaling NaN x.
- Add test_first_try_bounds, which compares the first tries with
  three words.

Fixes boostorg#1561
@ibmibmibm
ibmibmibm force-pushed the fixed-point-hyperbolic branch from 225c71b to e7a1a79 Compare November 24, 2026 08:30

@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 good. Thanks as always!

@mborland
mborland merged commit 43f6b8a into boostorg:develop Nov 25, 2026
83 checks passed
@ibmibmibm
ibmibmibm deleted the fixed-point-hyperbolic branch November 25, 2026 12:46
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.

sinh, cosh, tanh, expm1 and exp2 give incorrect results, and 0 or infinity for some finite results

2 participants