[zurück] | 2.3. Spezielle mathematische Funktionen |
[vor] |
Mit dem Befehl dir lassen sich alle Funktionen des Moduls math anzeigen.
print(dir(math)) | ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] |
Betrag
print(abs(4)) | 4 |
print(abs(4.0)) | 4.0 |
print(abs(-4)) | 4 |
print(math.fabs(-4)) | 4.0 |
Natürlicher Logarithmus und Eulersche Zahl
print(math.e) | 2.71828182846 |
print(math.exp(1)) | 2.71828182846 |
print(math.log(math.e)) | 1.0 |
Dekadischer Logarithmus
print(math.log10(100)) | 2.0 |
log1p(x)
print(math.log1p(5)) | 1.79175946923 |
print(math.log(1 + 5)) | 1.79175946923 |
frexp(x)
print(math.frexp(5)) | (0.625, 3) |
print(0.625*2**3) | 5.0 |
print(math.ldexp(0.625, 3)) | 5.0 |
Modulo (Rest einer Division)
print(7 % 2) | 1 |
print(8 % 2) | 0 |
print(23 % 5) | 3 |
print(23.0 % 5) | 3.0 |
print(3487 % 71) | 8 |
print(math.fmod(7, 2)) | 1.0 |
Fakultät
print(math.factorial(5)) | 120 |
Summe
print(math.fsum([1, 3, 7, 4])) | 15.0 |
Hypothenuse
print(math.hypot(3, 4)) | 5.0 |
print(math.sqrt(3**2 + 4**2)) | 5.0 |
modf(x)
print(math.modf(33.55)) | (0.54999999999999716, 33.0) |
print(math.modf(33.55)[0]) | 0.55 |
print(math.modf(33.55)[1]) | 33.0 |
[zurück] | [Inhaltsverzeichnis] | [vor] |