我有两个类型为 和 的变量 y 和doublez long。我的问题是,即使它们具有不相等的值,相等运算符也会为它们返回 true,如下面的代码片段所示:
public class Test {
public static void main(String[] args) throws Exception {
double y = (double) Long.MAX_VALUE;
System.out.println(y);//9.223372036854776E18
long z = Long.MAX_VALUE - 1;
System.out.println(z);//9223372036854775806
System.out.println(y == z);//=============true
}
}
Run Code Online (Sandbox Code Playgroud)
我的疑问是,如果 z 的值小于 y,为什么y==z返回 true?
有人可以解释这种行为吗?
假设您有两个相同的对象(意味着它们分别具有相同的属性和相同的值)。
你如何测试平等?
例子
$obj1&$obj2是相同的
这是我尝试过的:
if($obj1 -eq $obj2)
{
echo 'true'
} else {
echo 'false'
}
# RETURNS "false"
if(Compare-Object -ReferenceObject $obj1 -DifferenceObject $obj2)
{
echo 'true'
} else {
echo 'false'
}
# RETURNS "false"
Run Code Online (Sandbox Code Playgroud)
编辑
这并不相同
我不明白 struct 和 class 相等性检查之间的区别。由于 Struct 和 Class 都从内核获取 #hash,但它们的行为似乎不同。
我知道 instance.hash 会为每个类实例产生不同的结果。与类实例 [Foo, Object, Kernel, BasicObject] 相比,Struct 实例具有不同的祖先 [Customer, Struct, Enumerable, Object, Kernel, BasicObject]。是什么真正导致每个 Class 实例与其他实例具有不同的哈希数
Customer = Struct.new(:name, :phone, :address) do
end
class Foo
def initialize(the_name, phone, address)
@name = the_name
@phone = phone
@address = address
end
end
str_a = Customer.new('bond', 'ring', 'address')
str_b = Customer.new('bond', 'ring', 'address')
foo_a = Foo.new('bond', 'ring', 'address')
foo_b = Foo.new('bond', 'ring', 'address')
p str_a == str_b #true
p foo_a == foo_b …Run Code Online (Sandbox Code Playgroud) 如果我有一个结构:
pub struct Test {
val1: u8,
val2: u8,
}
Run Code Online (Sandbox Code Playgroud)
我做了vec!这样的一个:
let a = Test{val1: 1, val2: 1};
let b = Test{val1: 1, val2: 2};
let c = Test{val1: 1, val2: 2};
let my_vec = vec![a, b];
Run Code Online (Sandbox Code Playgroud)
如何判断是否my_vec包含与 c 具有相同值的结构?
我有 2 个数据框,其中有 2 个相同的列。我想检查数据集是否相同。原始数据集有大约 700K 记录,但我正在尝试找出一种使用虚拟数据集来做到这一点的方法
\n\n我尝试使用比较、相同、全部、all_equal 等。它们都没有返回 True。
\n\n虚拟数据集是 -
\n\na <- data.frame(x = 1:10, b = 20:11)\nc <- data.frame(x = 10:1, b = 11:20)\n\nall(a==c)\n[1] FALSE\n\ncompare(a,c)\nFALSE [FALSE, FALSE]\n\nidentical(a,c)\n[1] FALSE\n\n all.equal(a,c)\n[1] "Component \xe2\x80\x9cx\xe2\x80\x9d: Mean relative difference: 0.9090909" "Component \xe2\x80\x9cb\xe2\x80\x9d: Mean relative difference: 0.3225806"\n\nRun Code Online (Sandbox Code Playgroud)\n\n除了记录的顺序之外,数据集完全相同。如果这些函数仅在数据集互为镜像时才起作用,那么我必须尝试其他方法。如果是这种情况,有人可以帮助我如何为这两个数据集(无序)获得 True
\n在我的程序中,我试图通过编写验证我定义的函数的功能的测试来测试我的程序。在它们通过我的函数后,我正在测试序列的相等性。
my-reverse 的定义:
(defn my-reverse [coll]
(if (empty? coll)
[]
(conj
(my-reverse (rest coll))
(first coll)
)
)
)
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我的断言失败了,因为 (my-reverse [1]) 返回 [1]。这是断言:
(assert (identical? (my-reverse [1]) [1]))
Run Code Online (Sandbox Code Playgroud)
谢谢大家!
我有这个类,在那里我覆盖了 Object Equals:
public class Foo
{
public string string1 { get; set; }
public string string2 { get; set; }
public string string3 { get; set; }
public override bool Equals(object other)
{
if (!(other is Foo)) return false;
Foo otherFoo = (other as Foo);
return otherFoo.string1 == string1 && otherFoo.string2 == string2 && otherFoo.string3 == string3;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个警告“覆盖 object.equals 但不覆盖 object.gethashcode”,我理解覆盖 GetHashCode 的必要性,以便我的类型根据可散列类型进行操作。
据我研究,为了使此代码唯一,通常使用 XOR 运算符,或者涉及素数乘法。所以,根据我的消息来源,来源1和源2我正在考虑我的GesHashCode覆盖方法这两个选项。
1:
public override int GetHashCode() {
return …Run Code Online (Sandbox Code Playgroud) 据我了解,引用之间的相等比较比较的是引用对象的值,而不是引用中包含的地址。即它们隐式地取消引用引用。
既然如此,为什么还要写:
if ref_to_foo == &another_foo {
Run Code Online (Sandbox Code Playgroud)
而不是
if ref_to_foo == another_foo {
Run Code Online (Sandbox Code Playgroud)
什么时候
if ref_to_foo == ref_to_another_foo {
Run Code Online (Sandbox Code Playgroud)
双方已经隐式解除引用?
显而易见的答案是“因为编译器创造了我”,但我试图理解为什么语言设计者认为这是一个坏主意。
IEquatable<>我在我写的下面的课程中遇到了相等比较的问题:
public class Bead: IEquatable<Bead>
{
public string Name { get; set; }
public Point2d Location { get; private set; }
public void SetLocation(Point2d newLocation)
{
Location = newLocation;
}
#region equality comparison
/// <summary>
/// Equality comparisons
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public override bool Equals(object other)
{
if (!(other is Bead)) return false;
return Equals((Bead)other); // Calls method below
}
public bool Equals(Bead other) // Implements IEquatable<Point2d>
{
return Location == other.Location && Name == …Run Code Online (Sandbox Code Playgroud) 这可能是使用dplyr和tidyverse 工具的一个非常基本的问题,但我找不到一个好的方法来做到这一点。
\n假设我有一个宽格式的数据框,并且我想选择行,以便列的子集具有所有相同的值。天真地,我可以执行以下操作:
\n> df <- tribble(\n ~name, ~id, ~cost, ~value1 , ~value2, ~value3,\n "a", 1, 10, 1, 1, 1,\n "a", 2, 20, 1, 2, 1,\n "b", 3, 50, 1, 1, 3,\n "b", 4, 45, 1, 1, 1,\n "b", 5, 70, 2, 2, 2\n)\n\n\n> df %>% select(\n value1 == value2 &\n value1 == value3 &\n value2 == value\n)\n\n# A tibble: 3 \xc3\x97 6\n name id cost value1 value2 value3\n <chr> <dbl> <dbl> <dbl> <dbl> …Run Code Online (Sandbox Code Playgroud) equality ×10
c# ×2
r ×2
rust ×2
assert ×1
clojure ×1
collections ×1
compare ×1
conditional ×1
dataframe ×1
filter ×1
gethashcode ×1
iequatable ×1
if-statement ×1
java ×1
object ×1
powershell ×1
reference ×1
ruby ×1
startswith ×1
struct ×1
tidyverse ×1
traits ×1