C Language                
            Standard Math
        
        
            
    Suche…
Syntax
- #include <math.h>
- Doppelschlag (doppeltes x, doppeltes y);
- float powf (float x, float y);
- langes doppeltes powl (langes doppeltes x, langes doppeltes y);
Bemerkungen
-  Um eine Verknüpfung mit der Mathematikbibliothek -lmverwenden Sie-lmmit gcc-Flags.
-  Ein tragbares Programm, das nach einem Fehler in einer mathematischen Funktion errno, sollteerrnoauf null setzen und den folgenden Aufruffeclearexcept(FE_ALL_EXCEPT);bevor Sie eine mathematische Funktion aufrufen. Bei der Rückkehr von der mathematischen Funktion wird, wennerrnoungleich Null ist, oder der folgende Aufruf einen ungleich nullfetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);dann trat ein Fehler in der mathematischen Funktion auf. Lesen Sie die Manpage von math_error für weitere Informationen.
Fließkomma-Rest mit doppelter Genauigkeit: fmod ()
 Diese Funktion gibt den Fließkomma-Rest der Division von x/y . Der zurückgegebene Wert hat das gleiche Vorzeichen wie x. 
#include <math.h> /* for fmod() */
#include <stdio.h> /* for printf() */
int main(void)
{
    double x = 10.0;
    double y = 5.1;
    double modulus = fmod(x, y);
    printf("%lf\n", modulus); /* f is the same as lf. */
    return 0;
}
Ausgabe:
4.90000
Wichtig: Verwenden Sie diese Funktion mit Vorsicht, da unerwartete Werte aufgrund der Verwendung von Gleitkommawerten zurückgegeben werden können.
#include <math.h>
#include <stdio.h>
int main(void)
{
    printf("%f\n", fmod(1, 0.1));
    printf("%19.17f\n", fmod(1, 0.1));
    return 0;
}
Ausgabe:
0.1
0.09999999999999995
Gleitkomma-Rest mit einfacher Genauigkeit und langer doppelter Genauigkeit: fmodf (), fmodl ()
 C99 
         Diese Funktionen geben den Fließkomma-Rest der Division von x/y . Der zurückgegebene Wert hat das gleiche Vorzeichen wie x. 
Mit einfacher Genauigkeit:
#include <math.h> /* for fmodf() */
#include <stdio.h> /* for printf() */
int main(void)
{
    float x = 10.0;
    float y = 5.1;
    float modulus = fmodf(x, y);
    printf("%f\n", modulus); /* lf would do as well as modulus gets promoted to double. */
}
Ausgabe:
4.90000
Doppelte Doppelpräzision:
#include <math.h> /* for fmodl() */
#include <stdio.h> /* for printf() */
int main(void)
{
    long double x = 10.0;
    long double y = 5.1;
    long double modulus = fmodl(x, y);
    printf("%Lf\n", modulus); /* Lf is for long double. */
}
Ausgabe:
4.90000
Power-Funktionen - pow (), powf (), powl ()
Der folgende Beispielcode berechnet die Summe der Reihen 1 + 4 (3 + 3 ^ 2 + 3 ^ 3 + 3 ^ 4 + ... + 3 ^ N) unter Verwendung der pow () - Familie der Standard-Mathematik-Bibliothek.
#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
int main()
{
        double pwr, sum=0;
        int i, n;
        printf("\n1+4(3+3^2+3^3+3^4+...+3^N)=?\nEnter N:");
        scanf("%d",&n);
        if (n<=0) {
                printf("Invalid power N=%d", n);
                return -1;
        }
        for (i=0; i<n+1; i++) {
                errno = 0;
                feclearexcept(FE_ALL_EXCEPT);
                pwr = powl(3,i);
                if (fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
                        FE_UNDERFLOW)) {
                        perror("Math Error");
                }
                sum += i ? pwr : 0;
                printf("N= %d\tS= %g\n", i, 1+4*sum);
        }
        return 0;
}
Beispielausgabe:
1+4(3+3^2+3^3+3^4+...+3^N)=?
Enter N:10
N= 0    S= 1
N= 1    S= 13
N= 2    S= 49
N= 3    S= 157
N= 4    S= 481
N= 5    S= 1453
N= 6    S= 4369
N= 7    S= 13117
N= 8    S= 39361
N= 9    S= 118093
N= 10    S= 354289
Modified text is an extract of the original Stack Overflow Documentation
        Lizenziert unter CC BY-SA 3.0
        Nicht angeschlossen an Stack Overflow