Advertisement
alfps

Untitled

Aug 25th, 2020
1,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #ifndef _USE_MATH_DEFINES
  2. #   error "Define _USE_MATH_DEFINES in the build settings, please."
  3. #   include <fatal-error>   // Just stop the compiler here.
  4. #endif
  5. #include <math.h>
  6.  
  7. const double pi = M_PI;
  8.  
  9. template< auto unitfunc() -> double >
  10. struct Angle_
  11. {
  12.     double value;
  13.    
  14.     operator double() const { return value; }
  15.    
  16.     static auto unit() -> double { return unitfunc(); }
  17.  
  18.     template< class Other_angle_type >
  19.     auto to_() const
  20.         -> Other_angle_type
  21.     { return {(value / unit()) * Other_angle_type::unit()}; }
  22. };
  23.  
  24. auto degrees_unit() -> double { return 180; }
  25. using Degrees = Angle_<degrees_unit>;
  26.  
  27. auto radians_unit() -> double { return pi; }
  28. using Radians = Angle_<radians_unit>;
  29.  
  30. #include <iomanip>
  31. #include <iostream>
  32. using   std::setprecision,
  33.         std::cout, std::endl, std::fixed;
  34.  
  35. auto main()
  36.     -> int
  37. {
  38.     const Radians r = Degrees{ 180 }.to_<Radians>();
  39.  
  40.     cout << sin( r ) << endl;
  41.     cout << cos( r ) << endl;
  42.     cout << endl;
  43.     cout << fixed << setprecision( 10 );
  44.     cout << sin( r ) << endl;
  45.     cout << cos( r ) << endl;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement