473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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

Jun 18 '07 #1
10 1377
nas wrote:
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.
Jun 18 '07 #2
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??
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.
Interesting. I would have used modf(). (Both will work.)

--
James Kanze (GABI Software, from CAI) email:ja*********@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

Jun 18 '07 #3
On 2007-06-18 07:52, nas wrote:
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
Jun 18 '07 #4
nas
On Jun 18, 2:28 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-06-18 07:52, nas wrote:


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- 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??

Jun 18 '07 #5
On 2007-06-18 11:47, nas wrote:
On Jun 18, 2:28 am, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-06-18 07:52, nas wrote:


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- 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
Jun 18 '07 #6
Erik Wikström wrote:
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.
Jun 18 '07 #7
James Kanze <ja*********@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
Jun 19 '07 #8
On Jun 19, 1:05 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
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.

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

Jun 19 '07 #9
Guillermo Schwarz wrote:
On Jun 19, 1:05 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>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...
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'.
Jun 19 '07 #10
On 2007-06-19 22:16, Victor Bazarov wrote:
Guillermo Schwarz wrote:
>On Jun 19, 1:05 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>>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
Jun 19 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Sebastian | last post by:
Hi, yesterday I discovered something that won't come as a surprise to more experienced C++ - programmers - but I was genuinely amazed. I had this line of code: float x = 0.9f; and when I had...
5
by: thirtyseven | last post by:
I was wondering if anyone knew of some public code for a c++ class that represents a value, but can preserve fractions and powers and such without just turning the value into a float. Hard to...
5
by: Vijay Kumar R Zanvar | last post by:
Hi, Just like counting the number of bits set to 1 in an integral variable, can we count the same in a float? I have two solutions in my mind: 1. union { float f;
11
by: rahulsinner | last post by:
hi everyone, main() { float f=0.7; if ( f< 0.7) printf("C"); else printf("C++"); }
5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
27
by: galt_57 | last post by:
I need to do just a few multiplies of long integers and have a divide by ten. Can I avoid using floats? Can I use two longs as a 64 bit value somehow? Thanks.
2
by: bkfromgkp | last post by:
dear all I want a textbox which contains only 5 numeric and 3 fraction. if nothing is written it should desplay .000 as i enter data first five numeric value should be placed before decimal...
35
by: bala.pandu | last post by:
Hello Everyone, When i am assigning a float value and try to print the same, i found the value stored is not the exact value which i gave. int main() { float f1; printf("Enter a float value...
3
by: Myxamatosis | last post by:
we're given an implementation file to base our class of Fraction off of. Input and output are in the format x/y, with x and y being ints, with the "/" character in the middle. so to create an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.