Skip to content

Commit

Permalink
Merge pull request #4863 from PDoakORNL/consistent_0_output_tinyvector
Browse files Browse the repository at this point in the history
[WIP] ReferencePoints report -0.0 is equivalent to 0.0 output either as 0.
  • Loading branch information
ye-luo committed Dec 9, 2023
2 parents 53cc40b + de92736 commit 0017202
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Containers/OhmmsPETE/TinyVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@

// include files
#include <iomanip>
#include <cmath>
#include "PETE/PETE.h"
#include "OhmmsPETE/OhmmsTinyMeta.h"
#include "type_traits/complex_help.hpp"

namespace qmcplusplus
{
Expand Down Expand Up @@ -113,7 +115,7 @@ struct TinyVector
inline int size() const { return D; }

inline TinyVector& operator=(const TinyVector& rhs) = default;
inline TinyVector& operator=(TinyVector&& rhs) = default;
inline TinyVector& operator=(TinyVector&& rhs) = default;

template<class T1>
inline TinyVector<T, D>& operator=(const TinyVector<T1, D>& rhs)
Expand Down Expand Up @@ -218,14 +220,26 @@ template<class T>
struct printTinyVector
{};

// specialized for Vector<TinyVector<T,D> >
/** template functor for Vector<TinyVector<T,D> streamn output
* 0 equivalent values are output as 0.
* In the case of complex or integer type T's this can be added if needed.
* But now you don't pay for this if unless T is actually real.
*/
template<class T, unsigned D>
struct printTinyVector<TinyVector<T, D>>
{
inline static void print(std::ostream& os, const TinyVector<T, D>& r)
{
for (int d = 0; d < D; d++)
os << std::setw(18) << std::setprecision(10) << r[d];
if constexpr(IsComplex_t<T>::value)
os << std::setw(18) << std::setprecision(10) << r[d];
else if constexpr(IsReal_t<T>::value)
if (FP_ZERO == std::fpclassify(r[d]))
os << std::setw(18) << std::setprecision(10) << 0;
else
os << std::setw(18) << std::setprecision(10) << r[d];
else
os << r[d];
}
};

Expand Down
10 changes: 10 additions & 0 deletions src/Containers/OhmmsPETE/tests/test_TinyVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <stdio.h>
#include <string>
#include <iostream>

using std::string;

Expand Down Expand Up @@ -82,4 +83,13 @@ TEST_CASE("tiny vector", "[OhmmsPETE]")
test_tiny_vector_size_two<4>();
}

TEST_CASE("tiny vector operator out", "[OhmmsPETE]")
{
TinyVector<double, 3> point{0.0, -0.0, 1.0};
std::ostringstream ostr;
ostr << point;
std::string expected{" 0 0 1"};
CHECK(expected == ostr.str());
}

} // namespace qmcplusplus
5 changes: 5 additions & 0 deletions src/type_traits/complex_help.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ struct IsComplex_t<std::complex<T>> : public std::true_type

template<typename T>
using IsComplex = std::enable_if_t<IsComplex_t<T>::value, bool>;

// for symmetry with IsComplex_t
template<typename T>
using IsReal_t = std::is_floating_point<T>;

template<typename T>
using IsReal = std::enable_if_t<std::is_floating_point<T>::value, bool>;

Expand Down

0 comments on commit 0017202

Please sign in to comment.