[英]Java statement with less than equal to on the right side
Here's a line from a large Java program which compiles without errors.这是一个大型 Java 程序中的一行代码,它编译时没有错误。 I'm unable to understand how the <= operator is being used on the right side of assignment statement:
我无法理解如何在赋值语句的右侧使用 <= 运算符:
converged = measure.distance(centroid.getLengthSquared(), centroid, getCenter()) <= convergenceDelta;
Is this some obscure Java feature?这是一些不起眼的 Java 功能吗?
No, it's not.不,这不对。
As you will see converged
will be of boolean
type.如您所见,
converged
将是boolean
类型。
It will be exactly the same as saying和说的完全一样
boolean foo = 2 <= 3;
So, probably in your code measure.distance(centroid.getLengthSquared(), centroid, getCenter())
returns a number which is then compared using the relational <=
operator with convergenceDelta;
因此,可能在您的代码
measure.distance(centroid.getLengthSquared(), centroid, getCenter())
返回一个数字,然后使用关系<=
运算符将其与convergenceDelta;
进行比较. . The result will be
true
or false
, a boolean value which will be saved at converged
.结果将是
true
或false
,一个 boolean 值将在converged
时保存。
Not at all.一点也不。
<=
and the other relational operators return boolean values. <=
和其他关系运算符返回 boolean 值。 If converged
is a boolean variable, you can assign that value to it.如果
converged
的是 boolean 变量,则可以将该值分配给它。
It is setting converged to the truth statement of the distance being less than or equal to the value on the variable convergenceDelta.它设置收敛到距离小于或等于变量convergenceDelta 上的值的真值陈述。
<= is a binary operator like any other. <= 和其他运算符一样是二元运算符。 The value of it is true if LHS <= RHS, and false if not.
如果 LHS <= RHS,则它的值为 true,否则为 false。
So in this case, if the distance is less than the convergence delta it's considered to have converged.所以在这种情况下,如果距离小于收敛增量,则认为它已经收敛。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.