Angles (geometric), normalization and conversion: Difference between revisions
Content added Content deleted
(C++ entry) |
(→{{header|C++}}: Boost used) |
||
Line 404: | Line 404: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|Boost}} |
|||
<lang cpp>#include <functional> |
<lang cpp>#include <functional> |
||
#include <iostream> |
#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
#include <math.h> |
#include <math.h> |
||
#include <sstream> |
|||
#include <vector> |
#include <vector> |
||
#include <boost/algorithm/string.hpp> |
|||
template<typename T |
template<typename T> |
||
T normalize(T a) { return std::fmod(a, |
T normalize(T a, double b) { return std::fmod(a, b); } |
||
inline double d2d(double a) { return normalize<double |
inline double d2d(double a) { return normalize<double>(a, 360); } |
||
inline double g2g(double a) { return normalize<double |
inline double g2g(double a) { return normalize<double>(a, 400); } |
||
inline double m2m(double a) { return normalize<double |
inline double m2m(double a) { return normalize<double>(a, 6400); } |
||
inline double r2r(double a) { return |
inline double r2r(double a) { return normalize<double>(a, 2*M_PI); } |
||
double d2g(double a) { return g2g(a * 10 / 9); } |
double d2g(double a) { return g2g(a * 10 / 9); } |
||
Line 433: | Line 436: | ||
void print(const std::vector<double> &values, const char *s, const char *unit, std::function<double(double)> f) { |
void print(const std::vector<double> &values, const char *s, const char *unit, std::function<double(double)> f) { |
||
using namespace std; |
using namespace std; |
||
ostringstream out; |
|||
out << "┌─────────────────┬─────────────┬──────────────┐\n"; |
|||
out << "│ value │ unit(s) │ " << setw(12) << s << " │\n"; |
|||
out << "├─────────────────┼─────────────┼──────────────┤\n"; |
|||
for (double i : values) { |
for (double i : values) { |
||
out << "│ " << setw(15); |
|||
⚫ | |||
if (i == 0.) |
|||
out << defaultfloat << " │\n"; |
|||
else |
|||
⚫ | |||
cout << defaultfloat << " │\n"; |
|||
} |
} |
||
out << "└─────────────────┴─────────────┴──────────────┘\n"; |
|||
auto str = out.str(); |
|||
boost::algorithm::replace_all(str, ".000000", " "); |
|||
cout << str; |
|||
} |
} |
||
Line 476: | Line 480: | ||
│ value │ unit(s) │ normalized │ |
│ value │ unit(s) │ normalized │ |
||
├─────────────────┼─────────────┼──────────────┤ |
├─────────────────┼─────────────┼──────────────┤ |
||
│ -2 |
│ -2 │ deg | -2 │ |
||
│ -1 |
│ -1 │ deg | -1 │ |
||
│ 0 │ deg | 0 │ |
│ 0 │ deg | 0 │ |
||
│ 1 |
│ 1 │ deg | 1 │ |
||
│ 2 |
│ 2 │ deg | 2 │ |
||
│ 6.283185 │ deg | 6.283185 │ |
│ 6.283185 │ deg | 6.283185 │ |
||
│ 16 |
│ 16 │ deg | 16 │ |
||
│ 57.295780 │ deg | 57.295780 │ |
│ 57.295780 │ deg | 57.295780 │ |
||
│ 359 |
│ 359 │ deg | 359 │ |
||
│ 399 |
│ 399 │ deg | 39 │ |
||
│ 6399 |
│ 6399 │ deg | 279 │ |
||
│ 1000000 |
│ 1000000 │ deg | 280 │ |
||
└─────────────────┴─────────────┴──────────────┘ |
└─────────────────┴─────────────┴──────────────┘ |
||
... |
... |
||
Line 493: | Line 497: | ||
│ value │ unit(s) │ converted │ |
│ value │ unit(s) │ converted │ |
||
├─────────────────┼─────────────┼──────────────┤ |
├─────────────────┼─────────────┼──────────────┤ |
||
│ -2 |
│ -2 │ rad -> mil | -2037.183272 │ |
||
│ -1 |
│ -1 │ rad -> mil | -1018.591636 │ |
||
│ 0 │ rad -> mil | 0 │ |
│ 0 │ rad -> mil | 0 │ |
||
│ 1 |
│ 1 │ rad -> mil | 1018.591636 │ |
||
│ 2 |
│ 2 │ rad -> mil | 2037.183272 │ |
||
│ 6.283185 │ rad -> mil | 6399.999993 │ |
│ 6.283185 │ rad -> mil | 6399.999993 │ |
||
│ 16 |
│ 16 │ rad -> mil | 3497.466173 │ |
||
│ 57.295780 │ rad -> mil | 761.001765 │ |
│ 57.295780 │ rad -> mil | 761.001765 │ |
||
│ 359 |
│ 359 │ rad -> mil | 874.397248 │ |
||
│ 399 |
│ 399 │ rad -> mil | 3218.062679 │ |
||
│ 6399 |
│ 6399 │ rad -> mil | 2767.877408 │ |
||
│ 1000000 |
│ 1000000 │ rad -> mil | 6035.788130 │ |
||
└─────────────────┴─────────────┴──────────────┘</pre> |
└─────────────────┴─────────────┴──────────────┘</pre> |
||