Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# squaring

Greenhorn
Posts: 5
How do I get the square root of a number? I put:

int x;
double discriminant, result;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter value of x: ");
x = scan.nextInt();
discriminant = Math.pow(x, 2);
result = (Math.sqrt(discriminant));
System.out.print ("Result is: " + result);

Sheriff
Posts: 11343
That works for me. Is it the formatting of the (double) result you don't like?

Katrina Cobb
Greenhorn
Posts: 5
It won't work for me. When I put in a value for x, it prints out the same value instead of giving me the square root. I took away the Math.pow method and just used square root method and it worked. Which is fine, but I'm afraid when I put the entire formula in (square root of (7x^4 + 5x^3 + 3x^2 + x), it isn't going to compile the right way. I'm told I have to use the power method. So I'm kinda stumped.

author and iconoclast
Sheriff
Posts: 24217
38
I'm not sure why you're stumped; the program does exactly what it should. "x" is some number. "discriminant" is the square of x. "result" is the square root of discriminant which is, within the computation precision, equal to x. So the program should print the same number you type in. If you take out the Math.pow(), then you don't compute the square, so you print the root of x.

Hey, what was that sound I heard? Sounded kinda like somebody smacking their forehead!

Katrina Cobb
Greenhorn
Posts: 5
ok.... I suppose I do deserve a smack on the head

Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38

Originally posted by Katrina Cobb:
ok.... I suppose I do deserve a smack on the head

But anyway: you will want to use pow() to compute those squares. Remember that sqrt(a^2 + b^2) is not equal to a + b. So when you start computing more complicated things, you won't just end up with the same numbers you put in!

Ranch Hand
Posts: 1970

Originally posted by Ernest Friedman-Hill:
But anyway: you will want to use pow() to compute those squares.

As mentioned in another e-mail, pow() is not a good way to compute small integer powers (e.g. 2, 3, 4 ...) of numbers. The pow() function uses a complicated algorithm that is applicable to any power. But small integer powers can be computed much more simply, and more accurately, simply by multiplication.

For instance, the square of x is best written x*x and the cube is best written x*x*x etc.

The above is true for floating-point (float, double) and integer (int, long...) maths, but it is especially true for integer maths. That is because, with integers, x*x gives a totally accurate answer, whereas pow(x, 2) gives a floating-point answer with rounding errors.

When wanting a square root, it is normally a good idea to use sqrt(), rather than pow(). The algorithm in sqrt(), being specific to the power 0.5 only, is likely to be faster and more accurate.
[ September 25, 2006: Message edited by: Peter Chase ]

 Did you just should on me? You should read this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS