funkemunky

Test Hypot and Sqrt

Dec 24th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. public static void main(String[] args) {
  2. System.out.println("Starting...");
  3. long start = System.nanoTime();
  4. double hypot = 0;
  5.  
  6. hypot = hypotBasic(2, 2);
  7.  
  8. long end = System.nanoTime() - start;
  9.  
  10. System.out.println("Completed (" + (end / 1E6D) + "ms) (hypot=" + hypot + ")!");
  11. }
  12.  
  13. //Advanced hypot
  14. private static double hypot(double... array) {
  15. double total = 0;
  16. for (double v : array) {
  17. total+= v * v;
  18. }
  19. return sqrt(total);
  20. }
  21.  
  22. //Basic hyopt
  23. private static double hypotBasic(double one, double two) {
  24. return sqrt(one * one + two * two);
  25. }
  26.  
  27. public static double sqrt(double number) {
  28. if(number == 0) return 0;
  29. double t;
  30.  
  31. double squareRoot = number / 2;
  32.  
  33. do {
  34. t = squareRoot;
  35. squareRoot = (t + (number / t)) / 2;
  36. } while ((t - squareRoot) != 0);
  37.  
  38. return squareRoot;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment