Re: compare two float values
Eric Sosman wrote:
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
Eric Sosman wrote:
CBFalconer wrote:
>
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
>neha_chhatre@ya hoo.co.in wrote:
>>
>>
>#include <float.h /* FLT_EPSILON */
>#include <math.h /* fabsf() */
>>
>int floatequal(floa t 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 */
>>
>>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
>>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(floa t 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
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
Comment