Boost logo

Boost Users :

Subject: [Boost-users] [boost][numeric][conversion] boost::numeric_cast from integer to floating point (was Numeric Cast from integer to floating point)
From: dariomt (dariomt_at_[hidden])
Date: 2014-09-10 04:39:49


Changed subject to see if it gets any attention...

 <dariomt <at> gmail.com> writes:
>
> Hi,
> Quoting the docs for boost::numeric_cast: "There are several situations
where conversions are unsafe: [...] Conversions from floating point types to
integral types."
>
> What about conversions from integral types to floating point types? E.g.
from 64bit int to double.
>
> The following example shows what I mean:
>
>
> #include <iostream>
> #include <cmath>
> #include <boost/numeric/conversion/cast.hpp>
>
> int main()
> {
>     const uint64_t i = 123445678911188878;
>     std::cout << "i=" << i << std::endl;
>     
>     const double d = i;
>     std::cout << "d="    << std::fixed << d << std::endl;
>     std::cout << "next=" << std::fixed << std::nextafter(d,
 std::numeric_limits<double>::max() ) << std::endl;
>     std::cout << "prev=" << std::fixed << std::nextafter(d, -
std::numeric_limits<double>::max()) << std::endl;
>     
>     // I'd expect the following cast to fail
>     const double dd = boost::numeric_cast<double>(i);
>     std::cout <<"dd=" << std::fixed << dd << std::endl;
>
>     return 0;
> }
>
>
> prints
>
i=123445678911188878d=123445678911188880.000000next=123445678911188896.00000
0prev=123445678911188864.000000dd=123445678911188880.000000because that
integer cannot be represented in double precisionIs there something in Boost
to help here?Thanks in advance


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net