+ 1

# It's a integer or float?

My code works like this I input a random number program will divide the number and say wether the number is integer or float. I want to find the data type of number by using Boolean. I don't know who to do

11 Answers

+ 3

You could use modf from math.h library, in order to extract decimal part and check if it is zero. Example:
https://code.sololearn.com/c29OHjXbSokC/?ref=app

+ 3

TheZeroDoctor (result==(int)result) fails for big numbers. For example: 9999999999 / 3 = integer 3333333333. But your program shows result as float, because, result is 3333333248.0 !!! Using == with floats and double values which are results from a calculation is risky. modf detects that decimal is 0 in 9999999999 / 3 and you detect it's an integer

+ 2

Javier Felipe Toribio you are right. You can do it by this way too but why?
I think that it's make more complex code and it's not necessary.

+ 1

you can cast the number(result) to integer and check if it is equals to the number(result) or it is different.
If it is the same thing that means that it was integer already. Otherwise it is a float

+ 1

Javier Felipe Toribio i agree, program wasn't work for big numbers

+ 1

With 99999999999 / 3 it still fails. Calculation with big numbers is inaccurate. Besides, types int, long int, are limited by its maximum value and you don't know how big the input number is. I think using casting is not a good way to check if something is integer or not.

0

TheZeroDoctor can you write the code and post it , I will be thankful if you do that

0

Okay, wait a minute

0

TheZeroDoctor
Thanks man

0

Javier Felipe Toribio can you explain again why it is not working with big numbers? And check again my code, I do some changes