Javas Math.sin() produces NaN all the time
Forgive me if this is a dumb beginners problem, but I really don't get it.
I have a member variable declared like so:
public Double Value;
When I assign 3.14159265 to Value and try to compute the sine of it, this happens:
system.out.println(Value.toString()); //outputs 3.14159265
Value = Ma开发者_StackOverflow社区th.sin(Value);
system.out.println(Value.toString()); //outputs NaN
In fact, this happens with every single value I tried - even with 0! Math.sin() seems to always produce NaN as a result, regardless of the arguments value.
The docs say:
If the argument is NaN or an infinity, then the result is NaN.
But my argument is clearly not NaN or infinity!
What the heck is happening there?
UPDATE
Turns out I'm the dumbest programmer on earth. In my project the whole code is of course much more complex than the example above. It's kind of an expression parser & evaluator and for the defined mathematical functions I use a switch-clause to decide which function to call - I forgot the break statement in the cases which caused the sqrt function to be executed with a negative parameter.
As I said - dumbest programmer on earth...
I accepted the topmost answer as it is the best imho. Sorry guys for wasting your time -.-
Double value = 3.14159265;
System.out.println(value.toString()); //outputs 3.14159265
value = Math.sin(value);
System.out.println(value.toString()); //outputs NaN
outputs (as expected):
3.14159265
3.5897930298416118E-9
so problem should be somewhere else, which version of JDK are you using? is it 32 bit or 64 bit? is there other code in the middle that you didn't paste?
The only way to obtain a NaN
from Math.sin
is by using inf
o NaN
as the parameter. And without explicitly assign that value to a number you can obtain in just by doing wrong calculations, eg:
Double d = 0.0/0.0;
System.out.println(Math.sin(d));
double d2 = Double.POSITIVE_INFINITY;
System.out.println(Math.sin(d2));
this will output: NaN
twice.
Two notes:
- don't use capitalized letters for variables (use
value
, notValue
) - when you need stings as parameters (or composed to other strings) you don't need to explicitly call
toString()
, you can doSystem.out.println(value)
THis is what I get:
Double Value;
Value = 3.14159265 ;
System.out.println(Value.toString()); //outputs 3.14159265
Value = Math.sin(Value);
System.out.println(Value.toString()); //outputs 3.5897930298416118E-9
So there are 2 options:
- The code you posted isn't the code you're using (I notice you have a lower case S on system, so this won't compile)
- Something else is changing the value of Value to NaN before or after you do the Math.sin
For starters, you're using the Double
reference (wrapper) type, not the double
primitive type - why? What is the code that you're actually using to initialize Value
(which, BTW, should not start with an uppercase character based on common Java coding standards)? My guess is that you're just not initializing Value
properly. Try starting with using a double
(not a Double
).
精彩评论