Java Wurzel aus großer Dezimalzahl - BigDecimal

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Java Wurzel aus großer Dezimalzahl - BigDecimal

    Quellcode

    1. import java.math.*;
    2. /**
    3. *
    4. * @author Torben Brodt
    5. * @version 1.0
    6. *
    7. * <p />Zieht die Wurzel aus einer unendlich großen Zahl
    8. */
    9. public class A2_sqrt_BigInt {
    10. /**
    11. * @param args
    12. */
    13. public static void main(String[] args) {
    14. // TODO Auto-generated method stub
    15. BigDecimal x = new BigDecimal("1234567891011121314151617181920");
    16. BigDecimal uG = new BigDecimal("0");
    17. BigDecimal oG = x.add(new BigDecimal(1));
    18. BigDecimal w = new BigDecimal("0");
    19. BigDecimal epsilon = new BigDecimal("0.0000001");
    20. do {
    21. w = uG.add(oG).divide(new BigDecimal(2));
    22. if(w.multiply(w).compareTo(x) > 0)
    23. oG = w;
    24. else
    25. uG = w;
    26. } while(oG.subtract(uG).compareTo(epsilon) > 0);
    27. System.out.println("Wurzel: "+w.setScale(7,BigDecimal.ROUND_HALF_UP));
    28. }
    29. }
    Alles anzeigen