Double.doubleToLongBits()方法使用

本文详细介绍了Java中Double类的doubleToLongBits和longBitsToDouble方法,这两个方法分别用于将double类型数据转换为long类型以及将long类型数据转换回double类型。通过这些方法,可以按照long类型的数据比较方式来比较double类型的数值。文章还给出了方法的使用示例及注意事项,包括如何判断正负无穷大等情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

该方法可以将double类型数据转换成long类型数据,从而可以使double类型数据按照long的方法判断大小(<, >, ==)。

例如:

  1. Double.doubleToLongBits(10.00) == Double.doubleToLongBits(10.00)

  2. Double.doubleToLongBits(20.00) > Double.doubleToLongBits(10.00)

  3. Double.doubleToLongBits(10.00) < Double.doubleToLongBits(20.00)

Double类longBitsToDouble()方法 (Double class longBitsToDouble() method)

  • longBitsToDouble() method is available in java.lang package.

    longBitsToDouble()方法在java.lang包中可用。

  • longBitsToDouble() method is used to return the double value corresponding to the given long bit denotation followed by IEEE 754 double floating-point standards.

    longBitsToDouble()方法用于返回与给定的长位符号对应的双精度值,后跟IEEE 754双浮点标准。

  • longBitsToDouble() method is a static method, it is accessible with the class name too and if we try to access the method with the class object then also we will not get an error.

    longBitsToDouble()方法是一个静态方法,也可以使用类名进行访问,如果尝试使用类对象访问该方法,那么也不会出错。

  • longBitsToDouble() method does not throw an exception at the time of representing bits.

    longBitsToDouble()方法在表示位时不会引发异常。

Syntax:

句法

    public static double longBitsToDouble(long bits_rep);

Parameter(s):

参数:

  • long bits_rep – represents the long integer value.

    long bits_rep –表示长整数值。

Return value:

返回值:

The return type of this method is double, it returns the bits that represent the long integer value.

此方法的返回类型为double ,它返回表示长整数值的位。

Note:

注意:

  • If we pass "0x7ff0000000000000L", it returns the value "positive infinity".

    如果我们传递“ 0x7ff0000000000000L” ,它将返回值“ positive infinity” 。

  • If we pass "0xfff0000000000000L", it returns the value "negative infinity".

    如果我们传递“ 0xfff0000000000000L” ,它将返回值“ negative infinity” 。

// Java program to demonstrate the example 
// of longBitsToDouble(long bits_rep)
// method of Double class
 
public class LongBitsToDoubleOfDoubleClass {
    public static void main(String[] args) {
        // Variables initialization
        double d1 = 18.20;
        double d2 = 19.20;
 
        // Display value1,value2 values
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
 
        // Double instance creation
        Double value1 = new Double(d1);
        Double value2 = new Double(d2);
 
 
        // It returns the double value denoted by the given bit denotation 
        //by calling value1.longBitsToDouble(124568)
        double result1 = value1.longBitsToDouble(124568);
 
        // It returns the double value denoted by the given bit denotation 
        //by calling value1.longBitsToDouble(124568)
 
        double result2 = value2.longBitsToDouble(0xfff0000000000000L);
 
        // Display result1,result2 values
        System.out.println("value1.longBitsToDouble(124568): " + result1);
        System.out.println("value2.longBitsToDouble(0xfff0000000000000L): " + result2);
    }
}

 

d1: 18.2
d2: 19.2
value1.longBitsToDouble(124568): 6.1545E-319
value2.longBitsToDouble(0xfff0000000000000L): -Infinity


详细分析一下分析代码的封装 private final double initialPrice; private double currentValuation; private final Random random = new Random(); //constructors public MarketProperty(String id, String category, double size, double initialPrice) { super(id, category, size); this.initialPrice = initialPrice; this.currentValuation = this.initialPrice; } //methods /Through takes two parameters and updates thecurrent valuaton of the property based on a random value generated using the inflacyion rate and volatility ./ public void updateValuation(double inflationRate, double volatility) { double gaussian = Math.sqrt(volatility * volatility) * random.nextGaussian() + inflationRate; this.currentValuation = initialPrice * (1 + gaussian); } //getters public double getInitialPrice() { return initialPrice; } public double getCurrentValuation() { return this.currentValuation; } public double getTotalProfit() { return currentValuation - this.initialPrice; } public double getRelativeProfit() { return getTotalProfit() / this.initialPrice; } @Override public String toString() { return "ID : " + getID() + ", Initial Price = " + getInitialPrice() + ", Current Valuation= " + getCurrentValuation() + "."; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final MarketProperty other = (MarketProperty) obj; return Objects.equals(this.currentValuation, other.currentValuation); } @Override public int hashCode() { int hash = 7; hash = 67 * hash + (int) (Double.doubleToLongBits(this.initialPrice) ^ (Double.doubleToLongBits(this.initialPrice) >>> 32)); hash = 67 * hash + (int) (Double.doubleToLongBits(this.currentValuation) ^ (Double.doubleToLongBits(this.currentValuation) >>> 32)); return hash; } //MarketProperties are compared by theircurrent valuation public int compareTo(MarketProperty other) { return Double.compare(this.currentValuation, other.currentValuation);
05-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值