• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

squaring

 
Katrina Cobb
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works for me. Is it the formatting of the (double) result you don't like?
 
Katrina Cobb
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.... I suppose I do deserve a smack on the head
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Katrina Cobb:
ok.... I suppose I do deserve a smack on the head




I meant like "Doh!" where you smack your own forehead!

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!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic