waltermilner

Untitled

Jul 29th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. public class Test {
  2.   /*
  3.   The idea is to write a version of Math.pow(x,y), to find x^y, without
  4.   using any library functions.
  5.   If y is a positive integer, we just do a loop x X x X x.. y times.
  6.   But if y is not an integer?
  7.   We do exp( y log x )
  8.   So we need to write a log function and an exponential
  9.  
  10.   */
  11.  
  12.   static double abs(double x)
  13.   { // version of Maths.abs
  14.     return x<0?-x:x;
  15.   }
  16.  
  17.   static double log(double xx) {
  18.     final double E = 2.7182818284590452353602874713527; // Euler's constant, e
  19.     if (xx < 0) { // no log negative number
  20.       throw new IllegalArgumentException();
  21.     }
  22.     double nn=0; // see later
  23.     if (xx > 1) { // use Maclaurin series for log(1+x)
  24.       while (xx>E) // remove powers of e
  25.       {
  26.         xx/=E;
  27.         nn++; // counting them - we add in nn at the end
  28.       } // now xx is from 0 to 2.71...
  29.       if (xx>2) // power series really bad for 2 to 2.71 so
  30.       {
  31.         nn+=0.69314718; // this is log 2
  32.         xx/=2; // now xx is max 1.359
  33.       }
  34.       double x = xx - 1; // set up for power series
  35.       double result = 0;
  36.       int n = 1;
  37.       double pow = -1;
  38.       double term;
  39.       do {
  40.         pow *= -x;
  41.         term = pow / n;
  42.         n++;
  43.         result += term;
  44.  
  45.       } while (abs(term)>1e-6);
  46.       return result+nn;
  47.     }
  48.     if (xx == 1) {
  49.       return 0;
  50.     }
  51.     // use Maclaurin series log(1-x)
  52.     double x = 1 - xx;
  53.     double result = 0;
  54.     int n = 1;
  55.     double pow = 1;
  56.     double term;
  57.     do {
  58.       pow *= x;
  59.       term = pow / n;
  60.       n++;
  61.       result -= term;
  62.  
  63.     } while (n < 500);
  64.     return result;
  65.   }
  66.  
  67.   static double exp(double x)
  68.   { // standard power series for e^x
  69.     double pow=1;
  70.     double n=1;
  71.     double result=1;
  72.     double div=1;
  73.     do
  74.     {
  75.       pow*=x;
  76.       div*=n;
  77.       result+=pow/div;
  78.       n++;
  79.      
  80.     } while (n<50);
  81.     return result;
  82.   }
  83.  
  84.   static double pow(double x, double y)
  85.   {
  86.     return exp(y*log(x));
  87.   }
  88.  
  89.   public static void main(String[] d) {
  90.    
  91.     // test log
  92.     // System.out.println(log(-2)); //exception
  93.     System.out.println(log(2.1)); //0.7419373466666667
  94.     System.out.println(log(3)); //1.0986122718454672
  95.     System.out.println(log(1)); //0.0
  96.     System.out.println(log(0.5)); //-0.6931471805599451
  97.     System.out.println(log(0.1)); //-2.302585092994046
  98.     // test exp
  99.     System.out.println(exp(2)); //7.389056098930649
  100.     System.out.println(exp(5)); //148.41315910257657
  101.     System.out.println(exp(1)); //2.7182818284590455
  102.     System.out.println(exp(0.5)); //1.6487212707001278
  103.     System.out.println(exp(0)); // 1.0
  104.     System.out.println(exp(-1)); //0.36787944117144245
  105.     // test pow
  106.    
  107.     System.out.println(pow(2,2)); //3.9999960000044585
  108.     System.out.println(pow(2,3)); //7.999988000016374
  109.     System.out.println(pow(4,-1)); //0.24999993536993692
  110.     System.out.println(pow(3,2.1)); //10.04510821143608
  111.  
  112.   }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment