🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
9 messages
9 total messages Started by Alya Al Omrani Tue, 10 Jul 2001 11:01
Floating point equal to Integer Value ?
#64
Author: Alya Al Omrani
Date: Tue, 10 Jul 2001 11:01
25 lines
2046 bytes
Hi, how can i check if a floating point value is equal to integer value ?
I tried looking of there is a function that does so but there isn't

I tried doing the following

/* lets say those are my values */
int x=20;
float x=20.0, z;

z=(float)x;
/* I used a printf staetment to check that the value of x was assigned to z
and it worked ! */
printf("%f", z);

/* here i want to check */
if(z==x)                      /* at this instant it freezes */
    printf("values are equal");
else
    printf("they r not euqal");
///////////////


I sthere a substitue method that i can do so, i tried taking the % modulus
of both z and x to see if it will give zero but i got same erro, the program
wont compile
Re: Floating point equal to Integer Value ?
#65
Author: jan
Date: Tue, 10 Jul 2001 10:15
45 lines
2482 bytes
Where's the problem?
You have a redefinition int x and float x, which doesn't work
You can just compare with:
int x = 20;
float y =20.0;
if(x==y)
    printf("Equal!");
else
    printf ("Not equal!");

That's all. You can also cast (converting diffrent types):
y = (float)x;

"Alya Al Omrani" <alomrani@emirates.net.ae> schrieb im Newsbeitrag
news:9iejot$4an2@news.emirates.net.ae...
> Hi, how can i check if a floating point value is equal to integer value ?
> I tried looking of there is a function that does so but there isn't
>
> I tried doing the following
>
> /* lets say those are my values */
> int x=20;
> float x=20.0, z;
>
> z=(float)x;
> /* I used a printf staetment to check that the value of x was assigned to
z
> and it worked ! */
> printf("%f", z);
>
> /* here i want to check */
> if(z==x)                      /* at this instant it freezes */
>     printf("values are equal");
> else
>     printf("they r not euqal");
> ///////////////
>
>
> I sthere a substitue method that i can do so, i tried taking the % modulus
> of both z and x to see if it will give zero but i got same erro, the
program
> wont compile
>
>
>
Re: Floating point equal to Integer Value ?
#67
Author: j
Date: Tue, 10 Jul 2001 15:58
18 lines
2297 bytes
> if(z==(float)x)

Er, would this not mess things up?  Floating the int *could* move off to
nearest float (your 20 could become 19.999999999, etc) and that may or may
not be == to z.  Depending on what you want, you may want to be more
precise:

const float tol = 0.00001;
float f, chek;
int i;
f = i = 20;
chek = fabs(f-i);
if(chek < tol) //then they are more or less equal. Tol could be the smallest
representable float, but I would use 10 times this or more.
  etc....

I don't think this is an issue for small integers; it may not be an issue at
all, not sure.
Re: Floating point equal to Integer Value ?
#66
Author: Gregory Carvalho
Date: Tue, 10 Jul 2001 20:17
38 lines
2572 bytes
Alya Al Omrani wrote:
> 
> Hi, how can i check if a floating point value is equal to integer value ?
> I tried looking of there is a function that does so but there isn't
> 
> I tried doing the following
> 
> /* lets say those are my values */
> int x=20;
> float x=20.0, z;
> 
> z=(float)x;
> /* I used a printf staetment to check that the value of x was assigned to z
> and it worked ! */
> printf("%f", z);
> 
> /* here i want to check */
> if(z==x)                      /* at this instant it freezes */
>     printf("values are equal");
> else
>     printf("they r not euqal");
> ///////////////
> 
> I sthere a substitue method that i can do so, i tried taking the % modulus
> of both z and x to see if it will give zero but i got same erro, the program
> wont compile

Are you getting an error since you are defining x twice:

> int x=20;
> float x=20.0, z;

You can cast the comparison:

int x=20;
float z=20.0;
...
if(z==(float)x)
Re: Floating point equal to Integer Value ?
#68
Author: Sanjay kumar
Date: Wed, 11 Jul 2001 07:37
36 lines
2451 bytes
Hi I think it would be no problem if you convert float to integer
instead of interger to float just for comparision purpose

instead of (float(x) == y) 
to (x == (int) y)

this will never fail.

Sanjay 

"Alya Al Omrani" <alomrani@emirates.net.ae> wrote in message news:<9iejot$4an2@news.emirates.net.ae>...
> Hi, how can i check if a floating point value is equal to integer value ?
> I tried looking of there is a function that does so but there isn't
> 
> I tried doing the following
> 
> /* lets say those are my values */
> int x=20;
> float x=20.0, z;
> 
> z=(float)x;
> /* I used a printf staetment to check that the value of x was assigned to z
> and it worked ! */
> printf("%f", z);
> 
> /* here i want to check */
> if(z==x)                      /* at this instant it freezes */
>     printf("values are equal");
> else
>     printf("they r not euqal");
> ///////////////
> 
> 
> I sthere a substitue method that i can do so, i tried taking the % modulus
> of both z and x to see if it will give zero but i got same erro, the program
> wont compile
Re: Floating point equal to Integer Value ?
#69
Author: Richard Harter
Date: Wed, 11 Jul 2001 09:09
27 lines
2540 bytes
On Tue, 10 Jul 2001 14:01:37 +0300, "Alya Al Omrani"
<alomrani@emirates.net.ae> wrote:

>Hi, how can i check if a floating point value is equal to integer value ?
>I tried looking of there is a function that does so but there isn't

[snip code with problems]

Your question might mean one of two things:

(a) Is a particular floating point value, call it x, actually an
integer expressed in floating point form?

(b) Is a particular floating point value x equal to some integer y?

If we can answer (a) then it is trivial to answer (b).  Let us suppose
we have two functions, float and int.  Then, provided that x is in
normal form, then x is an integer in floating point form if
	x .bweq. float(int(x))
where .bweq. denotes bitwise equality, i.e., x and float(int(x)) have
the same bit pattern.

 
Richard Harter, cri@tiac.net,
http://www.tiac.net/users/cri, http://www.varinoma.com
It is unfortunate that the dishonest politicians give
the remaining one percent of their profession a bad name.
Re: Floating point equal to Integer Value ?
#70
Author: Michael Rubenstein
Date: Wed, 11 Jul 2001 11:56
14 lines
1826 bytes
On 11 Jul 2001 00:37:31 -0700, san_2820@yahoo.com (Sanjay kumar)
wrote:

>Hi I think it would be no problem if you convert float to integer
>instead of interger to float just for comparision purpose
>
>instead of (float(x) == y) 
>to (x == (int) y)
>
>this will never fail.

What will happen if y is too large to fit in an int?
-- 
Michael M Rubenstein
Re: Floating point equal to Integer Value ?
#71
Author: Samantha Fox
Date: Mon, 30 Jul 2001 13:55
35 lines
2538 bytes
you can't even use == or != to compare two floats

"Alya Al Omrani" <alomrani@emirates.net.ae> wrote in message
news:9iejot$4an2@news.emirates.net.ae...
> Hi, how can i check if a floating point value is equal to integer value ?
> I tried looking of there is a function that does so but there isn't
>
> I tried doing the following
>
> /* lets say those are my values */
> int x=20;
> float x=20.0, z;
>
> z=(float)x;
> /* I used a printf staetment to check that the value of x was assigned to
z
> and it worked ! */
> printf("%f", z);
>
> /* here i want to check */
> if(z==x)                      /* at this instant it freezes */
>     printf("values are equal");
> else
>     printf("they r not euqal");
> ///////////////
>
>
> I sthere a substitue method that i can do so, i tried taking the % modulus
> of both z and x to see if it will give zero but i got same erro, the
program
> wont compile
>
>
>
Re: Floating point equal to Integer Value ?
#72
Author: Karl Heinz Buchegger
Date: Mon, 30 Jul 2001 15:00
12 lines
1615 bytes

Samantha Fox wrote:
> 
> you can't even use == or != to compare two floats
> 

Oh, you can! But the results of that test are often surprising.

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads