23

[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 these are the principal methods used to check if a floating point value can be converted to an integral type T.

  1. if (f >= std::numeric_limits<T>::min() && f <= std::numeric_limits<T>::max() && f == (T)f))

  2. using std::fmod to extract the remainder and test equality to 0.

  3. using std::remainder and test equality to 0.

The first test assumes that a cast from f to a T instance is defined. Not true for std::int64_t to float, for example.

With C++11, which one is best? Is there a better way?

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • 2
    Compile time or run-time ? – P0W Oct 13 '14 at 13:46
  • It will be run-time. – P45 Imminent Oct 13 '14 at 13:47
  • 2
    I'd like to see/know/read why/how [testing if given number is integer](http://stackoverflow.com/q/7646512/1870232) is _ out of date with the current C++ standard_ ? – P0W Oct 13 '14 at 13:55
  • 2
    Numeric, of course. Scientifically, floats should be treated as finite approximations, and the range they're coming from contains an infinitely small fraction of integers – MSalters Oct 13 '14 at 14:08
  • @P0W I see no harm in re-asking this. The link you cite does not have a definitive answer. The accepted answer is flawed insofar you need an `abs` to make it work in all cases and does not use or mention a method only available in C++11. – P45 Imminent Oct 13 '14 at 14:20
  • wouldnt you just check the exponent, if less than some number then it fits in an int, otherwise it doesnt. Even better if it shifts greater than the size of the mantissa then you lose precision, the mantissa will fit (in a similar sized int) by definition, so that is also an easy test. – old_timer Oct 13 '14 at 15:12
  • @Slodge Monster Might want to comment on the importance and/or desired result of NaN and Inf. Suggest leaving those open to implementer's choice. – chux - Reinstate Monica Oct 17 '14 at 14:17
  • NaN should always evaluate to not an integer since a comparison to an integer would be false. (NaN compares false against everything). But, yes, let's leave it to the implementer. – P45 Imminent Oct 17 '14 at 15:05
  • 1
    Check this out http://stackoverflow.com/questions/12442685/can-a-ieee-754-real-number-cover-all-integers-within-its-range – Fabio A. Correa Oct 17 '14 at 15:47
  • @SlodgeMonster: Why are you asking for a "method available only in C+=11"? They didn't randomly add new functions when the old functions were already sufficient just to break backwards portability for the sake of it ! – MSalters Oct 20 '14 at 11:03
  • @MSalters See for example my answer: std::trunc (introduced in C++11) let's you formulate the test conveniently and safely for all floating-point types in just 3 lines of code. – user1225999 Oct 21 '14 at 07:49
  • @dwelch: mantissa 1.5 exponent 0 -> value is 1.5, exponent is a value that fits an an int. Checking only the exponent is not enough. – Mooing Duck Nov 11 '14 at 00:48

12 Answers12

19

Conclusion:

The answer is use std::trunc(f) == f the time difference is insignificant when comparing all these methods. Even if the specific IEEE unwinding code we write in the example below is technically twice is fast we are only talking about 1 nano second faster.

The maintenance costs in the long run though would be significantly higher. So use a solution that is easier to read and understand by the maintainer is better.

Time in microseconds to complete 12,000,000 operations on a random set of numbers:

  • IEEE breakdown:                                               18
  • std::trunc(f) == f                                  32
  • std::floor(val) - val == 0                35
  • ((uint64_t)f) - f) == 0.0                  38
  • std::fmod(val, 1.0) == 0                     87

The Working out of the conclusion.

A floating point number is two parts:

mantissa:      The data part of the value.
exponent:      a power to multiply it by.

such that:

   value =  mantissa * (2^exponent)

So the exponent is basically how many binary digits we are going to shift the "binary point" down the mantissa. A positive value shifts it right a negative value shifts it left. If all the digits to the right of the binary point are zero then we have an integer.

If we assume IEEE 754

We should note that this representation the value is normalized so that the most significant bit in the mantissa is shifted to be 1. Since this bit is always set it is not actually stored (the processor knows its there and compensates accordingly).

So:

If the exponent < 0 then you definitely do not have an integer as it can only be representing a fractional value. If the exponent >= <Number of bits In Mantissa> then there is definately no fractual part and it is an integer (though you may not be able to hold it in an int).

Otherwise we have to do some work. if the exponent >= 0 && exponent < <Number of bits In Mantissa> then you may be representing an integer if the mantissa is all zero in the bottom half (defined below).

Additional as part of the normalization 127 is added to the exponent (so that there are no negative values stored in the 8 bit exponent field).

#include <limits>
#include <iostream>
#include <cmath>

/*
 *  Bit  31      Sign
 *  Bits 30-23   Exponent
 *  Bits 22-00   Mantissa
 */
bool is_IEEE754_32BitFloat_AnInt(float val)
{
    // Put the value in an int so we can do bitwise operations.
    int  valAsInt = *reinterpret_cast<int*>(&val);

    // Remember to subtract 127 from the exponent (to get real value)
    int  exponent = ((valAsInt >> 23) & 0xFF) - 127;

    int bitsInFraction = 23 - exponent;
    int mask = exponent < 0
                    ? 0x7FFFFFFF
                    : exponent > 23
                         ? 0x00
                         : (1 << bitsInFraction) - 1;

    return !(valAsInt & mask);
}
/*
 *  Bit  63      Sign
 *  Bits 62-52   Exponent
 *  Bits 51-00   Mantissa
 */
bool is_IEEE754_64BitFloat_AnInt(double val)
{
    // Put the value in an long long so we can do bitwise operations.
    uint64_t  valAsInt = *reinterpret_cast<uint64_t*>(&val);

    // Remember to subtract 1023 from the exponent (to get real value)
    int  exponent = ((valAsInt >> 52) & 0x7FF) - 1023;

    int bitsInFraction = 52 - exponent;
    uint64_t mask = exponent < 0
                    ? 0x7FFFFFFFFFFFFFFFLL
                    : exponent > 52
                        ? 0x00
                        : (1LL << bitsInFraction) - 1;

    return !(valAsInt & mask);
}

bool is_Trunc_32BitFloat_AnInt(float val)
{
    return (std::trunc(val) - val == 0.0F);
}

bool is_Trunc_64BitFloat_AnInt(double val)
{
    return (std::trunc(val) - val == 0.0);
}

bool is_IntCast_64BitFloat_AnInt(double val)
{
    return (uint64_t(val) - val == 0.0);
}

template<typename T, bool isIEEE = std::numeric_limits<T>::is_iec559>
bool isInt(T f);

template<>
bool isInt<float, true>(float f) {return is_IEEE754_32BitFloat_AnInt(f);}

template<>
bool isInt<double, true>(double f) {return is_IEEE754_64BitFloat_AnInt(f);}

template<>
bool isInt<float, false>(float f) {return is_Trunc_64BitFloat_AnInt(f);}

template<>
bool isInt<double, false>(double f) {return is_Trunc_64BitFloat_AnInt(f);}

int main()
{
    double  x = 16;
    std::cout << x << "=> " << isInt(x) << "\n";

    x = 16.4;
    std::cout << x << "=> " << isInt(x) << "\n";

    x = 123.0;
    std::cout << x << "=> " << isInt(x) << "\n";

    x = 0.0;
    std::cout << x << "=> " << isInt(x) << "\n";

    x = 2.0;
    std::cout << x << "=> " << isInt(x) << "\n";

    x = 4.0;
    std::cout << x << "=> " << isInt(x) << "\n";

    x = 5.0;
    std::cout << x << "=> " << isInt(x) << "\n";

    x = 1.0;
    std::cout << x << "=> " << isInt(x) << "\n";
}

Results:

> ./a.out
16=> 1
16.4=> 0
123=> 1
0=> 1
2=> 1
4=> 1
5=> 1
1=> 1

Running Some Timing tests.

Test data was generated like this:

(for a in {1..3000000};do echo $RANDOM.$RANDOM;done ) > test.data
(for a in {1..3000000};do echo $RANDOM;done ) >> test.data
(for a in {1..3000000};do echo $RANDOM$RANDOM0000;done ) >> test.data
(for a in {1..3000000};do echo 0.$RANDOM;done ) >> test.data

Modified main() to run tests:

int main()
{
    // ORIGINAL CODE still here.
    // Added this trivial speed test.

    std::ifstream   testData("test.data");  // Generated a million random numbers
    std::vector<double>  test{std::istream_iterator<double>(testData), std::istream_iterator<double>()};
    std::cout << "Data Size: " << test.size() << "\n";
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;

    auto start = std::chrono::system_clock::now();
    for(auto const& v: test)
    {   count1 += is_IEEE754_64BitFloat_AnInt(v);
    }
    auto p1 = std::chrono::system_clock::now();
    for(auto const& v: test)
    {   count2 += is_Trunc_64BitFloat_AnInt(v);
    }
    auto p2 = std::chrono::system_clock::now();
    for(auto const& v: test)
    {   count3 += is_IntCast_64BitFloat_AnInt(v);
    }

    auto end = std::chrono::system_clock::now();

    std::cout << "IEEE  " << count1 << " Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(p1 - start).count() << "\n";
    std::cout << "Trunc " << count2 << " Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(p2 - p1).count()    << "\n";
    std::cout << "Int Cast " << count3 << " Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - p2).count()   << "\n";    }

The tests show:

> ./a.out
16=> 1
16.4=> 0
123=> 1
0=> 1
2=> 1
4=> 1
5=> 1
1=> 1
Data Size: 12000000
IEEE  6000199 Time: 18
Trunc 6000199 Time: 32
Int Cast 6000199 Time: 38

The IEEE code (in this simple test) seem to beat the truncate method and generate the same result. BUT the amount of time is insignificant. Over 12 million calls we saw a difference in 14 milliseconds.

Florian
  • 1,036
  • 10
  • 15
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 4
    More or less a more difficult way of writing `std::trunc(f)==f`. Also this posting does not actually answer the question since it does not evaluate the proposed options. – user1225999 Oct 17 '14 at 14:11
  • Various problems: Code returns 1 on pow(2,64+16). Suspect Undefined behavior with1LL << bitsInFraction as shifts greater than the bit width are undefined. Code returns 0 on 123.0. Suspect code should be int bitsInFraction = 52 – exponent;. Code returns 0 on 0.0. Code needs handling for 0.0 case. Code may fail when sizeof(long long) mis-matches sizeof (double). Recommend uint64_t instead. Code depends on integer and FP endian to match each other, which is common, but not required. – chux - Reinstate Monica Oct 17 '14 at 14:40
  • @chux: Yep I was thinking about that as I went to bed last night. I need to add a couple of consistency checks when values get large (small values have already been compensated for). I'll do that some-point today. – Martin York Oct 17 '14 at 15:08
  • @chux: Note: `sizeof(long long) * CHAR_BITS >= 64` – Martin York Oct 17 '14 at 15:38
  • "though you may not be able to hold it in an ant" nice typo – Drax Oct 17 '14 at 16:04
  • @Loki Astari A problem should `sizeof(long long) * CHAR_BITS > 64` is that the MSbytes may be in the lowest address (big endian) for both the integer and the `double`. Then the lower 64-bits used in `valAsInt` do not correspond to the data in the `double`. Note: "subtract 1023" comment does not match 1024. – chux - Reinstate Monica Oct 17 '14 at 16:23
  • @Loki Astari For me `is_IEEE754_64BitFloat_AnInt(double)` fails for 1.0 and 2.0, works for 4.0, fails for 5.0. Suspect `mask/exponent`. `mask = (1LL << (bitsInFraction-2)) - 1;` worked for me. – chux - Reinstate Monica Oct 17 '14 at 16:49
  • @user1225999: Added unit tests to compare the two methods. – Martin York Oct 17 '14 at 21:33
  • Also tested: `std::fmod(val, 1.0) == 0` Time: 87 – Martin York Oct 17 '14 at 23:02
  • Also tested: `std::floor(val) - val == 0` Time: 35 – Martin York Oct 17 '14 at 23:15
  • 1
    The question is: if your life depended on this functionality working correctly, would you use `std::trunc` or this piece of code? – Maxim Egorushkin Oct 20 '14 at 15:32
  • In reality, I'll use @Bathsheba's solution, but I'm awarding the bounty to this one as it's very insightful. – P45 Imminent Oct 21 '14 at 08:01
  • @MaximYegorushkin: See first paragraph now. – Martin York Oct 21 '14 at 15:28
  • @SlodgeMonster: That is the slowest of the 5 techniques I compare. I would use `std::trunc(f) == f` – Martin York Oct 21 '14 at 17:11
  • Please note that you cannot safely cast to an arbitrary integral type `T` if `is_IEEE754_*BitFloat_AnInt` returns true since the cast runs into undefined behaviour. See my answer below for details. – user1225999 Sep 07 '15 at 16:16
  • if (is_IEEE754_32BitFloat_AnInt(4294967296.0f)) std::cout << static_cast(f) << std::endl; // Output: 0 (on my platform) – user1225999 Sep 07 '15 at 16:16
  • @user1225999: What is the value of f? `static_cast(f)`. So it looks like it is working because it detected there was no data after the decimal point? Anyway the conclusion is use `trunc(f) == f` – Martin York Sep 07 '15 at 17:45
  • Sorry `float f=4294967296.0f``. My point was that no matter what you use to check if a floating-point number is integral one cannot assume that you can safely cast it if the number is out of the range of the target type without explicitly checking that first. – user1225999 Sep 08 '15 at 07:05
11

Use std::fmod(f, 1.0) == 0.0 where f is either a float, double, or long double. If you're worried about spurious effects of unwanted floating point promotions when using floats, then use either 1.0f or the more comprehensive

std::fmod(f, static_cast<decltype(f)>(1.0)) == 0.0

which will force, obviously at compile time, the correct overload to be called. The return value of std::fmod(f, ...) will be in the range [0, 1) and it's perfectly safe to compare to 0.0 to complete your integer check.

If it turns out that f is an integer, then make sure it's within the permitted range of your chosen type before attempting a cast: else you risk invoking undefined behaviour. I see that you're already familiar with std::numeric_limits which can help you here.

My reservations against using std::remainder are possibly (i) my being a Luddite and (ii) it not being available in some compilers partially implementing the C++11 standard, such as MSVC12. I don't like solutions involving casts since the notation hides that reasonably expensive operation and you need to check in advance for safety. If you must adopt your first choice, at least replace the C-style cast with static_cast<T>(f);

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • `fmod` and `remainder` are both a little more expensive than a conversion to integer and back on some implementations. If `f` is a `float`, I believe it will be converted to a `double` and fed to the `double` overload of `std::fmod`, causing a further performance hit. My `gcc` also uses the `double` overload for `std::fmod(mylongdouble, 1.0)`; I suspect that means premature rounding. – tmyklebu Oct 13 '14 at 15:26
  • Strictly I think you should use `1.0f` as the parameter if `f` is a `float`. Annoyingly, OP doesn't specify which floating point type their concerned with. Perhaps all 3? I've added a `decltype` to make sure. – Bathsheba Oct 13 '14 at 15:27
  • Hmm. `double` `fmod` on recent `gcc` and recent `glibc` on x86_64 Linux seems to use the FPU's `fprem`. Then it looks in the FPU status word. – tmyklebu Oct 13 '14 at 15:36
  • Boss, I'm not satisfied with this answer. Might wait for an expert before accepting. – P45 Imminent Oct 13 '14 at 15:38
  • 4
    @SlodgeMonster: Er, how are you going to tell who's an expert and who isn't? You have two correct answers so far. – tmyklebu Oct 13 '14 at 15:49
1

This test is good:

if (   f >= std::numeric_limits<T>::min()
    && f <= std::numeric_limits<T>::max()
    && f == (T)f))

These tests are incomplete:

using std::fmod to extract the remainder and test equality to 0.

using std::remainder and test equality to 0.

They both fail to check that the conversion to T is defined. Float-to-integral conversions that overflow the integral type result in undefined behaviour, which is even worse than roundoff.

I would recommend avoiding std::fmod for another reason. This code:

int isinteger(double d) {
  return std::numeric_limits<int>::min() <= d
      && d <= std::numeric_limits<int>::max()
      && std::fmod(d, 1.0) == 0;
}

compiles (gcc version 4.9.1 20140903 (prerelease) (GCC) on x86_64 Arch Linux using -g -O3 -std=gnu++0x) to this:

0000000000400800 <_Z9isintegerd>:
  400800:       66 0f 2e 05 10 01 00    ucomisd 0x110(%rip),%xmm0        # 400918 <_IO_stdin_used+0x18>
  400807:       00
  400808:       72 56                   jb     400860 <_Z9isintegerd+0x60>
  40080a:       f2 0f 10 0d 0e 01 00    movsd  0x10e(%rip),%xmm1        # 400920 <_IO_stdin_used+0x20>
  400811:       00
  400812:       66 0f 2e c8             ucomisd %xmm0,%xmm1
  400816:       72 48                   jb     400860 <_Z9isintegerd+0x60>
  400818:       48 83 ec 18             sub    $0x18,%rsp
  40081c:       d9 e8                   fld1
  40081e:       f2 0f 11 04 24          movsd  %xmm0,(%rsp)
  400823:       dd 04 24                fldl   (%rsp)
  400826:       d9 f8                   fprem
  400828:       df e0                   fnstsw %ax
  40082a:       f6 c4 04                test   $0x4,%ah
  40082d:       75 f7                   jne    400826 <_Z9isintegerd+0x26>
  40082f:       dd d9                   fstp   %st(1)
  400831:       dd 5c 24 08             fstpl  0x8(%rsp)
  400835:       f2 0f 10 4c 24 08       movsd  0x8(%rsp),%xmm1
  40083b:       66 0f 2e c9             ucomisd %xmm1,%xmm1
  40083f:       7a 22                   jp     400863 <_Z9isintegerd+0x63>
  400841:       66 0f ef c0             pxor   %xmm0,%xmm0
  400845:       31 c0                   xor    %eax,%eax
  400847:       ba 00 00 00 00          mov    $0x0,%edx
  40084c:       66 0f 2e c8             ucomisd %xmm0,%xmm1
  400850:       0f 9b c0                setnp  %al
  400853:       0f 45 c2                cmovne %edx,%eax
  400856:       48 83 c4 18             add    $0x18,%rsp
  40085a:       c3                      retq
  40085b:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
  400860:       31 c0                   xor    %eax,%eax
  400862:       c3                      retq
  400863:       f2 0f 10 0d bd 00 00    movsd  0xbd(%rip),%xmm1        # 400928 <_IO_stdin_used+0x28>
  40086a:       00
  40086b:       e8 20 fd ff ff          callq  400590 <fmod@plt>
  400870:       66 0f 28 c8             movapd %xmm0,%xmm1
  400874:       eb cb                   jmp    400841 <_Z9isintegerd+0x41>
  400876:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  40087d:       00 00 00

The first five instructions implement the range check against std::numeric_limits<int>::min() and std::numeric_limits<int>::max(). The rest is the fmod test, accounting for all the misbehaviour of a single invocation of the fprem instruction (400828..40082d) and some case where a NaN somehow arose.

You get similar code by using remainder.

tmyklebu
  • 13,915
  • 3
  • 28
  • 57
1

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;
}
Stefan Atev
  • 485
  • 3
  • 11
  • Good thought. I bet you can make the add-and-subtract trickery work by picking your constant a little more carefully. You'll also need to add an explicit test for infinity. And a range test to make sure it fits in whatever a `T` is. – tmyklebu Oct 13 '14 at 21:45
  • Well, the constant is magic for a reason - it forces nf to have an exponent large enough that float can only represent integers anyway; this is a common normalization trick; picking other constants is unlikely to work well. This code was tested with floats in the range 0 to 2^23 and works – Stefan Atev Oct 14 '14 at 02:00
  • 2
    Yes, I know how the trick works. But you can use `copysign(0x1.0p23, f)` instead to handle negative numbers as well. (There's still unfortunately an ugly range of numbers between 2^23 and 2^24 where all the odd ones will fail.) – tmyklebu Oct 14 '14 at 02:50
  • I see what you meant now; I have nothing but bad experience with the performance of copysign unfortunately, and at that point you're no longer using a constant known at compile time; but thanks for pointing out how to make this work for negatives... – Stefan Atev Oct 14 '14 at 11:28
1

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
user1225999
  • 912
  • 8
  • 14
  • 1
    Very helpful, thanks. Too bad there are so many other answer to weed through to get here. However, I avoid Boost at all costs, so the `trunc` solution is nicest for me. – chappjc Aug 10 '15 at 20:29
1

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;
}
V-X
  • 2,979
  • 18
  • 28
0

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
}
jwfearn
  • 28,781
  • 28
  • 95
  • 122
Ali Zand
  • 41
  • 2
0

The problem with:

  if (   f >= std::numeric_limits<T>::min()
      && f <= std::numeric_limits<T>::max()
      && f == (T)f))

is that if T is (for example) 64 bits, then the max will be rounded when converting to your usual 64 bit double :-( Assuming 2's complement, the same is not true of the min, of course.

So, depending on the number of bits in the mantisaa, and the number of bits in T, you need to mask off the LS bits of std::numeric_limits::max()... I'm sorry, I don't do C++, so how best to do that I leave to others. [In C it would be something along the lines of LLONG_MAX ^ (LLONG_MAX >> DBL_MANT_DIG) -- assuming T is long long int and f is double and that these are both the usual 64 bit values.]

If the T is constant, then the construction of the two floating point values for min and max will (I assume) be done at compile time, so the two comparisons are pretty straightforward. You don't really need to be able to float T... but you do need to know that its min and max will fit in an ordinary integer (long long int, say).

The remaining work is converting the float to integer, and then floating that back up again for the final comparison. So, assuming f is in range (which guarantees (T)f does not overflow):

  i  = (T)f ;         // or i  = (long long int)f ;
  ok = (i == f) ;

The alternative seems to be:

  i  = (T)f ;         // or i  = (long long int)f ;
  ok = (floor(f) == f) ;

as noted elsewhere. Which replaces the floating of i by floor(f)... which I'm not convinced is an improvement.

If f is NaN things may go wrong, so you might want to test for that too.

You could try unpacking f with frexp() and extract the mantissa as (say) a long long int (with ldexp() and a cast), but when I started to sketch that out it looked ugly :-(


Having slept on it, a simpler way of dealing with the max issue is to do: min <= f < ((unsigned)max+1) -- or min <= f < (unsigned)min -- or (double)min <= f < -(double)min -- or any other method of constructing -2^(n-1) and +2^(n-1) as floating point values, where n is the number of bits in T.

(Serves me right for getting interested in a problem at 1:00am !)

0

what about converting types like this?

bool can_convert(float a, int i)
{
  int b = a;
  float c = i;
  return a == c;
}
dau_sama
  • 4,247
  • 2
  • 23
  • 30
0

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.

Nard
  • 1,006
  • 7
  • 8
0

Use modf() which breaks the value into integral and fractional parts. From this direct test, it is known if the double is a whole number or not. After this, limit tests against the min/max of the target integer type can be done.

#include <cmath>

bool IsInteger(double x) {
  double ipart;
  return std::modf(x, &ipart) == 0.0;  // Test if fraction is 0.0.
}

Note modf() differs from the similar named fmod().

Of the 3 methods OP posted, the cast to/from an integer may perform a fair amount of work doing the casts and compare. The other 2 are marginally the same. They work, assuming no unexpected rounding mode effects from dividing by 1.0. But do an unnecessary divide.

As to which is fastest likely depends on the mix of doubles used.

OP's first method has a singular advantage: Since the goal is to test if a FP may convert exactly to a some integer, and likely then if the result is true, the conversion needs to then occur, OP's first method has already done the conversion.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

If your question is "Can I convert this double to int without loss of information?" then I would do something simple like :

template <typename T, typename U>
bool CanConvert(U u)
{
  return U(T(u)) == u;
}

CanConvert<int>(1.0) -- true
CanConvert<int>(1.5) -- false
CanConvert<int>(1e9) -- true
CanConvert<int>(1e10)-- false
Andy Jewell
  • 413
  • 3
  • 12
  • You get UB by converting `1e10` to a 32-bit `int`. – tmyklebu Oct 13 '14 at 14:50
  • @tmyklebu UB, yes, T(u) could be converted to any possible value of T and we don't know which one. But don't we know that, whatever this value, T(u) will be between min and max std::numeric_limits? If yes, whatever the undefined behavior, I fail to see how U(T(u)) would be equal to u... IOW, I think that CanConvert does its job. – aka.nice Oct 14 '14 at 14:21
  • 1
    @aka.nice: That's not what UB is. UB means nasal demons. UB means it eats your cat. UB is not the same thing as an unspecified or implementation-defined value. UB means, in particular, that **the compiler can assume that the branch of your code that results in a call of `CanConvert` on a `U` that's outside of the range of `T` is dead**. – tmyklebu Oct 14 '14 at 14:31
  • @tmyklebu yes, you're right, the compiler has a license to presume we never invoke UB, and thus do some optimizations ignoring UB. I think such optimization is not programmed in our compilers because it just won't pay, but I might be proven wrong anytime soon :) – aka.nice Oct 15 '14 at 11:13
  • @aka.nice: They've already started doing that, actually. Use-before-checking-null bugs now cause the null check to disappear. – tmyklebu Oct 15 '14 at 13:39
  • @tmyklebu you mean like this http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html ? I was more thinking of specific optimization related to floating point conversion... – aka.nice Oct 15 '14 at 17:10