sinh, cosh, tanh, expm1 and exp2 give incorrect results, and 0 or infinity for some finite results
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
sinh, cosh, tanh, expm1 and exp2 on develop (215bbf025) have these defects, for all three types:
- Many results are not correctly rounded. In round to nearest, up to 74% of the
sinhresults, 42% of thecoshresults, 80% of thetanhresults, 45% of theexpm1results and 100% of theexp2results in a range are not correct. The maximum error ofexp2is 519 units in the last place (ulp) fordecimal32_t, 1562 ulp fordecimal64_tand 66090 ulp fordecimal128_t. - A subnormal
xgives 0 forsinh,cosh,tanhandexpm1. The correct results arex, 1,xandx. sinhandcoshgive infinity whereexp(|x|)overflows but the result does not, for examplesinh(887_DD).exp2gives 0 for some results in the subnormal range, for exampleexp2(-1300_DD).- The directed rounding modes give incorrect values, for example
tanh(40_DD)in the downward mode. expfordecimal128_tgives the adjacent value for somexwhere the result is next to a rounding boundary, for exampleexp(-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:
sinhandcoshcomputeexp(|x|)and getexp(-|x|)from the reciprocal of its mantissa. The two parts have the same decimal exponent before the addition or subtraction.tanhcomputes(1 - t) / (1 + t)witht = exp(-2|x|).expm1computesexp(x) - 1with the 1 at the decimal exponent ofexp(x), and1 - exp(x)for a negativex.exp2useslog10(2)in place oflog10(e).- For
|x| < 1/16,sinh,cosh,tanhandexpm1use Taylor series in fixed point. - Where
x^2is below the last digit of the result,sinh,coshandtanhround a value next to|x|, or next to 1 forcosh, with no series.expm1does the same for|x|below 10 to the power of minus the precision. - Next to a rounding boundary,
decimal128_tcomputes the terms after the exact first term in their own precision. This correctsexptoo.
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 |
- 主要言語
- C++
- スター
- 67
- フォーク
- 16
- 平均マージ
- 1日 15時間
- マージ済み PR(30日)
- 12
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
boostorg/decimal のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 22/100
メンテナーはふだん 1 日以内に返信
-
The decimal types have no operator<=> with GCC and Clang if no earlier header includes <compare>対応中かも @ibmibmibm が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 25/100
メンテナーはふだん 1 日以内に返信
-
comparetotal gives incorrect results for NaN operands対応中かも @ibmibmibm が 1 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
メンテナーはふだん 1 日以内に返信
-
A comparison of a decimal and an integer gives incorrect results対応中かも @ibmibmibm が 1 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 22/100
メンテナーはふだん 1 日以内に返信
-
A comparison of two decimal types can use the type with fewer digits対応中かも @ibmibmibm が 1 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 20/100
メンテナーはふだん 1 日以内に返信
boostorg/decimal の issue をすべて見る
似ている issue
-
agent:WSL code-review linux pri:medium refactoring
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
kind/enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 82/100
psi29a/d2decomped#691 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
CachyOS/packageinstaller#144 ·
メンテナーはふだん 1 日以内に返信
-
(S1 - Need confirmation)
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
CleverRaven/Cataclysm-DDA#89715 ·
メンテナーはふだん 1 日以内に返信