Checking whether a number is int or float

I am working on a program that could calculate to calculate pth percentile from an array of size n. I have to check whether the product p*n gives an int or a float...i want to do doing

percentile = p*n
if(percentile == int)

cant really figure out what to put instead of this int so that it would tell me whether percentile is an int or a float. thanx
It's your code. When you made percentile, did you do it like this:

int percentile;

or like this?

float percentile;


Do you actually mean, is percentile a whole number (such as 12.0) or does it have a fractional amount (such as 12.23)?

Last edited on
i declared percentile as float
 
float percentile;

but obviously when i do
 
percentile = p*n;

answer can be a whole number too like 4.0.
So, yes, i want to check whether its a whole number or a fractional number?
Maybe:
int(percentile) == percentile?
o yes it worked. thanx ..... but i found another way too..

1
2
3
4
5
6
7
8
int percentile1;
float difference;
percentile1 == percentile;
difference = percentile - percentile1;
if(difference==0)
cout << "whole number" << endl;
else
cout << "fractional number" << endl;


These techniques are iffy at best. Remember that floating points are approximations and 10 might actually be 10.0000000001, in which case your above methods will fail.
you are right Disch, i just checked that. what can be the solution for that??
Don't use float. Use something with better precision. (Even double would be way better.)
double would be still inaccurate on it's lower bits.
You can test if two floating point numbers are approximately equal by testing if they are very close to each other :
1
2
3
4
bool is_float_equal(float x, float y) {    
    const float epsilon = 1.e-5; //very small number, margin of rounding error
    return std::abs(x-y) < epsilon;
}

(not tested)
Topic archived. No new replies allowed.