Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Standardmathematik | Einführung
C++ Datentypen

book
Standardmathematik

Zusätzlich zu den Operationen +, -, *, / und % können wir viele weitere mathematische Operationen mit Funktionen aus der <cmath>-Bibliothek durchführen. Hier ist eine Tabelle der am häufigsten verwendeten:

All dies ist Teil der Standard-Mathematikbibliothek. Um sie zu verwenden, müssen Sie die entsprechende Header-Datei einbinden. Dies können Sie tun, indem Sie entweder cmath oder math.h einbinden.

h

include

copy
#include <cmath>
// OR
#include <math.h>
123
#include <cmath> // OR #include <math.h>

Der Hauptunterschied zwischen cmath und math.h liegt in ihrem Zweck und ihrer Verwendung. cmath ist Teil der C++ Standardbibliothek, speziell für C++ entwickelt und erfordert den std Namensraum (z.B. std::sqrt()), während math.h aus der C Standardbibliothek stammt, den globalen Namensraum verwendet (z.B. sqrt()) und mit C++ kompatibel ist.

cpp

main

copy
#include <iostream>
#include <cmath>

int main()
{
std::cout << "cos(0) = " << cos(0) << std::endl;
std::cout << "sin(0) = " << sin(0) << std::endl;
std::cout << "tan(pi/4) = " << tan(M_PI/4) << std::endl;
std::cout << "exp(1) = " << exp(1) << std::endl;
std::cout << "log(e) = " << log(M_E) << std::endl;
std::cout << "pow(2, 3) = " << pow(2, 3) << std::endl;
std::cout << "sqrt(4) = " << sqrt(4) << std::endl;
std::cout << "cbrt(8) = " << cbrt(8) << std::endl;
std::cout << "ceil(7.8) = " << ceil(7.8) << std::endl;
std::cout << "floor(7.8) = " << floor(7.8) << std::endl;
std::cout << "round(7.8) = " << round(7.8) << std::endl;
}
1234567891011121314151617
#include <iostream> #include <cmath> int main() { std::cout << "cos(0) = " << cos(0) << std::endl; std::cout << "sin(0) = " << sin(0) << std::endl; std::cout << "tan(pi/4) = " << tan(M_PI/4) << std::endl; std::cout << "exp(1) = " << exp(1) << std::endl; std::cout << "log(e) = " << log(M_E) << std::endl; std::cout << "pow(2, 3) = " << pow(2, 3) << std::endl; std::cout << "sqrt(4) = " << sqrt(4) << std::endl; std::cout << "cbrt(8) = " << cbrt(8) << std::endl; std::cout << "ceil(7.8) = " << ceil(7.8) << std::endl; std::cout << "floor(7.8) = " << floor(7.8) << std::endl; std::cout << "round(7.8) = " << round(7.8) << std::endl; }

Hinweis

Konstanten M_PI und M_E für den Wert von π (3.1415...) und e (2.7183...) sind ebenfalls in der <cmath>-Bibliothek definiert.

question mark

Welche der folgenden Möglichkeiten ist die geeignetste, um die Mathematikbibliothek in ein C++-Programm einzubinden?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 4
We use cookies to make your experience better!
some-alt