boostorg / boostorg/multiprecision

Error in square root for cpp_dec_float_100

Open
#495 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
265
Forks
128
Avg merge
4h 48m
Merged PRs (30d)
2

Description

I recently updated boost from 1.71.0 to 1.80.0 and I started getting an error. After some investigation the problem arises from an incorrect rounding on the square root of 49 that makes it 7.000...01 instead of just 7. Here's the code that shows the issue

```c++
#include
#include

using boost::multiprecision::cpp_dec_float_100;
using std::string;

int main() {
cpp_dec_float_100 n(49);
n = sqrt(n);
if (n == (uint32_t) n)
std::cout << "This should be logged as sqrt(49) = 7" << std::endl;
else
std::cout << "Error: sqrt(49) != 7" << std::endl;

std::cout << std::endl << n.str() << std::endl;

return 0;
}
```

```
Error: sqrt(49) != 7

7.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
```

I also replicated the issue [here](https://wandbox.org/permlink/LdTnsrcG6DPbavgB) with version 1.79.0. In version 1.71.0 the calculation was correct.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the provided reproduction and the sqrt path exposed by boost/multiprecision/cpp_dec_float.hpp, comparing behavior with Boost 1.71.0 and 1.79.0/1.80.0. Trace why sqrt(49) produces a nonzero final digit; done means the reproduction compares equal to 7 and prints the expected value.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.