boostorg / boost

Super-project for modularized Boost
https://github.com/boostorg/wiki/wiki/Getting-Started%3A-Overview
Boost Software License 1.0
7.13k stars 1.75k forks source link

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

Open Mike-debug opened 3 months ago

Mike-debug commented 3 months ago

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.

#include <iostream>
#include <string>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/algorithm/string.hpp>
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;
}