Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package function.logarithmic;
  2.  
  3. import function.AbstractFunction;
  4.  
  5. import static java.lang.Double.*;
  6.  
  7. /**
  8. * Created by cezar on 3/25/17.
  9. */
  10. public class Ln extends AbstractFunction {
  11.  
  12. public Ln(double accuracy, boolean fromTable) {
  13. super(accuracy, fromTable);
  14. }
  15.  
  16. public Ln(double accuracy) {
  17. super(accuracy);
  18. }
  19.  
  20. public Ln() {
  21. super();
  22. }
  23.  
  24. public double calc(double arg) {
  25. if (isNaN(arg) || arg < 0.0) {
  26. return NaN;
  27. }
  28.  
  29. if (arg == Double.POSITIVE_INFINITY) {
  30. return Double.POSITIVE_INFINITY;
  31. }
  32.  
  33. if (arg == 0.0) {
  34. return Double.NEGATIVE_INFINITY;
  35. }
  36.  
  37. if(fromTable)
  38. return Math.log(arg);
  39. /*
  40. double x = (arg)/(arg - 1);
  41. double previousValue;
  42. double termFactor = 1/x;
  43. double term = 1/x;
  44. double value = term;
  45. int n = 2;
  46. do {
  47. previousValue = value;
  48. term *= termFactor;
  49. value += term / n;
  50. n++;
  51. } while (accuracy <= Math.abs(value - previousValue) && n < MAX_ITERATIONS);
  52. System.out.println(value);
  53. return value;
  54. */
  55. double value = 0;
  56. double preValue;
  57. int n = 1;
  58. int k = 1;
  59. if (Math.abs(arg - 1) <= 1) {
  60. do {
  61. preValue = value;
  62. value -= ((Math.pow(-1, n) * Math.pow(-1 + arg, n)) / n);
  63. n++;
  64. } while (accuracy <= Math.abs(value - preValue) && n < MAX_ITERATIONS);
  65. } else if (Math.abs(arg - 1) > 1) {
  66. do {
  67. preValue = value;
  68. value += calc(arg - 1) - ((Math.pow(-1, n) * Math.pow(-1 + arg, n)) / n);
  69. k++;
  70. } while (accuracy <= Math.abs(value - preValue) && k < MAX_ITERATIONS);
  71. }
  72.  
  73. return value;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement