本文介绍了Java:BigInteger floor和ceil函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java实施RSA攻击,并且我需要计算诸如floor和ceil到BigInteger变量的数学运算.我们知道math.ceil和math.floor仅适用于双变量,您知道我可以通过其他任何方式为BigIntegers计算下限和上限吗?谢谢.

I'm trying to implement an RSA attack in Java and I need to compute math operations like floor and ceil to BigInteger variables. As we know math.ceil and math.floor only apply to double variables, do you know any other way I can compute floor and ceil for BigIntegers? Thank you.

推荐答案

由于BigInteger值是整数,因此我假设您需要除法运算的上限和下限.为此,我建议您使用 BigDecimal 反而.它具有方法divide,该方法采用另一个BigDecimal和 MathContext .在MathContext中,您可以将舍入模式指定为上限或下限(以及其他).

Since BigInteger values are integers, I assume that you want the the ceiling and floor of division operations. For that, I suggest that you use BigDecimal instead. It has a method divide that takes another BigDecimal and a MathContext. In the MathContext, you can specify the rounding mode as ceiling or floor (among others).

如果您需要的全部舍入为零,也可以使用BigDecimal.divideToIntegralValue.

If all you need is rounding toward zero, you can also use BigDecimal.divideToIntegralValue.

这篇关于Java:BigInteger floor和ceil函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:11