[rational] Inadvertent construction from double
Hi,
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
2014-07-25 13:38 GMT+02:00 Andrzej Krzemienski
Hi, 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
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.
I forgot to mention why this is working. A double is implicitly converted to int and then the implicit constructor from int is used.
2014-07-25 14:13 GMT+02:00 Andrzej Krzemienski
2014-07-25 13:38 GMT+02:00 Andrzej Krzemienski
: Hi, 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
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.
+1 Regards, Kris
I forgot to mention why this is working. A double is implicitly converted to int and then the implicit constructor from int is used.
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
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? John.
2014-07-25 17:13 GMT+02:00 John Maddock
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
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 ( http://www.boost.org/doc/libs/develop/libs/rational/rational.html#Conversion...) explicitly says that "The library does not offer a conversion function from floating point to rational."
participants (3)
-
Andrzej Krzemienski
-
John Maddock
-
Krzysztof Czainski