This commit is contained in:
Claudio Maggioni 2022-09-21 11:51:41 +02:00
parent ea85b783c1
commit 5f121f7b95
5 changed files with 28 additions and 8 deletions

BIN
Project0/src/ciao Executable file

Binary file not shown.

View file

@ -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)

Binary file not shown.

Binary file not shown.

View file

@ -1,6 +1,7 @@
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#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);
}