fork download
  1. #include <cstdint>
  2. #include <cmath>
  3.  
  4. #include <iostream>
  5.  
  6. template <int32_t B, int32_t E>
  7. struct Pow {
  8. static int32_t const value = Pow<B,E-1>::value * B;
  9. };
  10.  
  11. template <int32_t B>
  12. struct Pow<B,0> {
  13. static int32_t const value = 1;
  14. };
  15.  
  16. template <int8_t E>
  17. struct AcceptableError {
  18. static double const value = 1.0 / Pow<10,E>::value;
  19. };
  20.  
  21.  
  22. template <typename Error = AcceptableError<1> >
  23. class Double {
  24. private :
  25. double num;
  26.  
  27. public:
  28.  
  29. Double( double value ) : num( value ) { }
  30.  
  31. bool equals ( Double const & rhs ) const {
  32. return fabs (num - rhs.num ) < Error::value;
  33. }
  34. };
  35.  
  36. int main() {
  37.  
  38. using namespace std;
  39.  
  40. Double<> a( 1e-1 ), b( 1e-2 );
  41. cout << boolalpha << a.equals(b) << endl;
  42.  
  43. Double< AcceptableError<8> > c( 1e-1 ), d( 1e-2 );
  44. cout << boolalpha << c.equals(d) << endl;
  45. }
  46.  
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
true
false