472,989 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

compare two float values

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
Feb 20 '08 #1
26 7048
ne**********@yahoo.co.in 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
Feb 20 '08 #2
Joachim Schmitz wrote:
ne**********@yahoo.co.in 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...
Feb 20 '08 #3
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,
Feb 20 '08 #4
ne**********@yahoo.co.in 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.

Feb 20 '08 #5
ne**********@yahoo.co.in 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>

Feb 20 '08 #6
ne**********@yahoo.co.in 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: <mailto:Th*************@gmail.com>

Feb 20 '08 #7
Kenneth Brody wrote:
ne**********@yahoo.co.in 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
Feb 20 '08 #8
Kenneth Brody wrote:
ne**********@yahoo.co.in 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
Feb 20 '08 #9
ne**********@yahoo.co.in 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
#include <float.h /* FLT_EPSILON */
#include <math.h /* fabsf() */

int floatequal(float t, float t1) {

float criterion;

criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
if (fabsf(t - t1) criterion) return 0; /* not equal */
else return 1; /* equal */
} /* untested */

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 20 '08 #10
thank you for giving me the site url
Feb 20 '08 #11
CBFalconer wrote:
ne**********@yahoo.co.in 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

#include <float.h /* FLT_EPSILON */
#include <math.h /* fabsf() */

int floatequal(float t, float t1) {

float criterion;

criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
if (fabsf(t - t1) criterion) return 0; /* not equal */
else return 1; /* equal */
} /* untested */
Needs a little more work for NaNs and infinities ...

More to the point, the choice of a proper epsilon is
not dictated solely by the machine's precision, but also
by the computations that produced the numbers. For example,
if you calculate a binomial coefficient as

exp(lgamma(n)-lgamma(m)-lgamma(n-m))*n/(m*(n-m))

.... the accuracy of the result will quite likely depend a
good deal on the accuracies of the four transcendental
function evaluations, as well as on the fundamental precision
of the floating-point representation. An "approximately
equal" comparison based solely on the machine's epsilon might
lead one to conclude that the number five-card poker hands
(52 choose 5 == 52!/(5!*47!) == 2598960) is not an integer!

--
Er*********@sun.com
Feb 20 '08 #12
On Feb 20, 6:42*am, neha_chha...@yahoo.co.in 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
This should be a FAQ.

#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

int float_compare (float d1, float d2)
{
if (d1 d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabsf (d2 * FLT_EPSILON))
return 0;
else
return -1;
return 0;
}

#ifdef UNIT_TEST

#include <stdio.h>

int main ()
{
double u = 1.0;
double v = u + 1e-14;
double big = 1.0;
double a = 1.0, b = 0.99999999999999999999999999;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));
a *= -1;
b *= -1;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));

big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));
u *= big;
v *= big;

printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));

return 0;
}

#endif
Feb 20 '08 #13
Kenneth Brody wrote:
>
How would you check for equality if t and t1 were "int"?
Now, how would you check for equality if t and t1 were "float"?
By some different means. Two mathematically identical floats may compare
inequal by obvious methods.
Or did you have something else in mind for what "equal" means?
Doubtful, but he also probably expected the below to print "true".

#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%f %f %s\n",
exp(log(4.444)), 4.444,
exp(log(4.444))==4.444?"true":"false");
return 0;
}

$ ./a.out
4.444000 4.444000 false

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Feb 20 '08 #14
user923005 wrote:
neha_chha...@yahoo.co.in 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

This should be a FAQ.

#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}
What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? I think you are
trying to do too much in one routine. I also think I have the
foul-up condition fouled. :-)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 21 '08 #15
Eric Sosman wrote:
CBFalconer wrote:
>ne**********@yahoo.co.in 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

#include <float.h /* FLT_EPSILON */
#include <math.h /* fabsf() */

int floatequal(float t, float t1) {

float criterion;

criterion = (fabsf(t) + fabsf(t1)) * FLT_EPSILON;
if (fabsf(t - t1) criterion) return 0; /* not equal */
else return 1; /* equal */
} /* untested */

Needs a little more work for NaNs and infinities ...

More to the point, the choice of a proper epsilon is
not dictated solely by the machine's precision, but also
by the computations that produced the numbers. For example,
if you calculate a binomial coefficient as
Ignoring NaNs and infinities, the criterion I used is sufficient to
guarantee that an equal/nonequal decision can be made. At most
that criterion is too large by a factor of two, giving an excessive
range of equality. BTW, I believe there is no guarantee that NaNs
and infinities exist in the floating point system.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 21 '08 #16
On Feb 20, 5:00*pm, CBFalconer <cbfalco...@yahoo.comwrote:
user923005 wrote:
neha_chha...@yahoo.co.in 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
This should be a FAQ.
#include <float.h>
#include <math.h>
int double_compare (double d1, double d2)
{
* if (d1 d2)
* * if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
* * * return 0;
* * else
* * * return 1;
* if (d1 < d2)
* * if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
* * * return 0;
* * else
* * * return -1;
* return 0;
}

What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? *I think you are
trying to do too much in one routine. *I also think I have the
foul-up condition fouled. :-)
Try it.
Feb 21 '08 #17
On Feb 20, 5:00*pm, CBFalconer <cbfalco...@yahoo.comwrote:
user923005 wrote:
neha_chha...@yahoo.co.in 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
This should be a FAQ.
#include <float.h>
#include <math.h>
int double_compare (double d1, double d2)
{
* if (d1 d2)
* * if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
* * * return 0;
* * else
* * * return 1;
* if (d1 < d2)
* * if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
* * * return 0;
* * else
* * * return -1;
* return 0;
}

What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? *I think you are
trying to do too much in one routine. *I also think I have the
foul-up condition fouled. :-)
I would be very interested to see any instance where this compare
function fails to perform as expected.

C:\tmp>cl /DUNIT_TEST /W4 /Ox fcomp.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

fcomp.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:fcomp.exe
fcomp.obj

C:\tmp>fcomp
compare( 1, 1) = 0.
compare( -1, -1) = 0.
compare( 1, 1) = -1.
compare( 1.84467e+019, 1.84467e+019) = -1.
compare( 2.22045e-016, -1.79769e+308) = 1.

C:\tmp>type fcomp.c
#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

int float_compare (float d1, float d2)
{
if (d1 d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabsf (d2 * FLT_EPSILON))
return 0;
else
return -1;
return 0;
}

#ifdef UNIT_TEST

#include <stdio.h>

int main ()
{
double u = 1.0;
double v = u + 1e-14;
double big = 1.0;
double a = 1.0, b = 0.99999999999999999999999999;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));
a *= -1;
b *= -1;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, a, DBL_DIG+2, b,
double_compare (a, b));

big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));
u *= big;
v *= big;

printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));

u = DBL_EPSILON;
v = -DBL_MAX;
printf ("compare(%*g, %*g) = %d.\n", DBL_DIG+2, u, DBL_DIG+2, v,
double_compare (u, v));

return 0;
}

#endif
Feb 21 '08 #18
On Feb 20, 5:59*pm, user923005 <dcor...@connx.comwrote:
On Feb 20, 5:00*pm, CBFalconer <cbfalco...@yahoo.comwrote:
[snip]
What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? *I think you are
trying to do too much in one routine. *I also think I have the
foul-up condition fouled. :-)
This version has better formatting, which will allow us to see the
dirt.
That makes the output much easier to grok.

#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

int float_compare (float d1, float d2)
{
if (d1 d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabsf (d2 * FLT_EPSILON))
return 0;
else
return -1;
return 0;
}

#ifdef UNIT_TEST

#include <stdio.h>

int main ()
{
double u = 1.0;
double v = u + 1e-14;
double big = 1.0;
double a = 1.0, b = 0.99999999999999999999999999;
printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, a, DBL_DIG+5, b,
double_compare (a, b));
a *= -1;
b *= -1;
printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, a, DBL_DIG+5, b,
double_compare (a, b));

big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
big *= 1 << 16;
printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, u, DBL_DIG+5, v,
double_compare (u, v));
u *= big;
v *= big;

printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, u, DBL_DIG+5, v,
double_compare (u, v));

u = DBL_EPSILON;
v = -DBL_MAX;
printf ("compare(%.*g, %.*g) = %d.\n", DBL_DIG+5, u, DBL_DIG+5, v,
double_compare (u, v));

return 0;
}
/*
compare(1, 1) = 0.
compare(-1, -1) = 0.
compare(1, 1.00000000000001) = -1.
compare(18446744073709552000, 18446744073709736000) = -1.
compare(2.2204460492503131e-016, -1.7976931348623157e+308) = 1.
*/
#endif

Feb 21 '08 #19
Mark McIntyre <ma**********@spamcop.netwrites:
Kenneth Brody wrote:
>How would you check for equality if t and t1 were "int"?
Now, how would you check for equality if t and t1 were "float"?

By some different means. Two mathematically identical floats may
compare inequal by obvious methods.
Depends on what you mean by "mathematically identical".
>Or did you have something else in mind for what "equal" means?

Doubtful, but he also probably expected the below to print "true".

#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%f %f %s\n",
exp(log(4.444)), 4.444,
exp(log(4.444))==4.444?"true":"false");
return 0;
}

$ ./a.out
4.444000 4.444000 false
Here you have two floating-point values that result from calculations
that, if they were carried out with complete mathematical exactness,
would be equal. But the actual values are not equal, because
floating-point calculations aren't completely exact. In this case,
you can see that they're inexact by using a different format. For
example:

#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%.32f\n%.32f\n%s\n",
exp(log(4.444)),
4.444,
exp(log(4.444))==4.444 ? "true" : "false");
return 0;
}

4.44399999999999906208358879666775
4.44399999999999995026200849679299
false

If you want to know whether two floating-point values are "equal", the
first thing you have to ask yourself is just what that means. The
usual recommendation is to test whether the values are close enough --
but how close is close enough depends on the nature of the
calculations that generated the values. If the tolerance is too
tight, numbers that are logically equal might appear to be unequal; if
it's too loose, numbers that are logically distinct might appear to be
equal.

And sometimes exact equality (the built-in "==" operator) is just what
you want. For example, with a comparison function that checks whether
two numbers are within some epsilon of each other, you can have X == Y
and Y == Z, but X != Z. Whether that's a problem depends on the
algorithm.

Floating-point arithmetic is very probably harder than you think it is
unless your name is David Goldberg, and perhaps even if it is.
Speaking of which, see David Goldberg's paper "What Every Computer
Scientist Should Know About Floating-Point Arithmetic",
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 21 '08 #20
user923005 <dc*****@connx.comwrote:
On Feb 20, 6:42=A0am, neha_chha...@yahoo.co.in 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

This should be a FAQ.
And surprise, surprise, it is: 14.5, <http://c-faq.com/fp/fpequal.html>.

Richard
Feb 21 '08 #21
user923005 wrote:
On Feb 20, 5:59 pm, user923005 <dcor...@connx.comwrote:
>On Feb 20, 5:00 pm, CBFalconer <cbfalco...@yahoo.comwrote:
[snip]
>>What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? I think you are
trying to do too much in one routine. I also think I have the
foul-up condition fouled. :-)

This version has better formatting, which will allow us to see the
dirt.
That makes the output much easier to grok.

#include <float.h>
#include <math.h>
<snip>
int float_compare (float d1, float d2)
{
if (d1 d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
<snip>

Where is fabsf()? Doesn't seem to be avaible on the platform I'm using,
which claims to be C89.
What do use instead?

Bye, Jojo
Feb 21 '08 #22
Joachim Schmitz wrote:
user923005 wrote:
>user923005 <dcor...@connx.comwrote:
>>CBFalconer <cbfalco...@yahoo.comwrote:

[snip]

What if d1 == DBL_EPSILON and d2 == -LARGE_NUMBER? I think you
are trying to do too much in one routine. I also think I have
the foul-up condition fouled. :-)

This version has better formatting, which will allow us to see
the dirt. That makes the output much easier to grok.

#include <float.h>
#include <math.h>
<snip>
>int float_compare (float d1, float d2) {
if (d1 d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
<snip>

Where is fabsf()? Doesn't seem to be avaible on the platform I'm
using, which claims to be C89. What do use instead?
>From N869, for C99. For C90 use doubles and fabs.
7.12.7.2 The fabs functions
Synopsis
[#1]
#include <math.h>
double fabs(double x);
float fabsf(float x);
long double fabsl(long double x);

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Feb 21 '08 #23
On Feb 20, 9:29*pm, santosh <santosh....@gmail.comwrote:
neha_chha...@yahoo.co.in 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%20flo...>
<http://docs.sun.com/source/806-3568/ncg_goldberg.html>
I like the links. Nice Collection :):)

Karthik Balaguru
Feb 21 '08 #24
On Feb 20, 11:25*pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
user923005 <dcor...@connx.comwrote:
On Feb 20, 6:42=A0am, neha_chha...@yahoo.co.in 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
This should be a FAQ.

And surprise, surprise, it is: 14.5, <http://c-faq.com/fp/fpequal.html>.
Unfortunately, it's hopelessly broken:
C:\tmp>reldif
RelDif of 1 and -1 is 2

C:\tmp>type reldif.c
#define Abs(x) ((x) < 0 ? -(x) : (x))
#define Max(a, b) ((a) (b) ? (a) : (b))

double RelDif(double a, double b)
{
double c = Abs(a);
double d = Abs(b);

d = Max(c, d);

return d == 0.0 ? 0.0 : Abs(a - b) / d;
}

int main(void)
{
printf("RelDif of %.20g and %.20g is %.20g\n", 1.0, -1.0,
RelDif(1.0, -1.0));
return 0;
}
Feb 21 '08 #25
On Thu, 21 Feb 2008 11:53:18 -0800, user923005 wrote:
On Feb 20, 11:25*pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>And surprise, surprise, it is: 14.5,
<http://c-faq.com/fp/fpequal.html>.

Unfortunately, it's hopelessly broken: C:\tmp>reldif
RelDif of 1 and -1 is 2
What's wrong with that? What answer did you expect, and why?
Feb 21 '08 #26
On Feb 20, 2:47*pm, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
neha_chha...@yahoo.co.in 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
You are doing nothing but perpetuating stupid myths. In a correct C
implementation (and there are a few incorrect ones around), t < check
is true if t is less than check, t == check is true if t is equal to
check, t check is true if t is greater than check. In an IEEE 754
compatible implementation, exactly one of these three will be true
unless one of t or check is Not-A-Number, in which case neither
condition is true.

Once you accept this fact, you then need to accept that floating-point
operations are usually not exact, and two calculations that you think
should give the same result will often give results that are close
together but not necessarily the same. For example, if you write

double x = sqrt (2.0) * sqrt (3.0);
double y = sqrt (6.0);

you can be sure that the results will be close together, but which of
x < y, x == y and x y is true is hard to predict. But the point is:
If they are equal, then x == y is true, and if they are not equal then
x == y is false. The == operator will give you the correct result,
except that the correct result may not be what you thought the exact
result is.

Feb 22 '08 #27

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

Similar topics

5
by: PyPK | last post by:
I have two files file1 in format <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T F file2 same as file1 <id> <val1> <test1> <test2> 'AA' 1 T T 'AB' 1 T T
2
by: Shi Jin | last post by:
Hi there, I have been thinking this for quite a while: if we are considering the number of different representations of one type,say int and float, is there any difference as long as they are...
3
by: Andrew Murray | last post by:
Is it possible to compare a double with a float in c# without casting to one of the types first? It appears you cannot... float num = 1.545f; double dnum = 1.545; if (dnum == num)
2
by: Locia | last post by:
How can I compare "if argument"? example: if (leftExpression==RightExpression) After parsing I know the type of RightExpression. I suppone that if RightExpression is wrap into " " is a...
11
by: Marc Pelletier | last post by:
Hello, I am having trouble implementing the following callback: CNCSError CECWCompressor::WriteReadLine(UINT32 nNextLine, void **ppInputArray) where ppInputArray is a 3 by x array. The...
1
by: joesfer | last post by:
I'm trying to develop a graphical user interface for a renderer i've got written in an unmanaged C++ DLL with C#. During the rendering process, several images are sent to a delegate as float*...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
5
by: Mike | last post by:
Hi, I use MS SQL Express and VS 2005 c#, win application. I would like to select value rom DateTimePicker and list all values for selected date within GridView. I have method as follows: ...
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.