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);
That works for me. Is it the formatting of the (double) result you don't like?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Sep 22, 2006
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.
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!
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!
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 ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com