Skip to content

Commit

Permalink
fix normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
barne856 committed Jul 18, 2024
1 parent b2e0bba commit 2d60eeb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/squint/linear_algebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif

#include "squint/dimension.hpp"
#include "squint/quantity.hpp"
#include "squint/tensor_base.hpp"
#include <algorithm>
#include <concepts>
Expand Down Expand Up @@ -341,8 +342,7 @@ template <typename Derived, error_checking ErrorChecking> class linear_algebra_m

auto norm() const {
if constexpr (quantitative<typename Derived::value_type>) {
using root_type = root_t<typename Derived::value_type::dimension_type, 2>;
return root_type(std::sqrt(squared_norm()));
return math::sqrt(squared_norm());
} else {
return std::sqrt(squared_norm());
}
Expand Down
14 changes: 14 additions & 0 deletions tests/linalg_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ TEST_CASE("linear_algebra_mixin tests") {
vec4 v{{4, 2, 3, 1}};
CHECK(v.max() == 4);
}

SUBCASE("normalize (quantity type)") {
auto n = normalize(vec3_t<units::length>({units::length(3), units::length(4), units::length(0)}));
CHECK(n[0] == doctest::Approx(0.6F));
CHECK(n[1] == doctest::Approx(0.8F));
CHECK(n[2] == doctest::Approx(0.0F));
}

SUBCASE("normalize (float type)") {
auto n = normalize(vec3_t<float>({3, 4, 0}));
CHECK(n[0] == doctest::Approx(0.6F));
CHECK(n[1] == doctest::Approx(0.8F));
CHECK(n[2] == doctest::Approx(0.0F));
}
}

TEST_CASE("fixed_linear_algebra_mixin tests") {
Expand Down

0 comments on commit 2d60eeb

Please sign in to comment.