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

Open
#1,561 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@ibmibmibm is already working on this.

Since Nov 15, 2026.

  • #1562 by @ibmibmibm — open

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
10/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
tooling

Research direction

Start from the existing fixed-point exp implementation that sinh, cosh, tanh and expm1 build on, and read the reproducer in the issue to see each failing case. The referenced branch fixed-point-hyperbolic shows the intended approach (reciprocal for exp(-|x|), (1 - t)/(1 + t) for tanh, fixed-point series below 1/16), but it is a large numerical rewrite, not a single edit. Done means the listed table rows return the correct values in all rounding modes.

Written by the indexing model from the issue text.

Description

sinh, cosh, tanh, expm1 and exp2 on develop (215bbf025) have these defects, for all three types:

  1. Many results are not correctly rounded. In round to nearest, up to 74% of the sinh results, 42% of the cosh results, 80% of the tanh results, 45% of the expm1 results and 100% of the exp2 results in a range are not correct. The maximum error of exp2 is 519 units in the last place (ulp) for decimal32_t, 1562 ulp for decimal64_t and 66090 ulp for decimal128_t.
  2. A subnormal x gives 0 for sinh, cosh, tanh and expm1. The correct results are x, 1, x and x.
  3. sinh and cosh give infinity where exp(|x|) overflows but the result does not, for example sinh(887_DD).
  4. exp2 gives 0 for some results in the subnormal range, for example exp2(-1300_DD).
  5. The directed rounding modes give incorrect values, for example tanh(40_DD) in the downward mode.
  6. exp for decimal128_t gives the adjacent value for some x where the result is next to a rounding boundary, for example exp(-1e-17_DL).

IEEE 754-2019 clause 9.2 recommends correctly rounded results for these functions.

Cause

sinh, cosh, tanh and expm1 compute exp(x) in the decimal type, and then add, subtract and divide in the decimal type. For a small |x|, they use a series in the decimal type. Each step rounds. sinh and cosh divide by two after exp(|x|), thus they overflow too early. The test for special values lets through only a normal x, thus a subnormal x goes to the path for 0. exp2(x) is pow(2, x), and its error increases with |x|. For exp and these x, the fixed-point result of three words does not have sufficient digits to find the side of the rounding boundary.

Reproducer

The program uses only Boost.Decimal. The comments give the correct values.

#include <boost/decimal.hpp>
#include <iostream>

using namespace boost::decimal;
using namespace boost::decimal::literals;

int main()
{
    std::cout << std::setprecision(34);
    std::cout << sinh(887_DD) << '\n';                 // 8.282767152491226e+384
    std::cout << sinh(30_DF) << '\n';                  // 5.343237e+12
    std::cout << cosh(2.5_DD) << '\n';                 // 6.132289479663686
    std::cout << cosh(3e-390_DD) << '\n';              // 1
    std::cout << tanh(1_DD) << '\n';                   // 0.7615941559557649
    std::cout << tanh(3e-390_DD) << '\n';              // 3e-390
    std::cout << expm1(0.6_DD) << '\n';                // 0.822118800390509
    std::cout << exp2(300.7_DF) << '\n';               // 3.309175e+90
    std::cout << exp2(-1300_DD) << '\n';               // 4.581478e-392
    std::cout << exp2("20000.5"_DL) << '\n';           // 5.628961489605482600400511804238708e+6020
    std::cout << exp("-1e-17"_DL) << '\n';             // 0.99999999999999999

    fesetround(rounding_mode::fe_dec_downward);
    std::cout << tanh(40_DD) << '\n';                  // 0.9999999999999999
    fesetround(rounding_mode::fe_dec_upward);
    std::cout << expm1(0.5_DF) << '\n';                // 0.6487213
}
Correct values

The error is the distance from the exact value, in ulp of the correct result. MPFR at 80000 bits gives the exact value. Boost.Multiprecision cpp_bin_float<1400> gives the same values to a relative difference of 1e-1000. Python decimal rounds the MPFR value once, with the precision, exponent range and rounding mode of each type. No reference value in the table is near a rounding boundary, but for the exp rows. There, the exact value of exp(-1e-17) is 0.4999999999999999983 ulp above 0.9999999999999999900000000000000000, and the exact value of exp(-2e-17) is 0.9999999999999999867 ulp above 0.9999999999999999800000000000000001. MPFR at 2000 bits and Python decimal at 120 digits give these values.

Function Type Mode x develop Correct Error
sinh decimal64_t nearest 887 inf 8.282767152491226e+384 infinite
cosh decimal64_t nearest -887 inf 8.282767152491226e+384 infinite
sinh decimal32_t nearest 221.5 7.855935e+95 7.855933e+95 2.45 ulp
sinh decimal64_t nearest 3e-390 0 3.00000000e-390 3e8 ulp
cosh decimal64_t nearest 3e-390 0 1.000000000000000 1e15 ulp
sinh decimal64_t nearest 0.05 0.050020835937655 0.05002083593765502 1.53 ulp
sinh decimal64_t nearest 5.83 170.1778704973842 170.1778704973843 0.502 ulp
cosh decimal64_t nearest 2.5 6.132289479663685 6.132289479663686 1.12 ulp
sinh decimal32_t nearest 30 5.343235e+12 5.343237e+12 2.29 ulp
sinh decimal128_t nearest 50 2592352764293536232043.726661466742 2592352764293536232043.726661466743 0.692 ulp
sinh decimal64_t downward -1e-30 -1e-30 -1.000000000000001e-30 above the exact value
tanh decimal64_t nearest 1 0.7615941559557651 0.7615941559557649 2.12 ulp
tanh decimal32_t nearest 0.5 0.4621169 0.4621172 2.57 ulp
tanh decimal64_t nearest 3e-390 0 3.00000000e-390 3e8 ulp
tanh decimal64_t nearest 12.5 0.9999999999722238 0.9999999999722241 3.12 ulp
tanh decimal64_t nearest 19 1 0.9999999999999999 0.628 ulp
tanh decimal128_t nearest 30 0.9999999999999999999999999824869784 0.9999999999999999999999999824869785 0.746 ulp
tanh decimal64_t downward 40 0.9999999999999995 0.9999999999999999 5 ulp
tanh decimal64_t upward -40 -0.9999999999999996 -0.9999999999999999 4 ulp
expm1 decimal64_t nearest 0.6 0.8221188003905088 0.8221188003905090 1.75 ulp
expm1 decimal64_t nearest 5e-390 0 5.00000000e-390 5e8 ulp
expm1 decimal64_t downward -1e-30 -9.999999999999999e-31 -1.000000000000000e-30 above the exact value
expm1 decimal32_t upward 0.5 0.6487215 0.6487213 2.29 ulp
exp2 decimal64_t nearest -1300 0 4.581478e-392 4.6e6 ulp
exp2 decimal64_t nearest -1270.5 3.478487995464459e-383 3.478487995464753e-383 294 ulp
exp2 decimal32_t nearest 300.7 3.309316e+90 3.309175e+90 141 ulp
exp2 decimal128_t nearest 100.25 1507499113128880389969770996485.796 1507499113128880389969770996485.783 13.3 ulp
exp2 decimal128_t nearest 20000.5 5.628961489605482600400511804236176e+6020 5.628961489605482600400511804238708e+6020 2530 ulp
exp2 decimal64_t upward -2000 0 1e-398 below the exact value
exp decimal128_t nearest -1e-17 0.9999999999999999900000000000000001 0.9999999999999999900000000000000000 more than 0.5 ulp
exp decimal128_t upward -2e-17 0.9999999999999999800000000000000003 0.9999999999999999800000000000000002 1 ulp
exp decimal128_t downward -2e-17 0.9999999999999999800000000000000002 0.9999999999999999800000000000000001 above the exact value
Frequency

The test takes 20000 random x per range, with a full significand and a random sign. The table shows the part of the results that are not correctly rounded, and the maximum error in ulp. The error comes from the exact relative error against MPFR, not from a comparison of digit strings.

sinh, round to nearest:

Range of |x| decimal32_t decimal64_t decimal128_t
[1e-20,1e-3) 2.0%, 1.67 ulp 21.7%, 5.22 ulp 61.5%, 5.32 ulp
[1e-3,1) 71.7%, 5.38 ulp 72.2%, 5.2 ulp 73.6%, 8.91 ulp
[1,10) 41.1%, 4.99 ulp 42.1%, 5 ulp 42.0%, 4.92 ulp
[10,100) 41.6%, 2.5 ulp 41.9%, 4.71 ulp 42.0%, 4.9 ulp
[100,overflow) 42.6%, 2.49 ulp 40.9%, 2.5 ulp 41.7%, 2.5 ulp
subnormal 100% 100% 100%

cosh, round to nearest:

Range of |x| decimal32_t decimal64_t decimal128_t
[1e-20,1e-3) 0.0%, 0.499 ulp 0.0%, 0.5 ulp 0.0%, 0.5 ulp
[1e-3,1) 0.5%, 0.554 ulp 0.5%, 0.541 ulp 0.7%, 0.618 ulp
[1,10) 42.0%, 4.96 ulp 42.0%, 4.94 ulp 41.9%, 4.93 ulp
[10,100) 41.3%, 2.5 ulp 41.0%, 4.8 ulp 41.5%, 4.94 ulp
[100,overflow) 40.8%, 2.5 ulp 41.9%, 2.5 ulp 42.2%, 2.5 ulp
subnormal 100% 100% 100%

tanh, round to nearest:

Range of |x| decimal32_t decimal64_t decimal128_t
[1e-20,1e-3) 0.5%, 0.821 ulp 3.9%, 0.939 ulp 11.2%, 0.932 ulp
[1e-3,1) 32.9%, 5.84 ulp 33.4%, 5.76 ulp 32.6%, 5.66 ulp
[1,10) 71.7%, 9.81 ulp 80.0%, 10.3 ulp 80.4%, 10.1 ulp
[10,100) 0.0%, 0.0409 ulp 8.2%, 9.64 ulp 26.4%, 10 ulp
subnormal 100% 100% 100%

expm1, round to nearest:

Range of |x| decimal32_t decimal64_t decimal128_t
[1e-20,1e-3) 10.6%, 5.27 ulp 34.0%, 5.19 ulp 44.3%, 5.41 ulp
[1e-3,1) 40.4%, 5.04 ulp 40.8%, 4.94 ulp 44.7%, 5.98 ulp
[1,10) 2.5%, 4.97 ulp 2.4%, 4.97 ulp 2.6%, 5 ulp
[10,100) 0.2%, 0.596 ulp 0.2%, 0.6 ulp 0.2%, 0.6 ulp
[100,overflow) 0.0%, 0.498 ulp 0.0%, 0.5 ulp 0.0%, 0.5 ulp
subnormal 100% 100% 100%

exp2, round to nearest:

Range of |x| decimal32_t decimal64_t decimal128_t
[1e-20,1e-3) 0.0%, 0.5 ulp 0.0%, 0.5 ulp 0.0%, 0.5 ulp
[1e-3,1) 4.1%, 0.921 ulp 7.9%, 1.16 ulp 10.9%, 1.38 ulp
[1,10) 61.8%, 7.06 ulp 79.2%, 13.8 ulp 87.1%, 17.9 ulp
[10,100) 94.2%, 62.9 ulp 98.2%, 134 ulp 99.0%, 175 ulp
[100,overflow) 98.4%, 519 ulp 99.9%, 1.56e+03 ulp 100.0%, 6.61e+04 ulp
subnormal 0.0%, 6.93e-89 ulp 0.0%, 0 ulp 0.0%, 0 ulp

The directed modes also give incorrect results for all five functions.

Fix

The branch fixed-point-hyperbolic computes the five functions in the integer fixed point of exp and rounds one time:

  1. sinh and cosh compute exp(|x|) and get exp(-|x|) from the reciprocal of its mantissa. The two parts have the same decimal exponent before the addition or subtraction.
  2. tanh computes (1 - t) / (1 + t) with t = exp(-2|x|).
  3. expm1 computes exp(x) - 1 with the 1 at the decimal exponent of exp(x), and 1 - exp(x) for a negative x.
  4. exp2 uses log10(2) in place of log10(e).
  5. For |x| < 1/16, sinh, cosh, tanh and expm1 use Taylor series in fixed point.
  6. Where x^2 is below the last digit of the result, sinh, cosh and tanh round a value next to |x|, or next to 1 for cosh, with no series. expm1 does the same for |x| below 10 to the power of minus the precision.
  7. Next to a rounding boundary, decimal128_t computes the terms after the exact first term in their own precision. This corrects exp too.

The same test as above with the branch gives 0 incorrect results in all ranges, types and modes. All rows in the table of correct values give the correct values.

Speed, ns per call, best of 3 runs, GCC 16.2.1 -O2, AMD Ryzen 9 3900X:

sinh:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 73 10 322 27 2009 96
[1e-3, 1) 105 26 387 46 1805 125
[1, 10) 96 34 153 52 313 138
[10, 100) 85 30 143 44 306 127
[100, overflow) 83 31 136 45 282 119

cosh:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 74 7 316 22 1945 91
[1e-3, 1) 105 26 372 40 1757 121
[1, 10) 102 34 160 51 318 142
[10, 100) 91 30 152 44 309 126
[100, overflow) 87 31 145 45 288 119

tanh:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 105 11 404 30 2613 138
[1e-3, 1) 122 33 382 55 1777 172
[1, 10) 109 35 168 56 354 139
[10, 100) 88 12 153 26 331 81
[100, overflow) 40 8 69 19 216 28

expm1:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 106 11 458 47 4043 124
[1e-3, 1) 151 29 440 48 2904 151
[1, 10) 66 34 114 48 232 129
[10, 100) 59 24 109 44 237 125
[100, overflow) 56 22 96 43 194 85

exp2:

Range of |x| decimal32_t develop branch decimal64_t develop branch decimal128_t develop branch
[1e-20, 1e-3) 380 16 762 46 2783 98
[1e-3, 1) 395 31 770 45 2794 101
[1, 10) 409 30 796 42 2838 114
[10, 100) 409 30 796 41 2841 113
[100, overflow) 410 32 796 42 2842 114
Dominant language
C++
Stars
67
Forks
16
Avg merge
1d 15h
Merged PRs (30d)
12

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/decimal

All issues in boostorg/decimal

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.