Slide 21
Slide 21 text
Closest approximation to the
desired accuracy
Irrationals of the Real type are implemented internally via
functions from precision to approximation.
Unlike Float and Double, the approximation is not stored for
each operation.
def +(y: Real): Real = (x, y) match {
case (Exact(nx), Exact(ny)) => Exact(nx + ny)
case (Exact(Rational.zero), _) => y
case (_, Exact(Rational.zero)) => x
case _ =>
Real(p => roundUp(Rational(x(p + 2) + y(p + 2), 4)))
}
Real型の無理数は精度を受け取り近似値を返す関数で実装さ
れています。求める精度に最も近い近似値を得ます。