boostorg / boostorg/boost

boost::multiprecision::cpp_dec_float_50 fail to handle division of two floating point numbers that are divisible

Open
#929 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
8.6k
Forks
1.9k
Avg merge
39m
Merged PRs (30d)
2

Description

As it shows below, dividing two floating point numbers that are divisible by themselves results in a loss of precision.
Is it a bug exists in the **boost of version 1.85.0.**
``` cpp
#include
#include
#include
#include
using namespace std;
namespace bmp = boost::multiprecision;
typedef bmp::cpp_dec_float_50 bfloat;
static const bfloat BFLOAT0{"0.0"};

void print_price(bfloat price);

int main()
{
bfloat amount{"158.94600000"};
bfloat quantity{"0.05900000"};
bfloat avg_price = (quantity == BFLOAT0) ? BFLOAT0 : amount / quantity;
print_price(avg_price);
bfloat correct_result{"2694"};
if (correct_result == avg_price) {
std::cout << "correct ans" << std::endl;
} else {
std::cout << "wrong ans" << std::endl;
}
/*
2694
2693.999999999999999999999999999999999999999999999999999999999999999999999978
wrong ans
*/
return 0;
}

void print_price(bfloat price)
{
std::cout << price << std::endl;
std::cout << price.str() << std::endl;
}

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.