Rational
Klasse Rational
: Rationale Zahl
Rational(p, q) Beispiel: Rational(3,17)
Rational(n) Beispiel: Rational(3)
Rational(s) Beispiel: Rational("3/17")
Rational
-Objekte können mit den
arithmetischen Operatoren +, -, *, / verknüpft werden.
Übersicht:
Rational (p, q=1)
Operator | Bedeutung |
abs(x) | |
x + y | Addition |
x / y | a / b à a.__div__(b), if a is of type Rational b.__rdiv__(a), otherwise |
x == y | |
float(x) | |
x // y | |
int(x) | |
long(x) | |
x * y | a * b à a.__mul__(b), if a is of type Rational b.__rmul__(a), otherwise |
x != y | |
-x | -a à a.__neg__() |
str(x) | |
x - y | a - b à a.__sub__(b), if a is of type Rational b.__rsub__(a), otherwise |
x / y |
Methode | Bedeutung |
abs () | Absolutbetrag |
copy () | unabhängige Kopie von r |
isInteger () | |
operators () | Nur zur Dokumentation |
toFloat () | Konvertierung in Gleitkommazahl |
toInt () | Rundung zur nächsten ganzen Zahl |
toStr () | Darstellung als String |
Konstruktor
Aufruf: Rational(p, q=1)
Absolutbetrag
Aufruf: abs(x)
a.__add__(b)
Aufruf: x + y
Beschreibung:
Addition
Also operator notation: a + b
Instead of a = a + b
you can write a += b
.
exact Division
Aufruf: x / y
Beschreibung:
a / b à a.__div__(b), if a is of type Rational
b.__rdiv__(a), otherwise
Aufruf: x == y
Umwandlung in Gleitkommazahl
Aufruf: float(x)
Aufruf: x // y
Auf-/Abrundung zur nächsten Ganzzahl
Aufruf: int(x)
Auf-/Abrundung zur nächsten Ganzzahl
Aufruf: long(x)
Multiplication:
Aufruf: x * y
Beschreibung:
a * b à a.__mul__(b), if a is of type Rational
b.__rmul__(a), otherwise
Aufruf: x != y
Unary operator -
Aufruf: -x
Beschreibung:
-a à a.__neg__()
Object representation as string
Aufruf: str(x)
Beschreibung:
Subtraction:
Aufruf: x - y
Beschreibung:
a - b à a.__sub__(b), if a is of type Rational
b.__rsub__(a), otherwise
Aufruf: x / y
r.abs()
Aufruf: self.abs()
Beschreibung:
Absolutbetrag
Auch globale Funktion: abs(r)
r.copy()
Aufruf: self.copy()
Beschreibung:
unabhängige Kopie von r
Aufruf: self.isInteger()
Rational.operators()
Aufruf: self.operators()
Beschreibung:
Nur zur Dokumentation
Die Klasse Rational erlaubt folgende Operatoren:
Op. | Funktion | Beispiele |
---|---|---|
+ | Addition | a + b; a += b |
- | Subtraktion | a - b; a -= b |
* | Multiplikation | a * b; a *= b |
/ | Division | a / b; a /= b |
- | unäres Minus | -a |
< > <= >= | Vergleichsoperatoren |
r.toFloat()
Aufruf: self.toFloat()
Beschreibung:
Konvertierung in Gleitkommazahl
Auch globale Funktion: float(r)
r.toInt()
Aufruf: self.toInt()
Beschreibung:
Rundung zur nächsten ganzen Zahl
Auch globale Funktion: int(r)
r.toStr()
Aufruf: self.toStr()
Beschreibung:
Darstellung als String
Auch globale Funktion: str(r)