Javas Math.sin() 始终产生 NaN

发布于 2024-08-25 19:48:22 字数 766 浏览 6 评论 0原文

如果这是一个愚蠢的初学者问题,请原谅我,但我真的不明白。

我有一个成员变量声明如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝戈者 2024-09-01 19:48:22
Double value = 3.14159265;
System.out.println(value.toString()); //outputs 3.14159265
value = Math.sin(value);
System.out.println(value.toString()); //outputs NaN

输出(如预期):

3.14159265
3.5897930298416118E-9

所以问题应该出在其他地方,您使用的是哪个版本的 JDK?是 32 位还是 64 位?中间还有其他代码没贴吗?

Math.sin 获取 NaN 的唯一方法是使用 inf o NaN 作为参数。如果没有显式将该值分配给一个数字,您只需进行错误的计算即可获得该数字,例如:

Double d = 0.0/0.0;
System.out.println(Math.sin(d));
double d2 = Double.POSITIVE_INFINITY;
System.out.println(Math.sin(d2));

这将输出:NaN两次。

两个注意事项:

  • 不要对变量使用大写字母(使用 value,而不是 Value
  • 当您需要不需要的字符串作为参数(或组成其他字符串)时, 要显式调用 toString(),您可以执行 System.out.println(value)
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, not Value)
  • when you need stings as parameters (or composed to other strings) you don't need to explicitly call toString(), you can do System.out.println(value)
不喜欢何必死缠烂打 2024-09-01 19:48:22

这就是我得到的:

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

所以有 2 个选项:

  1. 您发布的代码不是您正在使用的代码(我注意到您系统上有一个小写的 S,所以这不会编译)
  2. 其他东西正在改变值在执行 Math.sin 之前或之后将 Value 变为 NaN

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:

  1. 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)
  2. Something else is changing the value of Value to NaN before or after you do the Math.sin
同展鸳鸯锦 2024-09-01 19:48:22

对于初学者来说,您使用的是 Double 引用(包装器)类型,而不是 double 原始类型 - 为什么?您实际用于初始化 Value 的代码是什么(顺便说一句,根据常见的 Java 编码标准,该代码不应以大写字符开头)?我的猜测是您只是没有正确初始化 Value 。尝试从使用 double (不是 Double)开始。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文