diff --git a/C/analyzer.c b/C/analyzer.c index e565bcaaec1ddd40cac4184960deb046da9394b3..86bff770ba464bb59c4ec4d5d9ff2dd8c634f605 100644 --- a/C/analyzer.c +++ b/C/analyzer.c @@ -7,7 +7,7 @@ analyzer_t * analyzer_create(){ analyzer_t * res = (analyzer_t *) malloc( sizeof(analyzer_t) ); res->capacity = 4; res->cost = (double *) malloc( sizeof(double) * res->capacity ); - res->cumulative_cost = (double *) malloc( sizeof(double) * res->capacity ); + res->cumulative_cost = (long double *) malloc( sizeof(long double) * res->capacity ); res->cumulative_square = 0.; res->size = 0; return res; @@ -26,7 +26,7 @@ void analyzer_append(analyzer_t * a, double x){ if( a->size >= (a->capacity * 3)/4 ){ a->capacity *= 2; a->cost = (double *) realloc(a->cost, sizeof(double) * a->capacity*2); - a->cumulative_cost = (double *) realloc(a->cumulative_cost, sizeof(double) * a->capacity*2); + a->cumulative_cost = (long double *) realloc(a->cumulative_cost, sizeof(long double) * a->capacity*2); } a->cost[a->size] = x; a->cumulative_cost[a->size] = (a->size) ? a->cumulative_cost[a->size-1]+x : x; diff --git a/C/analyzer.h b/C/analyzer.h index 9bbd71b56504ae872330a105adb8386ed6b1c59a..31dab2d8f30a76ee44afcda7f92e77800ca2ac57 100644 --- a/C/analyzer.h +++ b/C/analyzer.h @@ -12,9 +12,9 @@ typedef struct analyzer_s{ double * cost; // Coût cumulatif. La case i contient la somme des coûts des i premières opérations. // Permet de calculer le coût amorti d'une opération. - double * cumulative_cost; + long double * cumulative_cost; // Carré du coût cumulatif. Sert à calculer la variance. On ne garde que la dernière valeur. - double cumulative_square; + long double cumulative_square; // Capacité de stockage des tableaux size_t capacity; // Nombre d'éléments dans chaque tableaux. diff --git a/CPP/analyzer.hpp b/CPP/analyzer.hpp index ce14436dfe22fb11d9d380e87df23dc549ea6045..7eb9d04057b3e99e2d099cefe40c2ab68f683161 100644 --- a/CPP/analyzer.hpp +++ b/CPP/analyzer.hpp @@ -85,9 +85,9 @@ private: std::vector<double> cost; // Coût cumulatif. La case i contient la somme des coûts des i premières opérations. // Permet de calculer le coût amorti d'une opération. - std::vector<double> cumulative_cost; + std::vector<long double> cumulative_cost; // Carré du coût cumulatif. Sert à calculer la variance. On ne garde que la dernière valeur. - double cumulative_square; + long double cumulative_square; }; diff --git a/Java/Analyzer.java b/Java/Analyzer.java index 53cffadb583c29be7d8636f391b402ecd2cb677f..389163ac2eb71d78183b2ae9d0869316f8176c4b 100644 --- a/Java/Analyzer.java +++ b/Java/Analyzer.java @@ -1,4 +1,6 @@ import java.io.*; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; /** @@ -13,8 +15,8 @@ public class Analyzer { */ public Analyzer() { cost = new ArrayList<Double>(); - cumulative_cost = new ArrayList<Double>(); - cumulative_square = 0.; + cumulative_cost = new ArrayList<BigDecimal>(); + cumulative_square = new BigDecimal(0.); } /** @@ -25,9 +27,11 @@ public class Analyzer { @param x est la valeur que l'on souhaite ajouter à l'analyse. */ void append(double x){ + BigDecimal x_big = new BigDecimal(x); cost.add(x); - cumulative_cost.add( (!cumulative_cost.isEmpty()) ? cumulative_cost.get(cumulative_cost.size()-1)+x : x); - cumulative_square += x*x; + cumulative_cost.add( (!cumulative_cost.isEmpty()) ? cumulative_cost.get(cumulative_cost.size()-1).add(x_big) : x_big); + BigDecimal x_square = x_big.multiply(x_big); + cumulative_square = cumulative_square.add(x_square); } /** @@ -35,7 +39,7 @@ public class Analyzer { Complexité en temps/espace, meilleur cas : O(1) @returns la somme des coûts enregistrés dans cette analyse. */ - double get_total_cost(){ + BigDecimal get_total_cost(){ return cumulative_cost.get(cumulative_cost.size()-1); } @@ -45,8 +49,8 @@ public class Analyzer { @param pos est l'indice de l'opération pour laquelle on veut connaître le coût amorti. @returns le coût amorti d'une opération. */ - double get_amortized_cost(int pos){ - return (pos > 0)? cumulative_cost.get(pos)/pos : cumulative_cost.get(pos); + BigDecimal get_amortized_cost(int pos){ + return (pos > 0)? cumulative_cost.get(pos).divide(new BigDecimal(pos), RoundingMode.HALF_UP) : cumulative_cost.get(pos); } /** @@ -54,10 +58,10 @@ public class Analyzer { Complexité en temps/espace, meilleur cas : O(1) @returns la moyenne des coûts de toutes les opérations enregistrées dans l'analyse. */ - double get_average_cost(){ + BigDecimal get_average_cost(){ if(cumulative_cost.isEmpty()) throw new RuntimeException("List is empty"); - return cumulative_cost.get(cumulative_cost.size()-1)/cumulative_cost.size(); + return cumulative_cost.get(cumulative_cost.size()-1).divide(new BigDecimal(cumulative_cost.size())); } /** @@ -65,11 +69,13 @@ public class Analyzer { Complexité en temps/espace, meilleur cas : O(1) @returns la variance des coûts de toutes les opérations enregistrées dans l'analyse. */ - double get_variance(){ - double mean, mean_square; + BigDecimal get_variance(){ + BigDecimal mean, mean_square; mean = get_average_cost(); - mean_square = mean * mean; - return cumulative_square - mean_square; + mean_square = mean.multiply(mean); + if(cumulative_square.compareTo(mean_square) < 0) + throw new RuntimeException("Error: mean of squares is less than square of mean: "+mean+" "+cumulative_square); + return cumulative_square.subtract(mean_square); } /** @@ -77,8 +83,8 @@ public class Analyzer { Complexité en temps/espace, meilleur cas : O(1) @returns l'écart-type des coûts de toutes les opérations enregistrées dans l'analyse. */ - double get_standard_deviation(){ - return Math.sqrt(get_variance()); + BigDecimal get_standard_deviation(){ + return new BigDecimal(Math.sqrt(get_variance().doubleValue())); } /** @@ -124,9 +130,9 @@ public class Analyzer { private ArrayList<Double> cost; // Coût cumulatif. La case i contient la somme des coûts des i premières opérations. // Permet de calculer le coût amorti d'une opération. - private ArrayList<Double> cumulative_cost; + private ArrayList<BigDecimal> cumulative_cost; // Carré du coût cumulatif. Sert à calculer la variance. On ne garde que la dernière valeur. - private double cumulative_square; + private BigDecimal cumulative_square; }