How to consider a number

How can I consider whether a number is int or double? I used the code but it doesn't output the desired one. Please help me with it. Thanks a lot! :)

SAMPLE INPUT 1:
3
SAMPLE OUTPUT 1:
Integer!

SAMPLE INPUT 2:
3.6
SAMPLE OUTPUT 2:
Double!

1
2
3
4
5
6
7
8
9
10
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
double a;
scanf("%lf",&a);
if(a>0.0) printf("Double!\n");
else printf("Integer!\n");
return 0;
}
Do you consider input of 3.0 as double? then probably you should read the input as string and search for a '.' Otherwise,
1
2
3
4
int int_a = a;
if (a-int_a)
  printf("Double!\n");
else printf("Integer!\n");
Umm... can you explain the first two lines a bit? I'm a little confused on that.
the first line will effectively get the integer part of your double a variable.

and the if will evaluate to true if that subtraction is any number except zero, which will happen only if the double a and int_a are 'equal'.

I'll repeat abhishekm71's question: Do you consider input of "3.0" as double or an integer?
Last edited on
why not use find() function in <string> header to search for a '.' like abbhiskehm suggested? If it is found, output that it is a 'double'.

http://www.cplusplus.com/reference/string/string/find/
I have a simple one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
bool integerOrFloat(int a)
{
    return false;
}

bool integerOrFloat(float a)
{
    return true;
}

int main()
{
    float num1 = 2.5;
    std::cout << num << " is: ";
    if(integerOrFloat(num) == true)
    {
        std::cout << "FLOAT!\n";
    }
    else
    {
        std::cout << "INTEGER!\n";
    }
    integerOrFloat(2.0); // returns true (means FLOAT)
    integerOrFloat(3); // returns false (means INTEGER)
}


Why don't you try this... If the value is a double then when it is rounded with floor() the new value will be different, however if it's an integer then floor() will not effect the value.

My only concern is if you enter something such as 3.0 whether your compiler will choose to treat 3.0 as the same as 3, or whether it says they're different values.
If this does happen then I suppose you can compare floor(test) to ceil(test) and they should equal the same for an integer no matter what strange things the compiler might do.

1
2
3
4
5
6
7
8
9
10
11
#include <cmath>
#include <stdio>

int main(){
    double test
    scanf("%f", &test)
    if(floor(test) == test)
        printf("Integer");
    else
        printf("Double");
}
Last edited on
@SatsumaBenji & @abhishekm71 Your ways worked! Thanks a lot! :)
@mutexe 3.0 is considered as integer
Last edited on
@Sakudo no problem, that's what this forum is for.
abhishekm71's method is risky because if you, for example, have some calculated numbers whose result mathematically should be exactly some integer, it may actually not be due to the inherent imprecision of floating point arithmetic. This would cause the difference to be not exactly 0.

Ciu's method is not really useful as it will just call the function based on the type of what you pass it. If you pass 3, it will cal the int version but if you pass 3.0f it'll call the float version, which may not be the result you want.

SatsumaBenji's method has the same issue as abhishekm71's, causing a calculation result to not exactly return an integer and so be different from the result of floor().

If you want perfect results, I'd recommend simply parsing the string directly as abhishekm71 suggested at the start.
Topic archived. No new replies allowed.