diff --git a/src/statistics.cpp b/src/statistics.cpp index c53ae99..2ac18c4 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -50,7 +50,7 @@ std::optional Statistics::fn_perm(dc::Stack &stack, co } unsigned long long permutation = numerator_opt.value() / denominator_opt.value(); - stack.push(trim_digits(permutation, parameters.precision)); + stack.push(trim_digits(static_cast(permutation), parameters.precision)); } else { return "'gP' requires integer values"; } @@ -89,7 +89,7 @@ std::optional Statistics::fn_comb(dc::Stack &stack, co combination /= i; } - stack.push(trim_digits(combination, parameters.precision)); + stack.push(trim_digits(static_cast(combination), parameters.precision)); } else { return "'gC' requires integer values"; } @@ -144,10 +144,10 @@ std::optional Statistics::fn_mean(dc::Stack &stack, co return "The stack of register 'X' is empty"; } - // Othewise compute mean + // Otherwise compute mean auto summation = regs['X'].stack.summation(); auto size = regs['X'].stack.size(); - auto mean = summation / size; + auto mean = summation / static_cast(size); stack.push(trim_digits(mean, parameters.precision)); return std::nullopt; @@ -164,10 +164,10 @@ std::optional Statistics::fn_sdev(dc::Stack &stack, co return "The stack of register 'X' is empty"; } - // Othewise, compute the mean + // Otherwise, compute the mean auto summation = regs['X'].stack.summation(); auto count = regs['X'].stack.size(); - auto mean = summation / count; + auto mean = summation / static_cast(count); // Then compute the sum of the deviations from the mean and square the result const auto& const_vec = regs['X'].stack.get_ref(); @@ -176,8 +176,8 @@ std::optional Statistics::fn_sdev(dc::Stack &stack, co double deviation = std::stod(val) - mean; return acc + std::pow(deviation, 2); }); - // Then compute the mean of previos values(variance) - auto variance = sum_of_deviations / (count-1); + // Then compute the mean of previous values(variance) + auto variance = sum_of_deviations / (static_cast(count) - 1); // Finally, compute the square root of the variance(standard deviation) auto s_dev = sqrt(variance); @@ -212,7 +212,7 @@ std::optional Statistics::fn_lreg(dc::Stack &stack, co return "'X' and 'Y' registers must be of the same length"; } - // Othwerise, retrieve count and summations of both sets + // Otherwise, retrieve count and summations of both sets auto count = regs['X'].stack.size(); auto x_sum = regs['X'].stack.summation(); auto y_sum = regs['Y'].stack.summation(); @@ -223,7 +223,7 @@ std::optional Statistics::fn_lreg(dc::Stack &stack, co std::size_t idx = 0; double sum_of_products = 0.0; - for(auto it : x_ref) { + for(const auto& it : x_ref) { auto x = std::stod(it); auto y = std::stod(y_ref[idx++]); sum_of_products += (x * y); @@ -233,12 +233,12 @@ std::optional Statistics::fn_lreg(dc::Stack &stack, co auto x_sum_squares = regs['X'].stack.summation_squared(); // Then compute the slope of the line(m) - auto slope_numerator = ((count * sum_of_products) - (x_sum * y_sum)); - auto slope_denominator = ((count * x_sum_squares) - std::pow(x_sum, 2)); + auto slope_numerator = ((static_cast(count) * sum_of_products) - (x_sum * y_sum)); + auto slope_denominator = ((static_cast(count) * x_sum_squares) - std::pow(x_sum, 2)); auto slope = slope_numerator / slope_denominator; // Then compute the intercept of the line(b) - auto intercept = (y_sum - (slope * x_sum)) / count; + auto intercept = (y_sum - (slope * x_sum)) / static_cast(count); // Finally push the slope and the intercept(in this order) into the main stack stack.push(trim_digits(slope, parameters.precision)); diff --git a/src/statistics.h b/src/statistics.h index 1342f1e..7f1f6d4 100644 --- a/src/statistics.h +++ b/src/statistics.h @@ -15,8 +15,8 @@ private: std::optional fn_mean(dc::Stack &stack, const dc::Parameters ¶meters, std::unordered_map ®s); std::optional fn_sdev(dc::Stack &stack, const dc::Parameters ¶meters, std::unordered_map ®s); std::optional fn_lreg(dc::Stack &stack, const dc::Parameters ¶meters, std::unordered_map ®s); - std::optional factorial(const long long n); - std::string trim_digits(double number, unsigned int precision); + static std::optional factorial(long long n); + static std::string trim_digits(double number, unsigned int precision); OPType op_type; };