Domanda

Secondo questo sito la funzione di errore erf (x) viene da math.h. Ma in realtà guardando in math.h, non c'è, e gcc non può compilare il seguente programma di test mentre g ++ può:

#include <math.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
  double x;
  double erfX;
  x = 1.0;
  erfX = erf(x);

  printf("erf(%f) = %f", x, erfX);
}

$ gcc mathHTest.c
/tmp/ccWfNox5.o: In function `main':
mathHTest.c:(.text+0x28): undefined reference to `erf'
collect2: ld returned 1 exit status
$ g++ mathHTest.c

Cosa non tira g ++ in quel gcc? Guardando in / usr / include, l'unico posto in cui ho potuto trovare erf (x) era in tgmath.h, che non includo. Quindi g ++ deve afferrare intestazioni diverse da gcc, ma quali?

EDIT: non stavo collegando in libm con gcc, quindi l'errore di collegamento. Tuttavia, non capisco ancora perché erf () non sia in math.h. Da dove viene?

È stato utile?

Soluzione

'erf' è effettivamente dichiarato in bit / mathcalls.h, che è #incluso da math.h. L'attuale dichiarazione è fortemente oscurata dalla macro magia per far sì che faccia la cosa giusta sia per C che per C ++

Altri suggerimenti

Ho avuto un problema simile e avevo bisogno di trovare la definizione esatta di erf , quindi lasciami espandere su questo. Come affermato da Chris Dodd, la funzione è dichiarata in bits / mathcalls.h che è inclusa in maths.h .

bit / mathcalls.h :

...
#if defined __USE_MISC || defined __USE_XOPEN || defined __USE_ISOC99
__BEGIN_NAMESPACE_C99
/* Error and gamma functions.  */
__MATHCALL (erf,, (_Mdouble_));
__MATHCALL (erfc,, (_Mdouble_));
__MATHCALL (lgamma,, (_Mdouble_));
__END_NAMESPACE_C99
#endif
...

La macro magia espande __MATHCALL (erf ,, (_Mdouble_)); in

extern double erf (double) throw (); extern double __erf (double) throw ();

Il codice attuale è in libm.a o libm.so ( gcc -lm ):

$ nm /usr/lib/libm.a
...
s_erf.o:
00000400 T __erf
00000000 T __erfc
         U __ieee754_exp
00000400 W erf
00000000 W erfc
...

La fonte può essere ottenuta dal gnu libc pagina web. Per un'idea approssimativa sull'attuazione effettiva qui alcune righe della fonte:

sysdeps / IEEE754 / DBL-64 / s_erf.c :

/* double erf(double x)
 * double erfc(double x)
 *                           x
 *                    2      |\
 *     erf(x)  =  ---------  | exp(-t*t)dt
 *                 sqrt(pi) \|
 *                           0
 *
 *     erfc(x) =  1-erf(x)
 *  Note that
 *              erf(-x) = -erf(x)
 *              erfc(-x) = 2 - erfc(x)
 *
 * Method:
 *      1. For |x| in [0, 0.84375]
 *          erf(x)  = x + x*R(x^2)
 *          erfc(x) = 1 - erf(x)           if x in [-.84375,0.25]
 *                  = 0.5 + ((0.5-x)-x*R)  if x in [0.25,0.84375]
 *         where R = P/Q where P is an odd poly of degree 8 and
 *         Q is an odd poly of degree 10.
 *                                               -57.90
 *                      | R - (erf(x)-x)/x | <= 2
 *
 *
 *         Remark. The formula is derived by noting
 *          erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
 *         and that
 *          2/sqrt(pi) = 1.128379167095512573896158903121545171688
 *         is close to one. The interval is chosen because the fix
 *         point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
 *         near 0.6174), and by some experiment, 0.84375 is chosen to
 *         guarantee the error is less than one ulp for erf.
 *
 *      2. For |x| in [0.84375,1.25], let s = |x| - 1, and     
 ...

Devi anche collegare la libreria matematica ( libm ):

$ gcc mathHTest.c -lm

Tutte le normali funzioni della libreria matematica sono effettivamente presenti e non nella libreria C standard ( libc ).

Secondo i miei test, g ++ include automaticamente libm , ma gcc no.

Ho avuto lo stesso problema usando gcc da cygwin su un processore x86. Il " -lm " la libreria include il parametro (dopo l'elenco dei file!) ha funzionato perfettamente.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top