简体   繁体   English

如何在BigDecimal(Java)中存储无穷大?

[英]How to store infinity in a BigDecimal (Java)?

I am writing algorithms inside methods that return BigDecimal values but now and again the result calculated will be + or - infinity. 我在返回BigDecimal值的方法中编写算法,但现在计算的结果将是+或 - 无穷大。
Rather than the program crashing I'd like to catch the exception and return infinity as a value like the way you could if the method was returning a double. 而不是程序崩溃我想捕获异常并返回无穷大作为一个值,如果该方法返回双精度的方式。

eg Double.POSITIVE_INFINITY; 例如Double.POSITIVE_INFINITY;

So how do I store infinity in a BigDecimal? 那么如何在BigDecimal中存储无穷大? Or is there another way of doing this? 还是有另一种方法吗?

public static BigDecimal myalgorithm(){

//code to store infinity in a BigDecimal
//return the BigDecimal holding infinity 

}

BigDecimal doesn't have the concept of infinity. BigDecimal没有无限的概念。 I can think of three options: 我可以想到三个选择:

  1. The cleanest approach is probably to derive your own MyBigDecimal class, adding an infinity flag telling you if the instance contains infinity, and overriding the methods to which it would be relevant (which would be most of them, I would think), using the base class's version when you're not holding infinity and your own code when you are. 最干净的方法可能是派生你自己的MyBigDecimal类,添加一个无穷大标志,告诉你实例是否包含无穷大,并覆盖与之相关的方法(我认为这些方法中的大部分都是),使用基数当你没有持有无限和你自己的代码时,类的版本。

  2. You could use null as a flag value in your code, although that might be a bit of a pain. 您可以在代码中使用null作为标志值,尽管这可能有点痛苦。 Eg: 例如:

     if (theBigDecimal == null) { // It's infinity, deal with that } else { // It's finite, deal with that } 
  3. If you're already using null for something else, you could have a BigDecimal instance that doesn't actually contain infinity, but which you pretend containts it, and use == to check against it. 如果你已经使用null作为其他东西,你可能有一个实际上不包含无穷大的BigDecimal实例,但你假装包含它,并使用==来检查它。 Eg: 例如:

     // In your class somewhere: static final BigDecimal INFINITE_BIG_DECIMAL = new BigDecimal(); // Value doesn't matter // Then: if (theBigDecimal == INFINITE_BIG_DECIMAL) { // It's infinity, deal with that } else { // It's finite, deal with that } 

An approach to the problem would be, to just use a really large number in this case: 解决问题的方法是,在这种情况下使用非常大的数字:

BigDecimal.valueOf(negative ? Double.MIN_VALUE : Double.MAX_VALUE)

As BigDecimal does not have a real maximum or minimum value, you can use the ones of double. 由于BigDecimal没有真正的最大值或最小值,因此可以使用double的值。

Important with this approach is, that it doesn't really solve the problem of representing infinity, but depending on the usecase it can solve the problem well enough. 这种方法的重要之处在于,它并没有真正解决表示无穷大的问题,但根据用例,它可以很好地解决问题。 I used it for the same usecase as you have mentioned above. 我将它用于上面提到的相同用例。

Actually double does more or less the same when calling intValue on it. 实际上,在调用intValue时,double或多或少相同。 In that case it will just represent the maximum integer. 在这种情况下,它只代表最大整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM