Best way of checking if a floating point is an integer

后端 未结 12 1842
陌清茗
陌清茗 2020-12-25 13:07

[There are a few questions on this but none of the answers are particularly definitive and several are out of date with the current C++ standard].

My research shows

相关标签:
12条回答
  • 2020-12-25 13:40

    Personally I would recommend using the trunc function introduced in C++11 to check if f is integral:

    #include <cmath>
    #include <type_traits>
    
    template<typename F>
    bool isIntegral(F f) {
        static_assert(std::is_floating_point<F>::value, "The function isIntegral is only defined for floating-point types.");
        return std::trunc(f) == f;
    }
    

    It involves no casting and no floating point arithmetics both of which can be a source of error. The truncation of the decimal places can surely be done without introducing a numerical error by setting the corresponding bits of the mantissa to zero at least if the floating point values are represented according to the IEEE 754 standard.

    Personally I would hesitate to use fmod or remainder for checking whether f is integral because I am not sure whether the result can underflow to zero and thus fake an integral value. In any case it is easier to show that trunc works without numerical error.

    None of the three above methods actually checks whether the floating point number f can be represented as a value of type T. An extra check is necessary.

    The first option actually does exactly that: It checks whether f is integral and can be represented as a value of type T. It does so by evaluating f == (T)f. This check involves a cast. Such a cast is undefined according to §1 in section 4.9 of the C++11 standard "if the truncated value cannot be represented in the destination type". Thus if f is e.g. larger or equal to std::numeric_limits<T>::max()+1 the truncated value will certainly have an undefined behavior as a consequence.

    That is probably why the first option has an additional range check (f >= std::numeric_limits<T>::min() && f <= std::numeric_limits<T>::max()) before performing the cast. This range check could also be used for the other methods (trunc, fmod, remainder) in order to determine whether f can be represented as a value of type T. However, the check is flawed since it can run into undefined behavior: In this check the limits std::numeric_limits<T>::min/max() get converted to the floating point type for applying the equality operator. For example if T=uint32_t and f being a float, std::numeric_limits<T>::max() is not representable as a floating point number. The C++11 standard then states in section 4.9 §2 that the implementation is free to choose the next lower or higher representable value. If it chooses the higher representable value and f happens to be equal to the higher representable value the subsequent cast is undefined according to §1 in section 4.9 since the (truncated) value cannot be represented in the destination type (uint32_t).

    std::cout << std::numeric_limits<uint32_t>::max() << std::endl;  // 4294967295
    std::cout << std::setprecision(20) << static_cast<float>(std::numeric_limits<uint32_t>::max()) << std::endl;  // 4294967296 (float is a single precision IEEE 754 floating point number here)
    std::cout << static_cast<uint32_t>(static_cast<float>(std::numeric_limits<uint32_t>::max())) << std::endl;  // Could be for example 4294967295 due to undefined behavior according to the standard in the cast to the uint32_t.
    

    Consequently, the first option would establish that f is integral and representable as uint32_t even though it is not.

    Fixing the range check in general is not easy. The fact that signed integers and floating point numbers do not have a fixed representation (such as two's complement or IEEE 754) according to the standard do not make things easier. One possibility is to write non-portable code for the specific compiler, architecture and types you use. A more portable solution is to use Boost's NumericConversion library:

    #include <boost/numeric/conversion/cast.hpp>
    
    template<typename T, typename F>
    bool isRepresentableAs(F f) {
        static_assert(std::is_floating_point<F>::value && std::is_integral<T>::value, "The function isRepresentableAs is only defined for floating-point as integral types.");
        return boost::numeric::converter<T, F>::out_of_range(f) == boost::numeric::cInRange && isIntegral(f);
    }
    

    Then you can finally perform the cast safely:

    double f = 333.0;
    if (isRepresentableAs<uint32_t>(f))
        std::cout << static_cast<uint32_t>(f) << std::endl;
    else
        std::cout << f << " is not representable as uint32_t." << std::endl;
    // Output: 333
    
    0 讨论(0)
  • 2020-12-25 13:42

    what about converting types like this?

    bool can_convert(float a, int i)
    {
      int b = a;
      float c = i;
      return a == c;
    }
    
    0 讨论(0)
  • 2020-12-25 13:42

    First of all, I want to see if I got your question right. From what I've read, it seems that you want to determine if a floating-point is actually simply a representation of an integral type in floating-point.

    As far as I know, performing == on a floating-point is not safe due to floating-point inaccuracies. Therefore I am proposing the following solution,

    template<typename F, typename I = size_t>
    bool is_integral(F f)
    {
      return fabs(f - static_cast<I>(f)) <= std::numeric_limits<F>::epsilon;
    }
    

    The idea is to simply find the absolute difference between the original floating-point and the floating-point casted to the integral type, and then determine if it is smaller than the epsilon of the floating-point type. I'm assuming here that if it is smaller than epsilon, the difference is of no importance to us.

    Thank you for reading.

    0 讨论(0)
  • 2020-12-25 13:42

    Here is what I would try:

    float originalNumber;
    cin >> originalNumber;
    int temp = (int) originalNumber;
    if (originalNumber-temp > 0)
    {
        // It is not an integer
    }
    else
    {
        // It is an integer
    }
    
    0 讨论(0)
  • 2020-12-25 13:43

    Some other options to consider (different compilers / libraries may produce different intrinsic sequences for these tests and be faster/slower):

    bool is_int(float f) { return floor(f) == f; }
    

    This is in addition to the tests for overflow you have...

    If you are looking to really optimize, you could try the following (works for positive floats, not thoroughly tested): This assumes IEEE 32-bit floats, which are not mandated by the C++ standard AFAIK.

    bool is_int(float f)
    {
        const float nf = f + float(1 << 23);
        const float bf = nf - float(1 << 23);
        return f == bf;
    }
    
    0 讨论(0)
  • 2020-12-25 13:45

    I'd go deep into the IEE 754 standard and keep thinking only in terms of this type and I'll be assuming 64 bit integers and doubles.

    The number is a whole number iff:

    1. the number is zero (regardless on the sign).
    2. the number has mantisa not going to binary fractions (regardless on the sing), while not having any undefined digits for least significant bits.

    I made following function:

    #include <stdio.h>
    
    int IsThisDoubleAnInt(double number)
    {
        long long ieee754 = *(long long *)&number;
        long long sign = ieee754 >> 63;
        long long exp = ((ieee754 >> 52) & 0x7FFLL);
        long long mantissa = ieee754 & 0xFFFFFFFFFFFFFLL;
        long long e = exp - 1023;
        long long decimalmask = (1LL << (e + 52));
        if (decimalmask) decimalmask -= 1;
        if (((exp == 0) && (mantissa != 0)) || (e > 52) || (e < 0) || ((mantissa & decimalmask) != 0))
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
    

    As a test of this function:

    int main()
    {
        double x = 1;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 1.5;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 2;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 2.000000001;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 1e60;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 1e-60;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 1.0/0.0;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = x/x;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 0.99;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = 1LL << 52;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
        x = (1LL << 52) + 1;
        printf("x = %e is%s int.\n", x, IsThisDoubleAnInt(x)?"":" not");
    }
    

    The result is following:

    x = 1.000000e+00 is int.
    x = 1.500000e+00 is not int.
    x = 2.000000e+00 is int.
    x = 2.000000e+00 is not int.
    x = 1.000000e+60 is not int.
    x = 1.000000e-60 is not int.
    x = inf is not int.
    x = nan is not int.
    x = 9.900000e-01 is not int.
    x = 4.503600e+15 is int.
    x = 4.503600e+15 is not int.
    

    The condition in the method is not very clear, thus I'm posting the less obfuscated version with commented if/else structure.

    int IsThisDoubleAnIntWithExplanation(double number)
    {
        long long ieee754 = *(long long *)&number;
        long long sign = ieee754 >> 63;
        long long exp = ((ieee754 >> 52) & 0x7FFLL);
        long long mantissa = ieee754 & 0xFFFFFFFFFFFFFLL;
        if (exp == 0)
        {
            if (mantissa == 0)
            {
                // This is signed zero.
                return 1;
            }
            else
            {
                // this is a subnormal number
                return 0;
            }
        }
        else if (exp == 0x7FFL)
        {
            // it is infinity or nan.
            return 0;
        }
        else
        {
            long long e = exp - 1023;
            long long decimalmask = (1LL << (e + 52));
            if (decimalmask) decimalmask -= 1;
            printf("%f: %llx (%lld %lld %llx) %llx\n", number, ieee754, sign, e, mantissa, decimalmask);
            // number is something in form (-1)^sign x 2^exp-1023 x 1.mantissa
            if (e > 63)
            {
                // number too large to fit into integer
                return 0;
            }
            else if (e > 52)
            {
                // number too large to have all digits...
                return 0;
            }
            else if (e < 0)
            {
                // number too large to have all digits...
                return 0;
            }
            else if ((mantissa & decimalmask) != 0)
            {
                // number has nonzero fraction part.
                return 0;
            }
        }
        return 1;
    }
    
    0 讨论(0)
提交回复
热议问题