OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
HelpFunctions.hpp
Go to the documentation of this file.
1 //
2 // Created by jstark on 2019-11-08.
3 //
12 #ifndef REDISTANCING_SUSSMAN_HELPFUNCTIONS_HPP
13 #define REDISTANCING_SUSSMAN_HELPFUNCTIONS_HPP
14 
15 #include <iostream>
16 #include <fstream>
23 template <class T>
24 int sgn(T val)
25 {
26  return (T(0) < val) - (val < T(0));
27 }
46 template <typename T>
47 T smooth_S(T val, T epsilon)
48 {
49  return (val / sqrt(val * val + epsilon * epsilon));
50 }
51 
52 
63 template <class T>
64 bool isApproxEqual(T val1, T val2, T tolerance)
65 {
66  return (val1 <= val2 + tolerance && val1 >= val2 - tolerance);
67 }
68 
75 template <typename T>
76 void append_value_to_textfile(std::string & textfile, T value)
77 {
78  std::ofstream out(textfile);
79  out << value;
80 }
81 
89 template <typename T>
90 std::string to_string_with_precision(const T myValue, const size_t n = 6)
91 {
92  std::ostringstream out;
93  out.precision(n);
94  out << std::fixed << myValue;
95  return out.str();
96 }
97 
98 
99 #endif //REDISTANCING_SUSSMAN_HELPFUNCTIONS_HPP
void append_value_to_textfile(std::string &textfile, T value)
Appends the value of a given variable of any type to a textfile as string.
std::string to_string_with_precision(const T myValue, const size_t n=6)
Converts value into string maintaining a desired precision.
int sgn(T val)
Gets the sign of a variable.
T smooth_S(T val, T epsilon)
Gets the smoothed sign of a variable.
bool isApproxEqual(T val1, T val2, T tolerance)
Checks, if two values are sufficiently close to each other within a given tolerance range,...