Advertisement
Guest User

Exponentiate (decimal) - unworking

a guest
Apr 27th, 2015
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. // http://stackoverflow.com/a/466434/848344
  2. public decimal powVappolinario(decimal x, decimal y)
  3. {
  4.     return DecimalExp(y * LogN(x));
  5. }
  6.  
  7. public decimal Exponentiate(decimal a, decimal b)
  8. {
  9.     decimal total = a;
  10.     for (int i = 1; i < b; i++) total = a*total;
  11.     return total;
  12. }
  13.  
  14. public int Factorial(int n)
  15. {
  16.     int j=1;
  17.     for(int i=1;i<=n;i++){ j = j*i; }
  18.     return j;
  19. }
  20.  
  21. // Adjust this to modify the precision
  22. public const int ITERATIONS = 27;
  23. // power series
  24.  
  25. public decimal DecimalExp(decimal power)
  26. {
  27.     int iteration = ITERATIONS;
  28.     decimal result = 1;
  29.     while (iteration > 0)
  30.     {
  31.         decimal fatorial = Factorial(iteration);
  32.         result += Exponentiate(power, iteration) / fatorial;
  33.         iteration--;
  34.     }
  35.     return result;
  36. }
  37.  
  38. // natural logarithm series
  39. public decimal LogN(decimal number)
  40. {
  41.     decimal aux = (number - 1);
  42.     decimal result = 0;
  43.     int iteration = ITERATIONS;
  44.     while (iteration > 0)
  45.     {
  46.         result += Exponentiate(aux, iteration) / iteration;
  47.         iteration--;
  48.     }
  49.     return result;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement