Java:BigDecimal 和 Double.NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28065158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:57:45  来源:igfitidea点击:

Java: BigDecimal and Double.NaN

javamathnumbersbigdecimal

提问by Denis Kulagin

I am trying to execute following code:

我正在尝试执行以下代码:

import java.math.*;

public class HelloWorld{

     public static void main(String []args){
            System.out.println(BigDecimal.valueOf(Double.NaN));
     }
}

And reasonably, I am getting:

并且合理地,我得到:

Exception in thread "main" java.lang.NumberFormatException                                                    
    at java.math.BigDecimal.<init>(BigDecimal.java:470)                                                   
    at java.math.BigDecimal.<init>(BigDecimal.java:739)                                                   
    at java.math.BigDecimal.valueOf(BigDecimal.java:1069)                                                 
    at HelloWorld.main(HelloWorld.java:6)    

Is there a way to represent Double.NaNin BigDecimal?

有没有办法在 BigDecimal 中表示Double.NaN

回答by Stephen C

Is there a way to represent Double.NaN in BigDecimal?

有没有办法在 BigDecimal 中表示 Double.NaN?

No. The BigDecimalclass provides no representation for NaN, +∞ or -∞.

不。BigDecimal该类不提供 NaN、+∞ 或 -∞ 的表示。

You might consider using null... except that you need at least 3 distinct nullvalues to represent the 3 possible cases, and that is not possible.

您可能会考虑使用null... ,但您需要至少 3 个不同的null值来表示 3 种可能的情况,这是不可能的。

You could consider creating a subclass of BigDecimalthat handles these "special" values, but it may be simpler to implement your "numbers" as a wrapper for BigDecimal, and treat NaNand the like as special cases; e.g.

您可以考虑创建一个BigDecimal处理这些“特殊”值的子类,但将“数字”实现为 的包装器可能更简单BigDecimalNaN并将其视为特殊情况;例如

public class MyNumber {
    private BigDecimal value;
    private boolean isNaN;
    ...

    private MyNumber(BigDecimal value, boolean isNaN) {
        this.value = value;
        this.isNaN = isNan;
    }

    public MyNumber multiply(MyNumber other) {
        if (this.isNaN || other.isNaN) {
            return new MyNumber(null, true);
        } else {
            return new MyNumber(this.value.multiply(other.value), false);
        }
    }

    // etcetera
}

回答by atish shimpi

"NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.

“NaN”代表“非数字”。如果浮点运算具有一些导致运算产生某些未定义结果的输入参数,则会产生“Nan”。例如,0.0 除以 0.0 在算术上是未定义的。取负数的平方根也是未定义的。

BigDecimal.valueOf(Double.NaN)It trying to convert Double.NaNto BigDecimal, and result is number can not be converted in to BigDecimali.e. java.lang.NumberFormatException.

BigDecimal.valueOf(Double.NaN)它试图转换Double.NaNBigDecimal,结果是数字无法转换为BigDecimalie java.lang.NumberFormatException

回答by Amr

NaN = not a number. Its not a number, so it cant be converted to BigDecimal

NaN = 不是数字。它不是数字,因此无法转换为 BigDecimal

回答by Stephan

You can rewrite your code like this:

你可以像这样重写你的代码:

 public static void main(String []args){
     Double d = ...;
     String value; 

     if (Double.isNaN(d) ==  true) {
         value= "My-NaN-formatting";
     } else {
         value=BigDecimal.valueOf(d);
     }

     System.out.println(value);
 }