# Thread: How to make a number not infinite

1. Senior Member Join Date
Oct 2012
Posts
176
Rep Power
7

## 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.  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## 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.  Reply With Quote

3. Senior Member Join Date
Oct 2012
Posts
176
Rep Power
7

## 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  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## Re: How to make a number not infinite

You're welcome.  Reply With Quote

5. ## Re: How to make a number not infinite Originally Posted by Daryn 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  Reply With Quote

#### Posting Permissions

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