mramine364

Math class

Aug 21st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include "Math.h"
  2.  
  3. Math::Math()
  4. {  
  5. }
  6.  
  7. Math::~Math()
  8. {
  9. }
  10.  
  11. double Math::pi = std::acos(-1);
  12.  
  13. int Math::sign(long n){
  14.     return (n < 0) ? -1 : 1;
  15. }
  16. double Math::round(double n, int r){
  17.     double p = pow(10, r);
  18.     return std::round(n*p) / p;
  19. }
  20. double Math::max(vector<double> t){
  21.     double largest = t[0];
  22.     for (int i = 1; i<t.size(); i++)
  23.     {
  24.         double val = t[i];
  25.         largest = (largest>val) ? largest : val;
  26.     }
  27.     return largest;
  28. }
  29. double Math::min(vector<double> t){
  30.     double lowest = t[0];
  31.     for (int i = 1; i<t.size(); i++)
  32.     {
  33.         double val = t[i];
  34.         lowest = (lowest<val) ? lowest : val;
  35.     }
  36.     return lowest;
  37. }
  38. double Math::gcd(int a, int b){
  39.     while (b != 0){
  40.         int r = a%b;
  41.         a = b;
  42.         b = r;
  43.     }
  44.     return a;
  45. }
  46. double Math::lcm(int a, int b){
  47.     return a*b / gcd(a, b);
  48. }
  49.  
  50. int Math::modpow(int a, int b, int c){
  51.     if (b == 0)
  52.         return 1 % c;
  53.     if (b == a)
  54.         return a % c;
  55.     int r = modpow(a, b / 2, c);
  56.     return r*r*((b % 2 == 1) ? a : 1) % c;
  57. }
  58. int Math::rand(int min, int max){
  59.     mt19937 rng;
  60.     rng.seed(random_device()());
  61.     uniform_int_distribution<mt19937::result_type> dist1(min, max);
  62.     double ra = std::rand();
  63.     return dist1(rng);
  64. }
  65. long Math::factorial(int n){
  66.     long p = 1;
  67.     for (int i = 2; i <= n; i++){
  68.         p *= i;
  69.     }
  70.     return p;
  71. }
Add Comment
Please, Sign In to add comment