Jump to content

Angles (geometric), normalization and conversion: Difference between revisions

(C++ entry)
(→‎{{header|C++}}: Boost used)
Line 404:
 
=={{header|C++}}==
{{libheader|Boost}}
<lang cpp>#include <functional>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <sstream>
#include <vector>
#include <boost/algorithm/string.hpp>
 
template<typename T, const int P>
T normalize(T a, double b) { return std::fmod(a, Pb); }
 
inline double d2d(double a) { return normalize<double, 360>(a, 360); }
inline double g2g(double a) { return normalize<double, 400>(a, 400); }
inline double m2m(double a) { return normalize<double, 6400>(a, 6400); }
inline double r2r(double a) { return d2dnormalize<double>(a, 2* 180 / M_PI) * M_PI / 180; }
 
double d2g(double a) { return g2g(a * 10 / 9); }
Line 433 ⟶ 436:
void print(const std::vector<double> &values, const char *s, const char *unit, std::function<double(double)> f) {
using namespace std;
ostringstream out;
coutout << "┌─────────────────┬─────────────┬──────────────┐\n";
coutout << "│ value │ unit(s) │ " << setw(12) << s << " │\n";
coutout << "├─────────────────┼─────────────┼──────────────┤\n";
for (double i : values) {
coutout << "│ " << setw(15);
coutout << fixed << i << defaultfloat << " │ " << setw(12) << unit << "| " << setw(12) << fixed << f(i);
if (i == 0.)
cout << "0 "out << defaultfloat << " │ " << setw(12) << unit << "| " << setw(12) << "0 \n";
else
cout << fixed << i << defaultfloat << " │ " << setw(12) << unit << "| " << setw(12) << fixed << f(i);
cout << defaultfloat << " │\n";
}
coutout << "└─────────────────┴─────────────┴──────────────┘\n";
auto str = out.str();
boost::algorithm::replace_all(str, ".000000", " ");
cout << str;
}
 
Line 476 ⟶ 480:
│ value │ unit(s) │ normalized │
├─────────────────┼─────────────┼──────────────┤
│ -2.000000 │ deg | -2.000000
│ -1.000000 │ deg | -1.000000
│ 0 │ deg | 0 │
│ 1.000000 │ deg | 1.000000
│ 2.000000 │ deg | 2.000000
│ 6.283185 │ deg | 6.283185 │
│ 16.000000 │ deg | 16.000000
│ 57.295780 │ deg | 57.295780 │
│ 359.000000 │ deg | 359.000000
│ 399.000000 │ deg | 39.000000
│ 6399.000000 │ deg | 279.000000
│ 1000000.000000 │ deg | 280.000000
└─────────────────┴─────────────┴──────────────┘
...
Line 493 ⟶ 497:
│ value │ unit(s) │ converted │
├─────────────────┼─────────────┼──────────────┤
│ -2.000000 │ rad -> mil | -2037.183272 │
│ -1.000000 │ rad -> mil | -1018.591636 │
│ 0 │ rad -> mil | 0 │
│ 1.000000 │ rad -> mil | 1018.591636 │
│ 2.000000 │ rad -> mil | 2037.183272 │
│ 6.283185 │ rad -> mil | 6399.999993 │
│ 16.000000 │ rad -> mil | 3497.466173 │
│ 57.295780 │ rad -> mil | 761.001765 │
│ 359.000000 │ rad -> mil | 874.397248 │
│ 399.000000 │ rad -> mil | 3218.062679 │
│ 6399.000000 │ rad -> mil | 2767.877408 │
│ 1000000.000000 │ rad -> mil | 6035.788130 │
└─────────────────┴─────────────┴──────────────┘</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.