Re: comparing doubles for equality



Richard Weeks <rweeks@xxxxxxxxxx> wrote:
Jens Thoms Toerring wrote:
Richard Weeks <rweeks@xxxxxxxxxx> wrote:
int dbl_isequal(double a, double b)
{
double fraca, fracb, reldiff;

if((int)a == (int)b)

This probably will fail for numbers that can't be represented as
integers, e.g. for numbers like the numbers in the order of the
number of particles in one Mol of a substance which is ca. 6e23
and probably not an uncommon number in scientific calculations.
But on many systems such a number (or just the national debt of
some countries;-) won't fit into an int (or even a long int)

In which case standard C (without extended precision routines)
will be inadequate for the calculations anyway.

Not necessarily - you may not need 24 digits of precision (you
rarely care about a few million molecules in a Mol of gas;-)
but you may want to know if two results you arrived at using
different methods are identical within the precision your double
type give you.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: 12 hour clock and offset problem
    ... not violate precision in time based on precise longitude location. ... even more complex if adjustment is made including seconds. ... is lessened by use of the int() function within perl core. ...
    (comp.lang.perl.misc)
  • Re: double to int conversion yields strange results
    ... giving me two different values for the int. ... the question raised is not about the precision of the ... is done in extended precision. ... It is the conversion to 'double' before the conversion to 'int' that ...
    (comp.lang.c)
  • Re: byte + byte -> int
    ... is in range for an int, but allows the line ... overflow, and the rules specifically address overflow. ... it thinks precision might be lost in a down-conversion. ... had one nice feature that our sleek fast modern machines have lost: ...
    (comp.lang.java.programmer)
  • Re: test whether a double is even?
    ... > On my machine an int is 32 bits, so I would lose precision by converting ... Your original question was about testing a number if it's ... course you may loose precision casting a big number to int, short, or char, ...
    (comp.programming)
  • Re: My scripting language - any suggestions?
    ... there will be a problem with precision. ... anything from Float it also does the property of Float being an ... Now what will happen if one argument is int and another ... conversion to int will not work ...
    (comp.compilers)