Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > compare two float values

Reply
Thread Tools

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
please help me as soon as possible
 
Reply With Quote
 
 
 
 
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
> please help me as soon as possible

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


 
Reply With Quote
 
 
 
 
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
>> please help me as soon as possible

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


 
Reply With Quote
 
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,
 
Reply With Quote
 
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
> > please help me as soon as possible

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

 
Reply With Quote
 
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.

 
Reply With Quote
 
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
>>> please help me as soon as possible

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


 
Reply With Quote
 
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)>

 
Reply With Quote
 
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.

You might want to read:

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

 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Float to int conversion by using two int variables for representation of the float variable k3n3dy C++ 15 04-20-2006 06:53 PM
How to compare values of two objects in C# Sukh ASP .Net 3 02-06-2006 05:50 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments