diff --git a/Project0/src/ciao b/Project0/src/ciao new file mode 100755 index 0000000..5cfc551 Binary files /dev/null and b/Project0/src/ciao differ diff --git a/Project0/src/fraction_summing/fraction_toolbox.cpp b/Project0/src/fraction_summing/fraction_toolbox.cpp index b3963c7..90e973e 100644 --- a/Project0/src/fraction_summing/fraction_toolbox.cpp +++ b/Project0/src/fraction_summing/fraction_toolbox.cpp @@ -79,24 +79,26 @@ fraction add_fractions(fraction frac1, fraction frac2) fraction sum_fraction_array(fraction frac_array[], int n) { fraction sum = { 0, 1 }; - print_fraction_array(frac_array, n); for (int i = 0; i < n; i++) { fraction frac = frac_array[i]; sum = add_fractions(sum, frac); - print_fraction(sum); } return sum; } double sum_fraction_array_approx(fraction frac_array[], int n) { - return fraction2double(sum_fraction_array(frac_array, n)); + double sum = 0; + for (int i = 0; i < n; i++) { + sum += fraction2double(frac); + } + return sum; // the approximation in this function is given by the fact that floating // point numbers cannot represent precisely rational numbers due to the - // fixed size of significant digits they can hold in the mantissa. This - // implementation minimizes the approximation by computing the sum into a - // fraction FIRST before performing the double conversion. + // fixed size of significant digits they can hold in the mantissa. The + // approximation adds up for each fraction to double conversion performed in + // the loop } void fill_fraction_array(fraction frac_array[], int n) diff --git a/Project0/src/fraction_summing/fraction_toolbox.o b/Project0/src/fraction_summing/fraction_toolbox.o new file mode 100644 index 0000000..7baa377 Binary files /dev/null and b/Project0/src/fraction_summing/fraction_toolbox.o differ diff --git a/Project0/src/fraction_summing/main b/Project0/src/fraction_summing/main new file mode 100755 index 0000000..611f16d Binary files /dev/null and b/Project0/src/fraction_summing/main differ diff --git a/Project0/src/fraction_summing/main.cpp b/Project0/src/fraction_summing/main.cpp index d263207..4f07c36 100644 --- a/Project0/src/fraction_summing/main.cpp +++ b/Project0/src/fraction_summing/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "fraction_toolbox.hpp" @@ -70,8 +71,19 @@ static void test_array_functions(int n) cout << "Sum of array as double (approx.): " << endl; cout << sum_fraction_array_approx(a, n) << endl; - - //TODO: find n for which sum function breaks. Explain what is happening. + + // find n for which sum function breaks. Explain what is happening. n=4448, + // this is the first number where the numerator overflows and thus + // erroneoulsly becomes a negative number. The overflow is caused by the + // sum_fractions function, specifically in the multiplication between + // denominators. The approximation function doesn't overflow since fractions + // are first converted into double floating point numbers, thus never + // calling the sum_fractions functions and performing dangerous fixed size + // integer multiplications. Additionally, due to the floating point nature + // of the double type, a sum of doubles will never overflow to something + // "weird", albeit precision will be lost due to the limited number of + // significant digits and for larger and larger values the sum will + // eventually reach the value "+Infinity" due to the fixed exponent size. } static void test_toolbox(int argc, char* argv[]) @@ -83,7 +95,13 @@ static void test_toolbox(int argc, char* argv[]) test5(); cout << "\n========== test_array_functions ========= " << endl; +#if 1 + cout << "Input n: " << endl; + int n; + cin >> n; +#else int n = 5; +#endif test_array_functions(n); }