- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近在计算 java 中 double 元组的哈希码时遇到了一个奇怪的情况。假设您有两个元组 (1.0,1.0) 和 (Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY)。使用 Joshua Bloch's Effective Java 中所述的成语(第 7 项),这两个元组不会被认为是相等的(假设这些元组是对象)。但是,使用第 8 项中所述的公式计算每个元组的 hashCode()
计算结果相同。
所以我的问题是:这个公式有什么奇怪的地方是我在编写公式时遗漏的,还是只是散列码冲突的奇怪情况?
这是我用来说明情况的简短比较方法(我将其编写为 JUnit4 测试,但它应该很容易转换为 main
方法)。
@Test
public void testDoubleHashCodeAndInfinity(){
double a = 1.0;
double b = 1.0;
double c = Double.POSITIVE_INFINITY;
double d = Double.POSITIVE_INFINITY;
int prime = 31;
int result1 = 17;
int result2 = 17;
long temp1 = Double.doubleToLongBits(a);
long temp2 = Double.doubleToLongBits(c);
//this assertion passes successfully
assertTrue("Double.doubleToLongBits(Double.POSITIVE_INFINITY" +
"==Double.doubleToLongBits(1.0)",temp1!=temp2);
result1 = prime*result1 + (int)(temp1^(temp1>>>32));
result2 = prime*result2 + (int)(temp2^(temp2>>>32));
//this assertion passes successfully
assertTrue("Double.POSITIVE_INFINITY.hashCode()" +
"==(1.0).hashCode()",result1!=result2);
temp1 = Double.doubleToLongBits(b);
temp2 = Double.doubleToLongBits(d);
//this assertion should pass successfully
assertTrue("Double.doubleToLongBits(Double.POSITIVE_INFINITY" +
"==Double.doubleToLongBits(1.0)",temp1!=temp2);
result1 = prime*result1+(int)(temp1^(temp1>>>32));
result2 = prime*result2+(int)(temp2^(temp2>>>32));
//this assertion fails!
assertTrue("(1.0,1.0).hashCode()==" +
"(Double.POSITIVE_INFINITY,Double.POSITIVE_INFINITY).hashCode()",
result1!=result2);
}
最佳答案
纯属巧合。然而,这是一个有趣的问题。试试这个:
Double d1 = 1.0;
Double d2 = Double.POSITIVE_INFINITY;
int hash1 = d1.hashCode();
int hash2 = d2.hashCode();
// These both print -1092616192
// This was me using the wrong hash combinator *and*
// the wrong tuples... but it's interesting
System.out.println(hash1 * 17 + hash2);
System.out.println(hash2 * 17 + hash1);
// These both print -33554432
System.out.println(hash1 * 31 + hash1);
System.out.println(hash2 * 31 + hash2);
基本上哈希的位模式决定了这一点。 hash1(1.0 的哈希码)是 0x3ff00000,hash2(infinity 的哈希码)是 0x7ff00000。那种哈希和那种乘法器产生那种效果......
执行摘要:这是一个巧合,但不要担心:)
关于java - Double.POSITIVE_INFINITY 的 hashCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1959742/
我试过了 System.out.println(Double.isInfinite(Float.POSITIVE_INFINITY)) System.out.println(Double.isInfi
我们可以比较 Java 中 Double wrapper 的 POSITIVE_INFINITY 吗? 如果 double a 和 double b 都设置为 Double.POSITIVE_INFI
我了解 Number.POSITIVE_INFINITY值为 Infinity , 和 Number.NEGATIVE_INFINITY值为 -Infinity . 我会使用 Number.POSIT
在java中,合法吗?我需要找到与原点具有相同斜率的共线点。垂直线具有正无穷斜率。在手机上,奇怪的打字 最佳答案 摘自Java语言规范 15.20.1 Numerical Comparison Ope
我最近在计算 java 中 double 元组的哈希码时遇到了一个奇怪的情况。假设您有两个元组 (1.0,1.0) 和 (Double.POSITIVE_INFINITY,Double.POSITIV
NaN、POSITIVE_INFINITY 和 Double 类中的一些其他常量的值是多少?从源代码中,我看到它们设置为自己,但这是如何工作的? public final class Double e
以下代码将如何表现,尤其是当双计数器达到其极限 ((2-2^-52)·2^1023) 时? for (double i = 0; i < Double.POSITIVE_INFINITY; i++){
我尝试了 BigDecimal.valueOf(Double.POSITIVE_INFINITY),但一直收到 NumberFormatException 谢谢 最佳答案 您不能在 BigDecima
简单的问题:以下代码是否可以在 double 组中找到最小值(假设至少存在一个值): double[] values = ... double currentMin = Double.POSITIVE
Float.POSITIVE_INFINITY 和 Float.MAX_VALUE 有什么区别?哪个更大?它们是一样的吗? 我遇到了他们,他们正在寻找一个比所有其他 float 都大的值,或者除了最大
我试过了: import kotlin.Double.Companion.POSITIVE_INFINITY import kotlin.Double.Companion.NaN const val
我有以下简单的 JPA 实体: @Entity @Table( name = myentity_table ) public class MyEntity { private double a;
这个问题在这里已经有了答案: Why does division by zero with floating point (or double precision) numbers not thro
有多种方法可以检查一个数字是否是有限的。 使用 Number.isFinite() 功能: Number.isFinite(number) 比较 Infinity : Math.abs(number)
描述:我正在做一些练习来熟悉 Javascript。其中一个问题要求我找到下面给定对象中的最小数字。 var myObj = { key: [5, 8, 1, 4] }; 我将 smallest 的初
我想通过 Double.NEGATIVE_INFINITY , Double.POSITIVE_INFINITY和 Double.NaN作为 JUnit5 中的 CSV 参数: @DisplayNam
出于某种奇怪的原因,如果我尝试使用设置 HBox 的高度 setPrefHeight(10000) 它创造了奇迹(它试图填充尽可能多的物理空间),同时尝试以下任何(更明显的)选项会使布局变得疯狂: s
我是一名优秀的程序员,十分优秀!