3.4: Rational and Complex Numbers
- Page ID
- 63977
One advantage that they have is that the numerator and denominator are stored as integers (64-bit by default) and are not subject to round-off errors that floating points are. The standard operations +, −, ·, ÷ between rationals results in a rational and as we will see in this course, there are advantages to using rationals instead of floating points.
Exercise
-
\(\dfrac{1}{2} + \dfrac{2}{3}\)
-
\(\dfrac{1}{2} - \dfrac{2}{3}\)
- \(\dfrac{2}{3} \cdot \dfrac{3}{5}\)
- \(\dfrac{2}{3} \div \dfrac{3}{5}\)
The Rational Type
If you enter
is called a Parametric Composite Type, which will be talked about later. In this particular case, this is a rational type, but inside it (the numerator and denominator), they are type Int64.
For example, to make a different type of rational you need to declare a different integer type inside, enter