+ 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

18th Dec 2018, 12:14 PM
Shyamal Bhatt
Shyamal Bhatt - avatar
11 Antworten
+ 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
18th Dec 2018, 4:09 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 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
20th Dec 2018, 2:16 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
18th Dec 2018, 12:51 PM
Lighton
Lighton - avatar
+ 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.
20th Dec 2018, 1:03 PM
Lighton
Lighton - avatar
+ 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
18th Dec 2018, 12:21 PM
Lighton
Lighton - avatar
+ 1
Javier Felipe Toribio i agree, program wasn't work for big numbers
20th Dec 2018, 2:22 PM
Shyamal Bhatt
Shyamal Bhatt - avatar
+ 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.
20th Dec 2018, 5:22 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
0
TheZeroDoctor can you write the code and post it , I will be thankful if you do that
18th Dec 2018, 12:24 PM
Shyamal Bhatt
Shyamal Bhatt - avatar
0
Okay, wait a minute
18th Dec 2018, 12:31 PM
Lighton
Lighton - avatar
0
TheZeroDoctor Thanks man
18th Dec 2018, 12:54 PM
Shyamal Bhatt
Shyamal Bhatt - avatar
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
20th Dec 2018, 4:52 PM
Lighton
Lighton - avatar