mercredi 24 mai 2017

Divide an unsigned long for a size_t and assign the result to a double

I have to divide an unsigned long int for a size_t (returned from a dimension of a array with size() ) like this:

vector<string> mapped_samples;
vector<double> mean;
vector<unsigned long> feature_sum;
.
. 
. elaboration here
.
.
mean.at(index) = feature_sum.at(index) /mapped_samples.size();

but in this way an integer divion takes place (I lose the decimal part. That's no good)

Therefore, I can do:

 mean.at(index) = feature_sum.at(index) /static_cast<double>(mapped_samples.size());

But in this way feature_sum.at(index) is automatically converted(Temporary copy) to a double and i could lose precision. How can I tackle the question? I have to use some library? Thanks in advance

(I'm using C++11)

Aucun commentaire:

Enregistrer un commentaire