-7

can you tell me why this boolean check is wrong? I want to check if its integer or not I don't want a double or float its just an integer in c++ I tried this code but it`s always give me integer

  bool isInt(int a) {
    if (sizeof(a)==4) {
        cout << "integer";
    }
    else
        cout << "not integer";
    return 0;
}

void main() {
    int a=15.5;
    
    isInt(a);
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 11
    It is always an integer, since you are using the type `int`. `int a=15.5;` stores `15` in `a`, not `15.5`. `sizeof` has nothing to do with the value contained in the variable. – user17732522 Mar 09 '22 at 20:11
  • when i changed the boolean type to double it`s give me not integer for integer and double – Black White Mar 09 '22 at 20:13
  • Maybe you could give some more context on why you need this check. – user17732522 Mar 09 '22 at 20:13
  • So is your question really how to test whether a `double` variable holds an integer value? – user17732522 Mar 09 '22 at 20:14
  • @BlackWhite since this is the 3rd question I see going wrong in this misconception, you also probably want to check [this](https://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it). Your approach can never work. Seemingly popular homeworks today, for whatever reason. Just hesitant to close as dupe, others should feel free, if they believe it's appropriate to solve the OPs problems. – πάντα ῥεῖ Mar 09 '22 at 20:16
  • @πάνταῥεῖ As far as I can see from the question this should be "Needs details or clarity" since I still can't tell without the context you seem to have what the intent of OP is. – user17732522 Mar 09 '22 at 20:20
  • 1
    This question may need some clarity or explanation on the intent of this code. It is not clear how checking if an `int` is `4` bytes would reveal that it's an integer. And an `int` is always an integer. – Drew Dormann Mar 09 '22 at 20:21
  • `template bool isInt(T a) { if (typeof(a) == typeof(int)) { cout << "integer"; return true; } cout << "not integer"; return false; }` ... but why would you need this? – Eljay Mar 09 '22 at 20:30
  • Also note that [floating point math is "broken"](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). You can wind up with integers that floating point simply cannot represent due to limitations in the encoding. – user4581301 Mar 09 '22 at 20:32

1 Answers1

1

remember that you are passing a double/float.

#include <iostream>
#include <cstdlib>
#include <cstdbool> // bool
#include <cmath>    // std::round

using std::cout;
using std::endl;

bool isInt(double);

int
main()
{
    double a = 15.5;

    if (isInt(a))
        cout << "Integer" << endl;
    else
        cout << "Not integer" << endl;

    return EXIT_SUCCESS;
}


bool
isInt(double a)
{
    return std::round(a) == a;
}
jr.çhåvez__07
  • 170
  • 2
  • 9
  • With this implementation, `isInt(-3.14)` will return `true`, which I don't think is your intention. – Drew Dormann Mar 09 '22 at 20:41
  • @DrewDormann Ahh, yeah. sorry. I have modified it, now it will probably work. – jr.çhåvez__07 Mar 09 '22 at 21:03
  • 1
    There are certainly edge-cases to this "is this double an integer" problem. With your revision, `isInt(12345678901);` is [very likely](https://godbolt.org/z/f6MsabsG1) to return `false`. – Drew Dormann Mar 09 '22 at 21:10
  • @DrewDormann - I see, I'm sorry it wasn't helpful. – jr.çhåvez__07 Mar 09 '22 at 21:17
  • 1
    `return std::round(a) == a;` would likely achieve the logic you were attempting. – Drew Dormann Mar 09 '22 at 21:25
  • @DrewDormann - Yes, that seems like a better solution. I'm sorry, I'm not very familiar with some things yet, and I'm not very experienced with this either. It seems that I still have a long way to go, I have to keep things in mind so that it doesn't happen again. Thank you, sir. – jr.çhåvez__07 Mar 09 '22 at 21:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242788/discussion-between-jr-chavez-07-and-drew-dormann). – jr.çhåvez__07 Mar 09 '22 at 21:34