fork download
  1. import java.math.BigDecimal;
  2. import java.math.RoundingMode;
  3.  
  4. class Calculo {
  5.  
  6. private static final BigDecimal CEM = new BigDecimal(100);
  7. private static final BigDecimal DOIS = new BigDecimal(2);
  8.  
  9. private static final int PRECISAO_CALCULO = 21;
  10. private static final int PRECISAO_FINAL = 5;
  11.  
  12. private static final BigDecimal DELTA =
  13. BigDecimal.ONE.divide(BigDecimal.TEN.pow(PRECISAO_FINAL));
  14.  
  15. private static final BigDecimal financiamento(int n, BigDecimal j, BigDecimal p) {
  16. BigDecimal a = BigDecimal.ONE.add(j).pow(n);
  17.  
  18. BigDecimal b = BigDecimal.ONE.divide(
  19. a,
  20. PRECISAO_CALCULO,
  21. RoundingMode.HALF_EVEN);
  22.  
  23. return BigDecimal.ONE
  24. .subtract(b)
  25. .multiply(p)
  26. .divide(j, PRECISAO_CALCULO, RoundingMode.HALF_EVEN);
  27. }
  28.  
  29. private static final BigDecimal taxaFinanciamento(
  30. int n,
  31. {
  32. BigDecimal j1 = DELTA;
  33. BigDecimal q1 = financiamento(n, j1, p);
  34. BigDecimal q2 = financiamento(n, j2, p);
  35. BigDecimal m1 = q1.subtract(q0);
  36. BigDecimal m2 = q2.subtract(q0);
  37. if (m1.abs().compareTo(DELTA) <= 0) return j1;
  38. if (m2.abs().compareTo(DELTA) <= 0) return j2;
  39. for (int i = 0; i < 50; i++) {
  40. if (m1.signum() == m2.signum()) {
  41. throw new ArithmeticException("Fora do intervalo"
  42. + ": q1=[" + q1 + "], j1=[" + j1 + "], m1=[" + m1 + "]"
  43. + ", q2=[" + q2 + "], j2=[" + j2 + "], m2=[" + m2 + "]"
  44. + ", i=" + i);
  45. }
  46. BigDecimal j3 = j1.add(j2).divide(DOIS);
  47. BigDecimal q3 = financiamento(n, j3, p);
  48. BigDecimal m3 = q3.subtract(q0);
  49. if (m3.abs().compareTo(DELTA) <= 0) return j3;
  50. if (m3.signum() == m1.signum()) {
  51. q1 = q3;
  52. j1 = j3;
  53. } else {
  54. q2 = q3;
  55. j2 = j3;
  56. }
  57. }
  58. throw new ArithmeticException("Não convergiu"
  59. + ": q1=[" + q1 + "], j1=[" + j1 + "], m1=[" + m1 + "]"
  60. + ", q2=[" + q2 + "], j2=[" + j2 + "], m2=[" + m2 + "]");
  61. }
  62.  
  63. private static void teste(int q0, int p, int n) {
  64. BigDecimal bdq0Ideal = new BigDecimal(q0);
  65. BigDecimal bdp = new BigDecimal(p);
  66. BigDecimal j = taxaFinanciamento(bdq0Ideal, n, bdp);
  67. BigDecimal bdq0Obtido = financiamento(n, j, bdp);
  68. System.out.println("----------");
  69. System.out.println("q0-ideal=[" + bdq0Ideal + "]");
  70. System.out.println("p=[" + bdp + "]");
  71. System.out.println("j=[" + j + "]");
  72. System.out.println("q0-obtido=[" + bdq0Obtido + "]");
  73. System.out.println("----------");
  74. System.out.println();
  75. }
  76.  
  77. public static void main(String[] args) {
  78. teste(750, 86, 10);
  79. teste(750, 85, 10);
  80. }
  81. }
Success #stdin #stdout 0.1s 2841600KB
stdin
Standard input is empty
stdout
----------
q0-ideal=[750]
p=[86]
j=[0.025690244208984076976776123046875]
q0-obtido=[749.999997639096753068406]
----------

----------
q0-ideal=[750]
p=[85]
j=[0.023429261477030813694000244140625]
q0-obtido=[749.999996056141438963093]
----------