Rational
Class Rational
: Rational numbers
Rational(p, q) Example: Rational(3,17)
Rational(n) Example: Rational(3)
Rational(s) Example: Rational("3/17")
For Rational
objects the arithmetical operators
+, -, *, / are defined.
Overview:
Rational (p, q=1)
Operator | Meaning |
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 |
Method | Meaning |
abs () | absolute value |
copy () | independent copy ("clone") of r |
isInteger () | |
operators () | For documentation only |
toFloat () | Conversion to floating point number |
toInt () | Round to the nearest integer |
toStr () | Representation as string |
Constructor
Usage: Rational(p, q=1)
absolute value
Usage: abs(x)
a.__add__(b)
Usage: x + y
Description:
Addition
Also operator notation: a + b
Instead of a = a + b
you can write a += b
.
exact Division
Usage: x / y
Description:
a / b à a.__div__(b), if a is of type Rational
b.__rdiv__(a), otherwise
Usage: x == y
Conversion to floating point number
Usage: float(x)
Usage: x // y
Round to the nearest integer
Usage: int(x)
Round to the nearest integer
Usage: long(x)
Multiplication:
Usage: x * y
Description:
a * b à a.__mul__(b), if a is of type Rational
b.__rmul__(a), otherwise
Usage: x != y
Unary operator -
Usage: -x
Description:
-a à a.__neg__()
Object representation as string
Usage: str(x)
Description:
Subtraction:
Usage: x - y
Description:
a - b à a.__sub__(b), if a is of type Rational
b.__rsub__(a), otherwise
Usage: x / y
r.abs()
Usage: self.abs()
Description:
absolute value
Also as global function: abs(r)
r.copy()
Usage: self.copy()
Description:
independent copy ("clone") of r
Usage: self.isInteger()
Rational.operators()
Usage: self.operators()
Description:
For documentation only
The following operators are defined in the class Rational:
Op. | Function | Examples |
---|---|---|
+ | Addition | a + b; a += b |
- | Subtraction | a - b; a -= b |
* | Multiplication | a * b; a *= b |
/ | Division | a / b; a /= b |
- | Unary minus | -a |
< > <= >= | Comparision operators |
r.toFloat()
Usage: self.toFloat()
Description:
Conversion to floating point number
Also as global function: float(r)
r.toInt()
Usage: self.toInt()
Description:
Round to the nearest integer
Also as global function: int(r)
r.toStr()
Usage: self.toStr()
Description:
Representation as string
Also as global function: str(r)