Fraction ObjectFraction objectExecuted this code:
int a = 10;
double recip = 1.0 / a;
double product = 0;
// add 1/a to itself a times
for (int i = 0; i < a; i++) {
product += recip;
}
double one = product;
int iter = 50;
for (int i = 0; i < iter; i++) {
one = one * one;
}
With idealized arithmetic
product = 1.0, one = 1.0
In reality
product = 0.99...9 or product = 1.00...01
one could be literally any non-negative value
Fixing this issue with design!
Floating point numbers are represented as (binary) decimal expansions.
0.33...3
float and double are fixed sizes (# of bits/digit)Think about how to represent precise fractional values in a computer
Fraction objectHint: no rounding errors for integer values
+ and *)?Fraction?