lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] boost::numeric_cast anomalies


From: Greg Chicares
Subject: [lmi] boost::numeric_cast anomalies
Date: Tue, 28 Mar 2017 12:21:22 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0

Vadim--I think you usually have the latest version of boost installed,
so could I ask you to build this program with it and post the output?
All these tests fail with boost-1.33.1; I think they may have made all
infinities interconvertible, but AFAICT they don't know about the rest.

---------8<--------8<--------8<--------8<--------8<--------8<--------8<-------
#include <boost/cast.hpp>
#include <climits>
#include <iostream>
#include <limits>
#include <ostream>

int main()
{
    using double_limits = std::numeric_limits<double>;

    // IEEE 754-2008 [5.8, conversion to integer]: "When a NaN or infinite
    // operand cannot be represented in the destination format and this
    // cannot otherwise be indicated, the invalid operation exception shall
    // be signaled."
    int nan = boost::numeric_cast<int>(double_limits::quiet_NaN());
    std::cout << "Integer converted from NaN = " << nan << std::endl;

    // IEEE 754-2008 [6.1]: "Operations on infinite operands are usually
    // exact and therefore signal no exceptions, including ... conversion of
    // an infinity into the same infinity in another format."
    try
        {
        boost::numeric_cast<long double>(double_limits::infinity());
        std::cout << "That worked, so this should too..." << std::endl;
        boost::numeric_cast<float>(double_limits::infinity());
        std::cout << "...because all infinities are convertible." << std::endl;
        }
    catch(...){std::cout << "Line " << __LINE__ << ": bad throw." << std::endl;}

    try
        {
        boost::numeric_cast<int>(INT_MIN);
        boost::numeric_cast<int>((double)INT_MIN);
        std::cout << "That worked, so this should too..." << std::endl;
        boost::numeric_cast<int>((float)INT_MIN);
        std::cout << "...because INT_MIN = an exact power of 2." << std::endl;
        }
    catch(...){std::cout << "Line " << __LINE__ << ": bad throw." << std::endl;}
        
    try
        {
        boost::numeric_cast<long long int>((long double)LLONG_MIN);
        std::cout << "That worked, so this should too..." << std::endl;
        boost::numeric_cast<long long int>((float)LLONG_MIN);
        std::cout << "...because LLONG_MIN = an exact power of 2." << std::endl;
        }
    catch(...){std::cout << "Line " << __LINE__ << ": bad throw." << std::endl;}
        
    try
        {
        boost::numeric_cast<long long int>((long double)LLONG_MIN);
        std::cout << "That worked, so this should too..." << std::endl;
        boost::numeric_cast<long long int>((double)LLONG_MIN);
        std::cout << "...because LLONG_MIN = an exact power of 2." << std::endl;
        }
    catch(...){std::cout << "Line " << __LINE__ << ": bad throw." << std::endl;}

    return 0;
}
--------->8-------->8-------->8-------->8-------->8-------->8-------->8-------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]