Search code examples
c++runtimenotation

C++ Mean and Median Absolute Deviation Returning Same Weird Notation


I am working on two functions to calculate the mean absolute deviation and median absolute deviation of a vector data set. I am using an overloaded calcAverage function inside of them. The problem is, I am returning the same incorrect value for both function calls.

This is the output. Is it giving me scientific notation or something?

Mean absolute deviation = 4.09929e-016
Median absolute deviation = 4.09929e-016

This is the mean absolute distribution function:

double calcMeanAD(vector<int> data_set){

vector<double> lessMean;
double mean = calcAverage(data_set);

for (auto it = data_set.begin(); it != data_set.end(); ++it){
    lessMean.push_back(*it);
}

for (auto it = lessMean.begin(); it != lessMean.end(); ++it){
    *it -= mean;
}

return calcAverage(lessMean);

}

This is the median absolute distribution function:

double calcMedAD(vector<int> data_set){

vector<double> lessMed;
double median = calcAverage(data_set);

for (auto it = data_set.begin(); it != data_set.end(); ++it){
    lessMed.push_back(static_cast<double>(*it));
}

for (auto it = lessMed.begin(); it != lessMed.end(); ++it){
    *it -= median;
}

return calcAverage(lessMed);

}

Can anybody spot something/s that is/are wrong? Thanks.


Solution

  • Both functions are returning zero with some roundoff error.

    Write down an algebraic expression for the values you are trying to calculate, and compare that with your code.

    I don't know what calcAverage does, but it's not overloaded; you are calling it with a vector<double> both times. There is no way it can calculate both a mean and a median.

    Hint: you seem to have missed the meanings of absolute and median