[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Sammenligning af to tal



"Sune Kirkeby" <sslug@sslug> writes:

[...]

> Jeg kan ikke umiddelbart komme på en brugbar funktion...
> Men hvis jeg har forstået problemet korrekt kan du bare
> beregne foreskellen ml. de to værdier. Hvis forskellen er
> mindre end 1 / (10 ^ d) er tallene ens op til og med mindst
> den d'te decimal.

For passende definitioner af `ens' ( |0.999 - 1.000| ) ;-)

Hvis man vitterlig ønsker ens decimaler i stedet for den bedre
`matematiske' metode ovenfor kan man gøre således:

#include <math.h>

/* Returner 1 hvis a og b har d ens decimaler efter kommaet.
   a og b skal have samme fortegn */
int ens_decimaler (double a, double b, int d)
{
  double s = pow (10, d);

  return fabs (floor (a*s) - floor (b*s)) < 0.5;
}

Man kunne godt have sagt floor() == floor(), men det er generelt en
dårlig ide at sammenligne floating point variable på den måde på grund
af afrundingsfejl.

Hvis du vil have bedre præcision, end du kan få fra double, så kig på
GNU MP biblioteket, som kommer med RedHat 5.2 (pakkerne gmp og
gmp-devel, se info gmp).

-- 
Jakob Stoklund Olesen, Department of Mathematics, University of Aarhus
http://www.imf.au.dk/~stoklund/          PGP public key ID: 0x4f06279d


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *