Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The release run heads these entries with the version and opens a fresh

## Unreleased

- Spreadsheet recalculation recognizes STDEV.S, STDEV.P, VAR.S, and VAR.P,
including their Excel and LibreOffice compatibility prefixes.

- PDF streams support TIFF predictor samples packed at 1, 2, or 4 bits per
component. LZW decoding rejects invalid width parameters and dictionary
growth beyond the format's limit.
Expand Down
21 changes: 9 additions & 12 deletions src/odr/internal/formula/formula_functions_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ template <bool largest> Value kth(const Call &call) {

/// The variance of the numbers, of a sample where @p sample. LibreOffice
/// takes the mean with `KahanSum` and each deviation with `approxSub`.
Value variance_of(const Call &call, const bool sample, const bool root) {
template <bool sample, bool root> Value variance_of(const Call &call) {
const Collected collected = collect(call);
if (collected.error.has_value()) {
return Value{*collected.error};
Expand Down Expand Up @@ -662,23 +662,20 @@ constexpr std::array entries{
FunctionEntry{"SINH", unary<hyperbolic_sine>},
FunctionEntry{"SMALL", kth<false>},
FunctionEntry{"SQRT", unary<square_root>},
FunctionEntry{
"STDEV",
[](const Call &call) { return variance_of(call, true, true); }},
FunctionEntry{
"STDEVP",
[](const Call &call) { return variance_of(call, false, true); }},
FunctionEntry{"STDEV", variance_of<true, true>},
FunctionEntry{"STDEV.P", variance_of<false, true>},
FunctionEntry{"STDEV.S", variance_of<true, true>},
FunctionEntry{"STDEVP", variance_of<false, true>},
FunctionEntry{"SUM", sum},
FunctionEntry{"SUMPRODUCT", sum_product},
FunctionEntry{"SUMSQ", sum_of_squares},
FunctionEntry{"TAN", unary<tangent>},
FunctionEntry{"TANH", unary<hyperbolic_tangent>},
FunctionEntry{"TRUNC", binary<round_down, 0, true>},
FunctionEntry{
"VAR", [](const Call &call) { return variance_of(call, true, false); }},
FunctionEntry{
"VARP",
[](const Call &call) { return variance_of(call, false, false); }},
FunctionEntry{"VAR", variance_of<true, false>},
FunctionEntry{"VAR.P", variance_of<false, false>},
FunctionEntry{"VAR.S", variance_of<true, false>},
FunctionEntry{"VARP", variance_of<false, false>},
};

} // namespace
Expand Down
17 changes: 17 additions & 0 deletions test/src/internal/formula/formula_functions_math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#include <gtest/gtest.h>

#include <array>
#include <clocale>
#include <cmath>
#include <numbers>
#include <optional>
#include <string>
#include <utility>

using namespace odr::internal::formula;
using namespace odr::test::formula;
Expand Down Expand Up @@ -231,3 +233,18 @@ TEST(FormulaFunctionsMath, the_blank_cells_of_a_whole_column_have_no_answer) {
// the grid counts the cells past the extent of the sheet
EXPECT_EQ(xlsx("=COUNTBLANK(A:A)"), std::nullopt);
}

TEST(FormulaFunctionsMath, modern_statistics_use_the_existing_implementations) {
const std::array names{std::pair{"STDEV.S", "STDEV"},
std::pair{"STDEV.P", "STDEVP"},
std::pair{"VAR.S", "VAR"}, std::pair{"VAR.P", "VARP"}};
for (const auto &[modern, legacy] : names) {
SCOPED_TRACE(modern);
EXPECT_EQ(xlsx(std::string("=") + modern + "(2,4,6)"),
xlsx(std::string("=") + legacy + "(2,4,6)"));
EXPECT_EQ(xlsx(std::string("=_xlfn.") + modern + "(A1:A6)"),
xlsx(std::string("=") + legacy + "(A1:A6)"));
EXPECT_EQ(ods(std::string("=COM.MICROSOFT.") + modern + "(2;4;6)"),
ods(std::string("=") + legacy + "(2;4;6)"));
}
}
Loading