java bigdecimal third root

Solutions on MaxInterview for java bigdecimal third root by the best coders in the world

showing results for - "java bigdecimal third root"
Bobby
21 Feb 2016
1public static long ITER = 1000;
2
3  public static BigDecimal cuberoot(BigDecimal b) {
4      // Specify a math context with 40 digits of precision.
5
6      MathContext mc = new MathContext(40);
7
8      BigDecimal x = new BigDecimal("1", mc);
9
10      // Search for the cube root via the Newton-Raphson loop. Output each // successive iteration's value.
11
12      for (int i = 0; i < ITER; i++) {
13          x = x.subtract(
14                  x.pow(3, mc)
15                          .subtract(b, mc)
16                          .divide(new BigDecimal("3", mc).multiply(
17                                  x.pow(2, mc), mc), mc), mc);
18      }
19      return x;
20  }