java - Why is the square root calculation slower for larger numbers? -


this java code ask number printing square root without using math.sqrt() method:

import java.util.random; import java.io.*; public class square {     public static void main(string[] args) throws ioexception {         final double tol = 0.5e-15;         inputstreamreader reader = new inputstreamreader(system.in);         bufferedreader input = new bufferedreader(reader);         system.out.print("enter number aquare of it: ");         double n = new double(input.readline()).doublevalue();         random random = new random();         double x = random.nextdouble();         {             x = (x+n/x)/2;         } while(math.abs(x*x-n)>tol*2*x);         system.out.println("sqrt(" + n + ") = " + x);     }  } 

please run in computer , test several numbers. numbers below 30.1, runs , calculates square root quickly. when enter 30.2 or larger numbers, no square root calculated(at least feasible waitings)! interesting explanation behavior?!

what have there so-called babylonian method computing square root of number. however, think stop condition should scalar (for example tol) , not tol*2*x.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -