Results 1 to 5 of 5
  1. #1
    SkoodlyWah is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Post 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.");
    }
    }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,651
    Rep Power
    5

    Default Re: need some heLp T.T

    You should not ask another question or questions on someone else's thread. You should start your own thread but only after you have tried to determine the answer yourself. Have you checked the Java API documentation to see what is says?

    I also recommend you read the forum rules under the FAQ menu item.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Math.sqRt and negative numbers

    Removed from need some heLp T.T

    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: need some heLp T.T

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Math.sqrt not working!
    By atiyarose17 in forum New To Java
    Replies: 15
    Last Post: 10-10-2012, 12:55 AM
  2. Math.sqrt is not working
    By GoingDarkness in forum NetBeans
    Replies: 5
    Last Post: 04-08-2012, 05:31 AM
  3. forming negative numbers
    By TopNFalvors in forum New To Java
    Replies: 4
    Last Post: 04-11-2011, 01:38 AM
  4. Mod of Negative Numbers
    By Venny in forum New To Java
    Replies: 7
    Last Post: 01-28-2011, 05:32 AM
  5. Help with negative numbers
    By cela0811 in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 05:04 PM

Posting Permissions

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