# Thread: Prob with an exercise

1. Member Join Date
Oct 2007
Posts
9
Rep Power
0

## Prob with an exercise

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:

Java 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.

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){
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("--------------------------------------");
}
}
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: ");
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.
Last edited by JavaBean; 10-30-2007 at 09:11 PM. Reason: Code placed inside [code] tag.  Reply With Quote

2. ## Windows does not count/report every milliseconds to you. There is a granularity in reporting which depends on the underlying OS.

If it reports the same number, that means your procedure does not run long enough to capture the difference. You can try your code with a dummy for loop which took more time..

Bring Java's System.currentTimeMillis() Back into the Fold for Transaction Monitoring  Reply With Quote

3. Member Join Date
Oct 2007
Posts
9
Rep Power
0

## I have to say that I followed the form that my professor has given into powerpoint slides and it has to work cause his produces results.Now if I understand correctly you are implying that if I use it on Linux it should work or at least has better chances than windows???

PS:If it isn't what you are saying please forgive my noobity,I only recently began this course.  Reply With Quote

4. ## I saw the same problem on Windows before but i did not test it on Linux. That tutorial tells this:

"The resolution or granularity of a clock is the smallest time difference that can be measured between two consecutive calls. For example, on many Microsoft Windows systems, the resolution of the call to currentTimeMillis is 10 ms. This is because the native implementation makes an operating-system call that has a resolution of 10 ms. Most other operating systems claim to have a resolution of 1 ms."

So you look like to have a better chance.

But you should check your method for inputs which may take more time to compute. Your prof. may use inputs which take more time! (Note that i am not thinking about newton method now or checking your code. Based on your input, i told you my quick informed guesses.)  Reply With Quote

5. Member Join Date
Feb 2008
Posts
2
Rep Power
0

## Hey all...new to Java

ooops wrong forum...  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•