Math.h

math.h ist eine Header-Datei in der Standard C Library der Programmiersprache C. Sie wurde für mathematische Funktionen entwickelt. Die Programmiersprache C++ benutzt die Funktionen ebenfalls, um die Kompatibilität von C zu C++ zu gewährleisten, und deklariert sie in der Header-Datei cmath (dort ohne die Dateinamenserweiterung „.h“ verwendet).

Alle Funktionen, die einen Winkel einlesen oder ausgeben, arbeiten mit Radiant. Die meisten Funktionen arbeiten mit Gleitkommazahlen. Mathematische Funktionen, die mit ganzzahligen Werten (Integer) arbeiten, wie abv, labs, div oder ldiv, sind stattdessen in der Header-Datei stdlib.h vertreten.

Funktionen bis C95

In den Normen bis einschließlich C95 waren die folgenden Funktionen deklariert.

Name Beschreibung Mathematische Formulierung
acos Arkuskosinus arccos x {\displaystyle \arccos x}
asin Arkussinus arcsin x {\displaystyle \arcsin x}
atan Arkustangens arctan x {\displaystyle \arctan x}
atan2 „Arkustangens“ mit zwei Argumenten atan2 ( y , x ) {\displaystyle \operatorname {atan2} (y,x)}
ceil Aufrundungsfunktion x {\displaystyle \lceil x\rceil }
cos Kosinus cos x {\displaystyle \cos x}
cosh Kosinus hyperbolicus cosh x {\displaystyle \cosh x}
exp Exponentialfunktion e x {\displaystyle e^{x}}
fabs Betragsfunktion | x | {\displaystyle |x|}
floor Ganzteilfunktion x {\displaystyle \lfloor x\rfloor }
fmod Führt die Modulo-Funktion für Gleitkommazahlen durch x mod y {\displaystyle x{\bmod {y}}}
frexp Teilt eine Gleitkommazahl in Faktor und Potenz mit der Basis 2 auf
ldexp Multipliziert den ersten Parameter mit 2 um den zweiten Parameter potenziert x 2 y {\displaystyle x2^{y}}
log Natürlicher Logarithmus ln x {\displaystyle \ln x}
log10 Logarithmus zur Basis 10 log 10 x {\displaystyle \log _{10}x}
modf Teilt eine Gleitkommazahl in zwei Zahlen auf, vor und nach dem Komma
pow Potenziert ersten mit dem zweiten Parameter x y {\displaystyle x^{y}}
sin Sinus sin x {\displaystyle \sin x}
sinh Sinus hyperbolicus sinh x {\displaystyle \sinh x}
sqrt Quadratwurzel x {\displaystyle {\sqrt {x}}}
tan Tangens tan x {\displaystyle \tan x}
tanh Tangens hyperbolicus tanh x {\displaystyle \tanh x}

C99-Funktionen

Mit der Norm C99 wurde math.h um die folgenden Funktionen erweitert.

Name Beschreibung Mathematische Formulierung
acosh Areakosinus hyperbolicus arcosh x {\displaystyle \operatorname {arcosh} x}
asinh Areasinus hyperbolicus arsinh x {\displaystyle \operatorname {arsinh} x}
atanh Areatangens hyperbolicus artanh x {\displaystyle \operatorname {artanh} x}
cbrt Kubikwurzel x 3 {\displaystyle {\sqrt[{3}]{x}}}
copysign(x,y) gibt den Wert von x mit dem Vorzeichen von y zurück | x | sgn y {\displaystyle |x|\cdot \operatorname {sgn} y}
erf Fehlerfunktion erf x {\displaystyle \operatorname {erf} x}
erfc Gibt den Komplementärfehler von x zurück erfc x {\displaystyle \operatorname {erfc} x}
exp2(x) Potenziert 2 mit dem übergebenen Parameter 2 x {\displaystyle 2^{x}}
expm1(x) Liefert den Wert von exp()-1 zurück e x 1 {\displaystyle e^{x}-1}
fdim(x,y) Positive Differenz max ( 0 , x y ) {\displaystyle \max(0,x-y)}
fma(x,y,z) Multipliziert und Addiert x y + z {\displaystyle xy+z}
fmax(x,y) Maximum max ( x , y ) {\displaystyle \max(x,y)}
fmin(x,y) Minimum min ( x , y ) {\displaystyle \min(x,y)}
hypot(x,y) Hypotenuse ( x 2 + y 2 ) {\displaystyle {\sqrt {(x^{2}+y^{2})}}}
ilogb wie logb, gibt allerdings int zurück (int)logb(x)
lgamma Logarithmus der Gammafunktion ln | Γ ( x ) | {\displaystyle \ln |\Gamma (x)|}
llrint Rundungsfunktion
lrint Rundungsfunktion
llround Rundungsfunktion
lround Rundungsfunktion
log1p(x) Natürlicher Logarithmus von 1 + x ln ( 1 + x ) {\displaystyle \ln(1+x)}
log2 Logarithmus zur Basis 2 lb x {\displaystyle \operatorname {lb} x}
logb Liefert den ganzzahligen Exponenten einer Gleitkommazahl als Gleitkommazahl rint ( log F L T _ R A D I X | x | ) {\displaystyle \operatorname {rint} (\log _{\mathrm {FLT\_RADIX} }|x|)}
nan(s) Ein NaN erzeugen 0 / 0 {\displaystyle 0/0}
nearbyint Rundet Gleitkommazahlen zum nächsten Integer
nextafter(x,y) Gibt die nächst darstellbare Zahl nach x (Richtung y) zurück
nexttoward(x,y) Wie nextafter, außer dass y als long double übergeben wird
remainder(x,y) Rest einer Division
remquo(x,y,p) Genauso wie remainder, speichert jedoch den Quotienten (als int) als Ziel des Zeigers p
rint Rundet je nach Rundungsmodus zum nächsten Integer, gibt eine Gleitkommazahl zurück
round kaufmännische Rundungsfunktion x + 0 , 5 {\displaystyle \lfloor x+0{,}5\rfloor }
scalbln(x,y) x * FLT_RADIXy (y ist long) x F L T _ R A D I X y {\displaystyle x\cdot \mathrm {FLT\_RADIX} ^{y}}
scalbn(x,y) x * FLT_RADIXy (y ist int) x F L T _ R A D I X y {\displaystyle x\cdot \mathrm {FLT\_RADIX} ^{y}}
tgamma Gammafunktion Γ ( x ) {\displaystyle \Gamma (x)}
trunc Beschneidet eine Gleitkommazahl, d. h. rundet „Richtung Null“ { x , x 0 x , x < 0 {\displaystyle {\begin{cases}\lfloor x\rfloor ,&x\geq 0\\\lceil x\rceil ,&x<0\end{cases}}}

Beispiel

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

int main() {
    double a = 5, b = 4;
    double c = pow(a, b);
    printf("%f hoch %f ist %f\n", a, b, c);
}
  • Rationale for International Standard – Programming Languages – C. (PDF, 898 kB) Abgerufen am 4. September 2011 (C99). 
  • Beschreibung auf cplusplus.com (englisch)
  • Dokumentation von math.h auf dinkumware.com (Memento vom 21. Februar 2010 im Internet Archive)