Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old June 18th, 2007, 06:55 AM
nas
Guest
 
Posts: n/a
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

  #2  
Old June 18th, 2007, 07:25 AM
red floyd
Guest
 
Posts: n/a

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


nas wrote:
Quote:
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.
  #3  
Old June 18th, 2007, 09:45 AM
James Kanze
Guest
 
Posts: n/a

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


On Jun 18, 8:15 am, red floyd <no.s...@here.dudewrote:
Quote:
nas wrote:
Quote:
Quote:
Is there any way that i can find wether float variable contains
fraction??
for eg:-
Quote:
Quote:
if( isWholeNumber(b))
{
//do here
}
Quote:
Quote:
isWholeNumber(float var) should return true for the float numbers like
10.000 ,11.000 etc
Quote:
Quote:
and it shuold be false for numbers like 10.112 (if it contains
fraction), 11.092123
Quote:
look up fmod() in your favorite reference.
Interesting. I would have used modf(). (Both will work.)

--
James Kanze (GABI Software, from CAI) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  #4  
Old June 18th, 2007, 10:35 AM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a

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


On 2007-06-18 07:52, nas wrote:
Quote:
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.

--
Erik Wikström
  #5  
Old June 18th, 2007, 10:55 AM
nas
Guest
 
Posts: n/a

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


On Jun 18, 2:28 am, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
On 2007-06-18 07:52, nas wrote:
>
>
>
>
>
Quote:
Hi
>
Quote:
Is there any way that i can find wether float variable contains
fraction??
for eg:-
>
Quote:
if( isWholeNumber(b))
{
//do here
}
>
Quote:
isWholeNumber(float var) should return true for the float numbers like
10.000 ,11.000 etc
>
Quote:
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.
>
--
Erik Wikström- Hide quoted text -
>
- Show quoted text -
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??

  #6  
Old June 18th, 2007, 11:05 AM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a

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


On 2007-06-18 11:47, nas wrote:
Quote:
On Jun 18, 2:28 am, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
>On 2007-06-18 07:52, nas wrote:
>>
>>
>>
>>
>>
Quote:
Hi
>>
Quote:
Is there any way that i can find wether float variable contains
fraction??
for eg:-
>>
Quote:
if( isWholeNumber(b))
{
//do here
}
>>
Quote:
isWholeNumber(float var) should return true for the float numbers like
10.000 ,11.000 etc
>>
Quote:
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.
>>
>--
>Erik Wikström- Hide quoted text -
>>
>- Show quoted text -
>
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).

--
Erik Wikström
  #7  
Old June 18th, 2007, 11:15 AM
Juha Nieminen
Guest
 
Posts: n/a

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


Erik Wikström wrote:
Quote:
Cast the number to int and back to float/double again and compare.
That works with float but not with double if the value is too large.
  #8  
Old June 19th, 2007, 06:05 PM
Marcus Kwok
Guest
 
Posts: n/a

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


James Kanze <james.kanze@gmail.comwrote:
Quote:
On Jun 18, 8:15 am, red floyd <no.s...@here.dudewrote:
Quote:
>nas wrote:
Quote:
Is there any way that i can find wether float variable contains
fraction??
>
Quote:
>look up fmod() in your favorite reference.
>
Interesting. I would have used modf(). (Both will work.)
I would have suggested floor() (or possibly ceil() for negative
numbers).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
  #9  
Old June 19th, 2007, 09:15 PM
Guillermo Schwarz
Guest
 
Posts: n/a

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


On Jun 19, 1:05 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Quote:
James Kanze <james.ka...@gmail.comwrote:
Quote:
On Jun 18, 8:15 am, red floyd <no.s...@here.dudewrote:
Quote:
nas wrote:
Is there any way that i can find wether float variable contains
fraction??
>
Quote:
Quote:
look up fmod() in your favorite reference.
>
Quote:
Interesting. I would have used modf(). (Both will work.)
>
I would have suggested floor() (or possibly ceil() for negative
numbers).
>
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
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.

  #10  
Old June 19th, 2007, 09:25 PM
Victor Bazarov
Guest
 
Posts: n/a

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


Guillermo Schwarz wrote:
Quote:
On Jun 19, 1:05 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Quote:
>James Kanze <james.ka...@gmail.comwrote:
Quote:
>>On Jun 18, 8:15 am, red floyd <no.s...@here.dudewrote:
>>>nas wrote:
>>>>Is there any way that i can find wether float variable contains
>>>>fraction??
>>
Quote:
>>>look up fmod() in your favorite reference.
>>
Quote:
>>Interesting. I would have used modf(). (Both will work.)
>>
>I would have suggested floor() (or possibly ceil() for negative
>numbers).
>>
>--
>Marcus Kwok
>Replace 'invalid' with 'net' to reply
>
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...
Quote:
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'.


  #11  
Old June 19th, 2007, 10:05 PM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a

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


On 2007-06-19 22:16, Victor Bazarov wrote:
Quote:
Guillermo Schwarz wrote:
Quote:
>On Jun 19, 1:05 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Quote:
>>James Kanze <james.ka...@gmail.comwrote:
>>>On Jun 18, 8:15 am, red floyd <no.s...@here.dudewrote:
>>>>nas wrote:
>>>>>Is there any way that i can find wether float variable contains
>>>>>fraction??
>>>
>>>>look up fmod() in your favorite reference.
>>>
>>>Interesting. I would have used modf(). (Both will work.)
>>>
>>I would have suggested floor() (or possibly ceil() for negative
>>numbers).
>>>
>>--
>>Marcus Kwok
>>Replace 'invalid' with 'net' to reply
>>
>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...
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 :-)

--
Erik Wikström
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 15th, 2005 12:17 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 08:57 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 07:46 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 03:55 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 5 November 14th, 2005 12:36 PM