Skip to content

Commit 413ef1e

Browse files
Merge pull request #38 from drubinstein/drubinstein/typed-sum
Vector sum should initialize with vector type
2 parents 50dd9e6 + e1c8acf commit 413ef1e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

include/cotila/vector/math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ constexpr T dot(const vector<T, N> &a, const vector<T, N> &b) {
9797
* Computes the sum of the elements of a vector.
9898
*/
9999
template <typename T, std::size_t N> constexpr T sum(const vector<T, N> &v) {
100-
return accumulate(v, 0, std::plus<T>());
100+
return accumulate(v, static_cast<T>(0), std::plus<T>());
101101
}
102102

103103
/** @brief computes the minimum valued element

test/vector_test.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#define COTILA_VECTOR_TEST_H_
33

44
#include <complex>
5+
#include <string>
56
#include <cotila/cotila.h>
67

7-
namespace cotila {
8-
namespace test {
8+
namespace cotila::test {
99

1010
static_assert(make_vector(1, 2, 3) == vector{1, 2, 3},
1111
"make_vector and uniform initialization deduction guide");
@@ -85,7 +85,11 @@ static_assert(real(vector{{{-1., 2.}, {1., -2.}}}) == vector{-1., 1.}, "real");
8585

8686
static_assert(imag(vector{{{-1., 2.}, {1., -2.}}}) == vector{2., -2.}, "imag");
8787

88-
} // namespace test
89-
} // namespace cotila
88+
static_assert(sum(vector{1,2,3}) == 6, "vector sum of integers");
89+
90+
static_assert(abs(sum(vector{0.1,0.2,0.3}) - 0.6) < 1e-5, "vector sum of floating");
91+
92+
93+
} // namespace cotila::test
9094

9195
#endif // COTILA_VECTOR_TEST_H_

0 commit comments

Comments
 (0)