Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: How to make a number not infinite

  1. #1
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Exclamation How to make a number not infinite

    Alright so in my math class my teacher wants us to buy a 100 dollar Calculator inwich i cant offored. So I am now designing a calculator that will do everything I need the other one to do. Currently i am trying to find the correlation Coeffiecent everything works fine so fare except in the formula there is a line
    Java Code:
    r = Math.sqrt(totalPoints*xTotalSquared-Math.pow(xTotal,2);
    So after some trouble shooting i have found for a fact that my problem is the Math.sqrt.It returns a infinite number so I get nan instead of the Answer. Would any of you happen to know how to make my answer go from infinite to terminating.Please help right away i need to do my homework.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How to make a number not infinite

    Generally NaN is considered a "feature".

    Some mathematical expressions are syntactically correct, but make no sense in practice. For example you can use trigonometry to find the angles of a triangle given the sides. It's a nice expression, and all goes well until you substitute in the values 1 - 1 - 100 as the sides of the triangle: there simply is no such triangle or angles to be found in this case. The computing result in such cases is often NaN.

    The best attitude, on encountering a NaN result, is not to wonder how to avoid it, but, rather, ask "why does this occur?", "am I calculating the right thing?", "am I calculating the right thing, but in an invalid context?".

    ---

    According to the API docs Math.sqrt() will return NaN if you try to evaluate the square root of a negative number.

    n*sigma(x^2) - sigma(x)^2 looks ok for the variance in one direction. Ie it's not obvious why the thing in the sqrt() should be negative if "total" means what it ought to mean. But, almost certainly, you are trying to find the square root of a negative quantity and the reason for that lies elsewhere in your code. Consider posting a SSCCE that shows the NaN occurring.
    penguinCoder likes this.

  3. #3
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: How to make a number not infinite

    Thanks you just help me solve it. It was because of the whole negative thing and i already know how to fix it thank you so much

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: How to make a number not infinite

    You're welcome.

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

    Default Re: How to make a number not infinite

    Quote Originally Posted by Daryn View Post
    Java Code:
    r = Math.sqrt(totalPoints*xTotalSquared-Math.pow(xTotal,2);
    So after some trouble shooting i have found for a fact that my problem is the Math.sqrt.It returns a infinite number so I get nan instead of the Answer. Would any of you happen to know how to make my answer go from infinite to terminating.Please help right away i need to do my homework.
    I think I don't understand your question; Math.sqrt(x) return infinite if and only if x (the argument) is infinite; and where does the NaN come from?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. how to make grid number ?
    By nikiiz in forum New To Java
    Replies: 6
    Last Post: 05-28-2012, 06:02 PM
  2. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 03:28 AM
  3. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 09:59 AM
  4. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 05:56 AM
  5. Will make a pyramid of some kind out of a number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:20 AM

Posting Permissions

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