Boost logo

Boost :

Subject: Re: [boost] [rational] Inadvertent construction from double
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2014-07-25 11:26:47

2014-07-25 17:13 GMT+02:00 John Maddock <boost.regex_at_[hidden]>:

> I have encountered something I consider a bug in boost::rational's
>> interface. Its documentation discusses in detail why conversion from
>> double
>> is not supported, but the following code works fine:
>> #include <iostream>
>> #include <boost/rational.hpp>
>> int main()
>> {
>> double d = 31.82;
>> boost::rational<int> r = d;
>> std::cout << r << std::endl;
>> }
>> With the following result:
>> 31/1
>> It just discards the fractional part. Such conversion is really confusing
>> an I claim should be explicitly deleted.
> It should indeed be prohibited and not proceed via an implicit conversion.
> It's probably an easy fix via enable/disable_if as well.
> Note that the Boost.Multiprecision rational classes *do* allow implicit
> construction from floating point values since these are actually exact
> conversions when correctly written (this is a new change in response to a
> bug report - will be in the next release). So there's a discrepancy there.
> However, rational<int> doesn't have enough bits to do such a conversion
> correctly, so I guess it's maybe safer to forbid it in that case?

The documentation (
explicitly says that "The library does not offer a conversion function from
floating point to rational."

Boost list run by bdawes at, gregod at, cpdaniel at, john at