Tell me how to correctly put the type check: If the number res is integer

if (...) { ...; } 

Can eat standard?

  • and how the number res you announced? - KoVadim
  • After changing the wording of the question from "How to determine whether an integer is" (this is me by memory) to "Check the number on its type" the answers look very strange . - Dear "editors", in such cases, take the trouble to provide a reference to the version to be fixed. - avp
  • 2
    Dear author, what do you mean: 1) what value (integer or decimal fractional part) in a double or float variable? 2) in a character string numbers and you want to know whether a whole number? 3) "how to correctly put type checking" is a variable of what type is placed when compiled to a given address? 4) something else ??? - avp

4 answers 4

I haven't used C ++ for a long time, but as far as I remember, such things are done through the ghost operator:

 dynamic_cast 

All other ghosts are something inherited from С In С++ there is an operator for each case.

For checking built-in types, this approach will not work. Different compilers have macros, for example, __typeof__ . They are specific to a particular compiler.

C++ Supports trimmed RTTI. It is possible to use only the language features of C++ , look at this code:

 #include <stdio.h> #include <typeinfo.h> using namespace std; int main(){ int a = 10; printf("Type is %s", typeid(a).name()); }; 
  • I am looking at the description of dynamic_cast - but I don’t see conversions of simple types there to the stop .... - timka_s

Something like this:

 float a; if ( (float)( (int) a ) === a ) //Целое 

    It is better to always use double (float only for storage in large arrays).

     double r; if (r == (long long)r) // Целое 
    • one
      it does not affect the essence of the approach ... Copy & Paste - there is no gud - timka_s
    • Better for what? They provide greater accuracy, but at the same time they occupy more memory and in some cases (for example, when calculating on GPU) the speed of double operations is much lower than with float. - skegg
    • To @timka_s, @mikillskegg "this does not affect the essence of the approach ..." if it is about float and double, then in C it influences very much, since when passed to a function (by default), the float is converted to double, and newcomers sometimes have "unexplainable" errors. It is better to get used immediately to use double (except in special cases (like a GPU) and only consciously use float). - avp

    Or so:

     if(floor(value) == value) ... 

    For this you need to connect math.h.

    Some may argue that comparing floating-point numbers for equality is wrong. But if it were always wrong, the compiler would not allow this in principle. In this case, integers can be represented exactly in double. The operations of addition, subtraction and multiplication for integers will also give integers, since they do not introduce rounding errors. For the numbers specified manually or obtained in this way, the verification above will work correctly. If you still need to check that the number is an integer within a certain error eps (for example, 0.000001), you can use the following test:

     if(abs(floor(value + 0.5) - value) < eps) ... 

    Or if the round function is available in the used version of the standard library:

     if(abs(round(value) - value) < eps) ... 

    These two options use rounding to the nearest integer so that the difference between this and the original number is in the interval (-0.5; 0.5), and not [0; 1), as in the case of rounding to a smaller one.

    • Instead of floor, you can use ceil or trunc. Well, we must remember that, depending on the type of parameter / result, the names of these functions have different suffixes. - alexlz