Velocity Reviews > compare two float values

# compare two float values

neha_chhatre@yahoo.co.in
Guest
Posts: n/a

 02-20-2008
can anybody tell me how to compare two float values
say for example
t and check are two variables declared float how to compare t and
check

Joachim Schmitz
Guest
Posts: n/a

 02-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> can anybody tell me how to compare two float values
> say for example
> t and check are two variables declared float how to compare t and
> check

float t, check;
....
if ( t > check)
printf("%f is greater than %f\n" t, check);
else
printf("%f is less than %f\n" t, check);

Don't compare for equality, won't work for float (or double), rather compare
the difference against some delta that suits your needs for accuracy

Bye, Jojo

Joachim Schmitz
Guest
Posts: n/a

 02-20-2008
Joachim Schmitz wrote:
> (E-Mail Removed) wrote:
>> can anybody tell me how to compare two float values
>> say for example
>> t and check are two variables declared float how to compare t and
>> check

> float t, check;
> ...
> if ( t > check)
> printf("%f is greater than %f\n" t, check);
> else
> printf("%f is less than %f\n" t, check);
>
> Don't compare for equality, won't work for float (or double), rather
> compare the difference against some delta that suits your needs for
> accuracy

oops, epsilon, not delta, off by one in greek alphabeth...

neha_chhatre@yahoo.co.in
Guest
Posts: n/a

 02-20-2008
actually i want to check wether two float values are equal or not
equal
i.e suppose t and t1 are two float values and i want to check wether t
and t1 are equal or not

please send in syntax form as in code form

please tell me how to do,

fred.l.kleinschmidt@boeing.com
Guest
Posts: n/a

 02-20-2008
On Feb 20, 6:47*am, "Joachim Schmitz" <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > can anybody tell me how to compare two float values
> > say for example
> > t and check are two variables declared float how to compare t and
> > check

>
> float t, check;
> ...
> if ( t > check)
> * * printf("%f is greater than %f\n" t, check);
> else
> * * printf("%f is less than %f\n" t, check);
>
> Don't compare for equality, won't work for float (or double), rather compare
> the difference against some delta that suits your needs for accuracy
>
> Bye, Jojo

Of course it will work:
if ( t == check ) {...}
WILL execute the block if the float value 't' is equal to the
float value 'check'.

It depends on how the values for 't' and 'check' were assigned
whether this is accurate enough to provide the desired results.
For example:

t = 10.0;
check = 5.0 + 5.0;

The above test will work, since the two values will
be identical.

However, for:
t = 11.0;
check = 10 * 1.1;

the above comparison (t==check), will most likely NOT work
to determine whether these are mathematically identical.

Bottom line: using the == operator will always check for
exact equality of the two entities. However, either of the
two entities being checked (or both) may not represent the
exact mathematical value desired, and will probably
differ if their calcualtion and assignment were not
identical.
--
Fred Kleinschmidt

Mark Bluemel
Guest
Posts: n/a

 02-20-2008
(E-Mail Removed) wrote:
> actually i want to check wether two float values are equal or not
> equal
> i.e suppose t and t1 are two float values and i want to check wether t
> and t1 are equal or not
>
> please send in syntax form as in code form

Smells like homework to me...

have you looked at the FAQ (http://www.c-faq.com)? There's a whole
section on floating point numbers, and question 14.5 is probably
close to what you want.

Joachim Schmitz
Guest
Posts: n/a

 02-20-2008
(E-Mail Removed) wrote:
> On Feb 20, 6:47 am, "Joachim Schmitz" <(E-Mail Removed)>
> wrote:
>> (E-Mail Removed) wrote:
>>> can anybody tell me how to compare two float values
>>> say for example
>>> t and check are two variables declared float how to compare t and
>>> check

>>
>> float t, check;
>> ...
>> if ( t > check)
>> printf("%f is greater than %f\n" t, check);
>> else
>> printf("%f is less than %f\n" t, check);
>>
>> Don't compare for equality, won't work for float (or double), rather
>> compare the difference against some delta that suits your needs for
>> accuracy
>>
>> Bye, Jojo

>
> Of course it will work:
> if ( t == check ) {...}
> WILL execute the block if the float value 't' is equal to the
> float value 'check'.
>
> It depends on how the values for 't' and 'check' were assigned
> whether this is accurate enough to provide the desired results.
> For example:
>
> t = 10.0;
> check = 5.0 + 5.0;
>
> The above test will work, since the two values will
> be identical.
>
> However, for:
> t = 11.0;
> check = 10 * 1.1;
>
> the above comparison (t==check), will most likely NOT work
> to determine whether these are mathematically identical.
>
>
> Bottom line: using the == operator will always check for
> exact equality of the two entities. However, either of the
> two entities being checked (or both) may not represent the
> exact mathematical value desired, and will probably
> differ if their calcualtion and assignment were not
> identical.

That's what I meant.

Bye, Jojo

Kenneth Brody
Guest
Posts: n/a

 02-20-2008
(E-Mail Removed) wrote:
>
> actually i want to check wether two float values are equal or not
> equal
> i.e suppose t and t1 are two float values and i want to check wether t
> and t1 are equal or not
>
> please send in syntax form as in code form
>
> please tell me how to do,

How would you check for equality if t and t1 were "int"?

How would you check for equality if t and t1 were "short"?

How would you check for equality if t and t1 were "long"?

Now, how would you check for equality if t and t1 were "float"?

Or did you have something else in mind for what "equal" means?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

santosh
Guest
Posts: n/a

 02-20-2008
(E-Mail Removed) wrote:

> actually i want to check wether two float values are equal or not
> equal
> i.e suppose t and t1 are two float values and i want to check wether t
> and t1 are equal or not
>
> please send in syntax form as in code form
>
> please tell me how to do,

Please quote relevant parts of the post to which you are replying. This
is good practise on Usenet.

Comparing floats for equality is fraught with danger as many values are
not perfectly representible in binary floating point. You might find
that you need to allow for a small variation on either side of the
comparison value, just like the margin of error in scientific
experiments.

<http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm>
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>

Pietro Cerutti
Guest
Posts: n/a

 02-20-2008
Kenneth Brody wrote:
> (E-Mail Removed) wrote:
>> actually i want to check wether two float values are equal or not
>> equal
>> i.e suppose t and t1 are two float values and i want to check wether t
>> and t1 are equal or not
>>
>> please send in syntax form as in code form
>>
>> please tell me how to do,

>
> How would you check for equality if t and t1 were "int"?
>
> How would you check for equality if t and t1 were "short"?
>
> How would you check for equality if t and t1 were "long"?
>
> Now, how would you check for equality if t and t1 were "float"?

In a different way than above....

>
> Or did you have something else in mind for what "equal" means?
>

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp