Java BigDecimal Square Root sqrt(BigDecimal value)

Here you can find the source of sqrt(BigDecimal value)

Description

sqrt

License

Open Source License

Declaration

public static BigDecimal sqrt(BigDecimal value) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;

import java.util.Vector;

public class Main {
    public static BigDecimal sqrt(BigDecimal value) {

        double val = Math.sqrt(value.doubleValue());

        if (Double.isNaN(val) || Double.isInfinite(val))
            return value;

        BigDecimal x = new BigDecimal(val);
        return x.add(new BigDecimal(value.subtract(x.multiply(x)).doubleValue() / (x.doubleValue() * 2.0)));
    }/*from  w  w w  . ja v a2s . c  o m*/

    /***                         ***/

    // TODO: write vector class that incorporates most vector operations
    public static Vector<BigDecimal> add(Vector<BigDecimal> a, Vector<BigDecimal> b) {
        int m = a.size();
        Vector<BigDecimal> c = new Vector(m);
        for (int i = 0; i < m; i++) {
            BigDecimal value = ((BigDecimal) a.elementAt(i)).add((BigDecimal) b.elementAt(i));
            c.add(value);
        }
        return c;
    }

    public static Vector<BigDecimal> subtract(Vector<BigDecimal> a, Vector<BigDecimal> b) {
        int m = a.size();

        Vector<BigDecimal> c = new Vector(m);

        for (int i = 0; i < m; i++) {
            BigDecimal value = ((BigDecimal) a.elementAt(i)).subtract((BigDecimal) b.elementAt(i));
            c.add(value);
        }
        return c;
    }
}

Related

  1. sqrt(BigDecimal number)
  2. sqrt(BigDecimal number, RoundingMode rounding)
  3. sqrt(BigDecimal original, int scale)
  4. sqrt(BigDecimal randicand)
  5. sqrt(BigDecimal value, int decimalPlaces)
  6. sqrt(BigDecimal value, MathContext mc)
  7. sqrt(BigDecimal x)
  8. sqrt(final ArrayList data)