Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

Why does a segment of code in java produce NaN when running?

So I'm running a segment of code (posted below and it produces a NaN when running (the code I'm running is just producing an answer that can be either negative or positive (quadratic basically)))

public double quadratic(int a, int b, int c)
{
  double quadratic = Math.pow(def, 2) - 4*def*def;
  return((-1*b + Math.sqrt(quadratic)) / 2*a);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Math.sqrt(quadratic) returns a valid value only when quadratic is greater or equals to zero.

If quadratic is less than zero Math.sqrt(quadratic) returns NaN. For instance, it returns NaN if def is one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...