令人困惑的返回语句

发布于 2024-07-06 18:55:04 字数 376 浏览 5 评论 0原文

我无法准确理解 IF 语句的作用,从我所看到的来看,它正在检查变量 x 是否等于 int 0。 如果这是true,则返回变量y的绝对值...这是当我失去情节时,为什么return语句会继续包含><= ESPILON? 这肯定意味着小于或等于 epsilon 的值吗? 如果是的话,效果如何? 如果不是这个意思那么它是什么意思呢?

(JAVA代码)

final double EPSILON = 1E-14;
if (x == 0)
    return Math.abs(y) <= EPSILON;

I'm failing to understand exactly what the IF statement is doing, from what I can see it is checking if the variable x is equal to the int 0. If this is true the ABSOLUTE value of the variable y is returned... this is when I lose the plot, why would the return statement then go on to include <= ESPILON? Surely this means less than or equal to the value of epsilon? if so how is that working? If it doesn't mean that then what does it mean?

(JAVA CODE)

final double EPSILON = 1E-14;
if (x == 0)
    return Math.abs(y) <= EPSILON;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

饭团 2024-07-13 18:55:04

浮点数学本质上是不准确的,因此开发人员没有测试等效性(这总是一个坏主意),而是选择一个小数字(在本例中为 1x10^-14)作为接近零的可接受容差。 return 语句返回一个比较,因此它会获取 y 的绝对值,并且当且仅当它足够接近零时才返回 true,其中足够接近由 EPSILON 定义。

Floating-point math is by its nature inaccurate, so rather than testing for equivalence (which is always a bad idea), instead the developer has chosen a small number (1x10^-14 in this case) as the acceptable tolerance for proximity to zero. The return statement returns a comparison, so what this will do is take the absolute value of y, and return true if and only if it is sufficiently close to zero, where sufficiently close is defined by EPSILON.

铃予 2024-07-13 18:55:04

如果 y 的绝对值 <= EPSILON,则返回 true,否则返回 false。 <= 在 return 语句之前计算。 此代码是等效的:

if(x == 0)
{
   boolean ret = Math.abs(y) <= EPSILON;
   return ret;
}

该代码不是简单地从左到右读取。 一个更简单的例子是,

int x = 3 + 4 * 5;

计算后,x 是 23,而不是 35。计算结果是 3 + (4*5),而不是 (3+4)*5,因为 * 的优先级高于 +。 原始示例中的 return 语句的优先级非常低。 所有运算符(例如 +、-、<、>=)均在其之前求值。

It returns true if the absolute value of y is <= EPSILON, and false otherwise. The <= is evaluated before the return statement. This code is equivalent:

if(x == 0)
{
   boolean ret = Math.abs(y) <= EPSILON;
   return ret;
}

The code isn't simply read from left to right. A simpler example is

int x = 3 + 4 * 5;

After evaluating this, x is 23, not 35. The evaluation is 3 + (4*5), not (3+4)*5, because the * has a higher precedence than the +. The return statement in the original example has a very low precedence. All operators like +, -, <, >= are evaluated before it.

南风几经秋 2024-07-13 18:55:04

应首先计算整个表达式

Math.abs(y) <= EPSILON

,这意味着该函数将返回一个布尔值(true/false)。 话虽如此,如果

x != 0

那样我不确定会返回什么。

The entire expression

Math.abs(y) <= EPSILON

should be evaluated first, which means the function is going to return a boolean value (true/false). Having said that, if

x != 0

then I'm not sure what will get returned.

祁梦 2024-07-13 18:55:04

你是对的,它正在检查变量 x 是否等于(好吧,可能是 int)0。但是,如果这是真的,那么它不会返回 y 的绝对值,它返回一个布尔值,即<= 运算符。

You're right that it is checking if the variable x is equal to (well, maybe int) 0. However, if this is true then it doesn't return the absolute value of y, it returns a boolean, the result of the <= operator.

-小熊_ 2024-07-13 18:55:04

它返回一个布尔值。

Epsilon 是双精度型,其值为 1E-14。

这是实际的 IF 语句

if (x==0) {
    return MATH.abs(y) <= EPSILON;
}

因此,返回的是 y 的绝对值是否小于或等于 Epsilon。

It's returning a boolean value.

Epsilon is a double, holding the value 1E-14.

This is the actual IF statement

if (x==0) {
    return MATH.abs(y) <= EPSILON;
}

So, what's getting returned is if the absolute value of y is less than or equals to Epsilon.

握住你手 2024-07-13 18:55:04

我已经很长时间没有使用Java了,但是看起来这实际上返回了一个布尔值(可能是隐式转换的)。

我想说的是,如果 x 等于 0,则当 y 的绝对值 <= Epsilon 时返回 true,否则返回 false。

但是,如果 x 不等于 0,则它将返回 null,因为没有语句涵盖 else。

I haven't done Java in a long time but it would appear that this is actually returning a boolean (which might be implicitly cast).

I would say that if x equals 0, it returns true when the absolute value of y <= Epsilon, otherwise it returns false.

However if x doesn't equal 0 then it would return null, as no statement covers the else.

只怪假的太真实 2024-07-13 18:55:04

“问题”是这个片段严重依赖于运算符优先级(本身不错,但有时可能会令人困惑)。

在这里您可以找到所有 java 运算符的列表及其优先级,此处用于比较 C/C++ 的同一个表

The "issue" is that this fragment relies heavyly on operator precedence (not bad per se, but sometimes it can be confusing).

Here you can find a list of all java operators with their precedence, and here for comparison the same table for C/C++

┈┾☆殇 2024-07-13 18:55:04

为了清楚起见,它相当于

return (Math.abs(y) <= EPSILON);

应该添加到代码中的内容。 正如已经提到的,它返回一个布尔值。

替代方案是

if (Math.abs(y) <= EPSILON)
    return true;
else
    return false;

It is equivalent to this

return (Math.abs(y) <= EPSILON);

which should have been added to the code for clarity. As has been mentioned, it returns a boolean.

An alternatives would be

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