Skip to content

Commit

Permalink
Minor fixes for casting in direct variance calculations.
Browse files Browse the repository at this point in the history
This aims to avoid potential architecture-specific behaviors when multiplying a
float with an integer, by just doing an up-front cast of everything to a float.
  • Loading branch information
LTLA committed Sep 2, 2024
1 parent 2d65c3d commit 1f1c8b7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions include/tatami_stats/variances.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,24 @@ std::pair<Output_, Output_> direct(const Value_* value, Index_ num_nonzero, Inde
::tatami_stats::internal::nanable_ifelse<Value_>(
skip_nan,
[&]() {
for (Index_ i = 0; i < num_nonzero; ++i, ++value) {
auto val = *value;
for (Index_ i = 0; i < num_nonzero; ++i) {
auto val = value[i];
if (!std::isnan(val)) {
var += (val - mean) * (val - mean);
auto delta = static_cast<Output_>(val) - mean;
var += delta * delta;
}
}
},
[&]() {
for (Index_ i = 0; i < num_nonzero; ++i, ++value) {
auto val = *value;
var += (val - mean) * (val - mean);
for (Index_ i = 0; i < num_nonzero; ++i) {
auto delta = static_cast<Output_>(value[i]) - mean;
var += delta * delta;
}
}
);

if (num_nonzero < num_all) {
var += (num_all - num_nonzero) * mean * mean;
var += static_cast<Output_>(num_all - num_nonzero) * mean * mean;
}

if (count == 0) {
Expand Down

0 comments on commit 1f1c8b7

Please sign in to comment.