Javas Math.sin() 始终产生 NaN
如果这是一个愚蠢的初学者问题,请原谅我,但我真的不明白。
我有一个成员变量声明如下:
public Double Value;
当我将 3.14159265 分配给 Value 并尝试计算它的正弦时,会发生这种情况:
system.out.println(Value.toString()); //outputs 3.14159265
Value = Math.sin(Value);
system.out.println(Value.toString()); //outputs NaN
事实上,这种情况发生在我尝试的每个值上 - 即使是 0! 无论参数值如何,Math.sin() 似乎总是产生 NaN 结果。
文档说:
如果参数为 NaN 或无穷大,则结果为 NaN。
但我的论点显然不是 NaN 或无穷大!
那里到底发生了什么?
更新
原来我是地球上最愚蠢的程序员。在我的项目中,整个代码当然比上面的示例复杂得多。它是一种表达式解析器和表达式解析器。对于定义的数学函数,我使用 switch 子句来决定调用哪个函数 - 我忘记了在导致 sqrt 函数使用负参数执行的情况下的 break 语句。
正如我所说 - 地球上最愚蠢的程序员......
我接受了最重要的答案,因为它是最好的恕我直言。抱歉各位浪费了你们的时间-.-
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 = Math.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 -.-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
输出(如预期):
所以问题应该出在其他地方,您使用的是哪个版本的 JDK?是 32 位还是 64 位?中间还有其他代码没贴吗?
从
Math.sin
获取NaN
的唯一方法是使用inf
oNaN
作为参数。如果没有显式将该值分配给一个数字,您只需进行错误的计算即可获得该数字,例如:这将输出:
NaN
两次。两个注意事项:
value
,而不是Value
)toString()
,您可以执行System.out.println(value)
outputs (as expected):
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
fromMath.sin
is by usinginf
oNaN
as the parameter. And without explicitly assign that value to a number you can obtain in just by doing wrong calculations, eg:this will output:
NaN
twice.Two notes:
value
, notValue
)toString()
, you can doSystem.out.println(value)
这就是我得到的:
所以有 2 个选项:
THis is what I get:
So there are 2 options:
对于初学者来说,您使用的是
Double
引用(包装器)类型,而不是double
原始类型 - 为什么?您实际用于初始化Value
的代码是什么(顺便说一句,根据常见的 Java 编码标准,该代码不应以大写字符开头)?我的猜测是您只是没有正确初始化Value
。尝试从使用double
(不是Double
)开始。For starters, you're using the
Double
reference (wrapper) type, not thedouble
primitive type - why? What is the code that you're actually using to initializeValue
(which, BTW, should not start with an uppercase character based on common Java coding standards)? My guess is that you're just not initializingValue
properly. Try starting with using adouble
(not aDouble
).