Math.sqRt and negative numbers

Hi there. I realize that this thread is over two year old, but I am currently working on this same problem. My question is slightly different.

All I would like to know is - Does the Math.sqrt() method actually throw an **ArithmeticException** if a negative integer is used for the argument?

I tried some simple code and all I'm getting in my output is "The square root of your number is NaN" when I enter a negative value.

I have included my code below.

Cheers,

Ian

~~~~~~~~~~~~~~~~~~~~~~~~~~~

import javax.swing.*;

import java.lang.Math.*;

public class SqrtException

{

public static void main(String[] args) throws ArithmeticException

{

String entry = new String();

int squareRoot;

double squareRootResult;

try

{

entry = JOptionPane.showInputDialog(null, "Enter a number to be square-rooted: ");

squareRoot = Integer.parseInt(entry);

squareRootResult = Math.sqrt(squareRoot);

JOptionPane.showMessageDialog(null, "The square root of your number is " + squareRootResult);

}

catch(ArithmeticException error)

{

JOptionPane.showMessageDialog(null, "Negative number is invalid for calculation of square root.");

}

}

}

Re: Math.sqRt and negative numbers

Removed from http://www.java-forums.org/new-java/...-help-t-t.html

SkoodlyWah, don't post to old dead threads and don't hijack another poster's thread. Not for a slightly different problem, not for the same problem. get your own thread; they're free.

db

Re: Math.sqRt and negative numbers

Only integer division (/) or integer remainder (%) operators throw an ArithmeticException if the right operand is zero; floating point numbers produce +- Infinity or NaN (if both operands are zero).

kind regards,

Jos