Hi,I'm having a very strange problem.I have written code for an exercise that calculates the square root of a number either by asking the user for input or by giving it as argument when running the prog using Newtons method(a method I must create) and Math.sqrt and printing the results to see if they are the same.That is done.The next exercise is to tamper with the code and add a method that fills an array X(declared on the top) random integers so that another method I have to create will again use Math.sqrt and Newton so that it can measure the milliseconds in execution and print which method is faster.Only when I use start=System.currentTimeMillis(),the value stored in start is like this:1.193772081281E12 and is the same in both start and end variables even if i have a for() in between.And as a result it gives 0.00 ms.
Here is my code:
package sroot2;
import java.io.*;
import java.util.Random;
public class SRoot2 {
public static int i;
public static Random random = new Random();
public final static int X=1000;
public static double[] numbers=new double[X];
public static double startNewton=0, endNewton=0, startMath=0, endMath=0, timeNewton=0, timeMath=0;
//Here we declare the two variables that will help us get the input from the user
//and parse it into an integer.
static public InputStreamReader input = new InputStreamReader(System.in);
static public BufferedReader keyboardInput = new BufferedReader(input);
public static void main(String[] args) throws IOException {
//If the string array is empty(meaning that no arguments were given upon launch of the program),
//the inputdatainteger variable calls the readline function to get an input from the user and
//return an integer value.Then the program runs the same way as if args wasn't empty but only for
//one value at a time.Also the program asks repeatedly for more inputs from the user until the latter
//gives 0 for input.
if(args.length==0){
double inputdatainteger=readLine();
while(inputdatainteger!=0){
double NewtonResUs=Newtonsqrt(inputdatainteger,1);
double MathResUs=Math.sqrt(inputdatainteger);
double VariationUs=Math.abs(MathResUs-NewtonResUs);
System.out.println("Number:"+Math.round(inputdatainteger));
System.out.println("SRoot(Newton):"+Math.round(NewtonResUs));
System.out.println("SRoot(Java Math):"+Math.round(MathResUs));
System.out.println("Variation:"+Math.round(VariationUs));
System.out.println("--------------------------------------");
inputdatainteger=readLine();
}
}
else{
//If the string array is not empty it runs as SRoot.
double[] argumentsdouble = new double[args.length];
double[] NewtonResults= new double[args.length];
double[] MathResults= new double[ args.length];
double[] Variation= new double[args.length];
for(i=0;i<args.length;i++){
argumentsdouble[i] = Double.parseDouble(args[i]);
if(argumentsdouble[i]>0){
MathResults[i]=Math.sqrt(argumentsdouble[i]);
NewtonResults[i]=Newtonsqrt(argumentsdouble[i],1);
Variation[i]=Math.abs(MathResults[i]-NewtonResults[i]);
}
else{
NewtonResults[i]=MathResults[i]=-1;
}
}
for(i=0;i<argumentsdouble.length;i++){
System.out.println("Number:"+Math.round(argumentsdouble[i]));
if(argumentsdouble[i]<0){
System.out.println (argumentsdouble[i]+" ERROR: Non-positive number");
System.out.println("--------------------------------------");
}
else{
System.out.println("SRoot(Newton):"+Math.round(NewtonResults[i]));
System.out.println("SRoot(Java Math):"+Math.round(MathResults[i]));
System.out.println("Variation:"+Math.round(Variation[i]));
System.out.println("--------------------------------------");
}
}
}
fillRandom(numbers);
executionTimeStatistics(numbers);
}
//The function that calculates the square root of a given number using Newton's method.
//It runs by using the guess and the number recursively until the difference between the
//x and newx is smaller than the threshold.Which means that we have more or less found the square root.
public static double Newtonsqrt(double B,double x){
double newX;
double threshold = 0.1;
newX = 0.5 * (x + B / x);
if (Math.abs(x - newX) > threshold) {
newX = Newtonsqrt(B, newX);
}
return newX;
}
//This function reads the input from the keyboard and parses it(as it is a string) into an integer
//before returning it for storage.
public static double readLine() throws IOException{
System.out.print("Input an integer: ");
String ss = keyboardInput.readLine();
double inputdatainteger = Double.parseDouble(ss);
return inputdatainteger;
}
public static void fillRandom(double[] numbers){
int rn=Math.abs(random.nextInt(300));
for(i=0;i<X;i++){
numbers[i]=(double)rn;
rn=Math.abs(random.nextInt(300));
}
}
public static void executionTimeStatistics(double[] numbers){
startNewton = System.currentTimeMillis();
for(i=0;i<X;i++){
Newtonsqrt(numbers[i],1);
}
endNewton = System.currentTimeMillis();
System.out.println(startNewton+" "+endNewton);
//timeNewton=endNewton-startNewton;
startMath = System.currentTimeMillis();
for(i=0;i<X;i++){
Math.sqrt(numbers[i]);
}
endMath = System.currentTimeMillis();
timeMath=endMath-startMath;
System.out.print("Statistics for "+X+" computations.\nExecution time using Newton: "+timeNewton+" ms.\nExecution time using Math: "+timeMath+" ms.\n");
if(timeMath<timeNewton){
System.out.print("The winner is Java.Math.\n");
}
else if(timeMath==timeNewton){
System.out.print("The winner is ................ none.This is a draw.\n");
}
else{
System.out.print("The winner is Newton.\n");
}
}
}
PS:sorry if some things are a little sloppy,I'm trying several things to debug this.