Is there any way that i can find wether float variable contains fraction??

N

nas

Hi

Is there any way that i can find wether float variable contains
fraction??
for eg:-

if( isWholeNumber(b))
{
//do here
}

isWholeNumber(float var) should return true for the float numbers like
10.000 ,11.000 etc

and it shuold be false for numbers like 10.112 (if it contains
fraction), 11.092123
 
R

red floyd

nas said:
Hi

Is there any way that i can find wether float variable contains
fraction??
for eg:-

if( isWholeNumber(b))
{
//do here
}

isWholeNumber(float var) should return true for the float numbers like
10.000 ,11.000 etc

and it shuold be false for numbers like 10.112 (if it contains
fraction), 11.092123

look up fmod() in your favorite reference.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi

Is there any way that i can find wether float variable contains
fraction??
for eg:-

if( isWholeNumber(b))
{
//do here
}

isWholeNumber(float var) should return true for the float numbers like
10.000 ,11.000 etc

and it shuold be false for numbers like 10.112 (if it contains
fraction), 11.092123

Cast the number to int and back to float/double again and compare.
 
N

nas

Cast the number to int and back to float/double again and compare.

Thanks for all the replies..
This is what i had thought initially(casting to int and back to
float)..but my doubt is wether this solution works every time?? i
mean..what i think is there will be a probem becoz of truncation.. but
i am not able to give the example...do any one have to say about this??
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Thanks for all the replies..
This is what i had thought initially(casting to int and back to
float)..but my doubt is wether this solution works every time?? i
mean..what i think is there will be a probem becoz of truncation.. but
i am not able to give the example...do any one have to say about this??

It should work for int and double on a x86 since a double can give an
exact representation of all number an int can (so a float and int should
also work). This won't work if the number is larger than can be
represented by an int (or long or whatever you use) but otherwise I can
see no problem (though I'm no expert on floating point numbers).
 
G

Guillermo Schwarz

I would have suggested floor() (or possibly ceil() for negative
numbers).

In fact it would be much better to use fractions in the first place,
and then if the fraction can't represent the number (for example if it
is PI or sqrt(2)) then use float or double.

Fractions never loose precision, so 1/3 * 3 is 1, exactly.
 
V

Victor Bazarov

Guillermo said:
In fact it would be much better to use fractions in the first place,
and then if the fraction can't represent the number (for example if it
is PI or sqrt(2)) then use float or double.

I always thought that PI was 22/7... Isn't that what Archimedes found
like, 2500 years ago?... Or was it Pythagoras? I may have had too much
coffee, and it's possible that Pi is not a single fraction but rather
a whole bunch of them...
Fractions never loose precision, so 1/3 * 3 is 1, exactly.

Cool. Better than the alternative, right? On my computer

std::cout << (1/3 * 3)

prints '0'.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I always thought that PI was 22/7... Isn't that what Archimedes found
like, 2500 years ago?... Or was it Pythagoras? I may have had too much
coffee, and it's possible that Pi is not a single fraction but rather
a whole bunch of them...

No, but 22/7, and even better 355/113, are close enough for most
practical purposes. The number is irrational and can not be expressed as
a fraction. For better approximations you can download the first few
millions of decimals from the net :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top