Java: BigInteger floor and ceil functions [closed]

回眸只為那壹抹淺笑 提交于 2019-12-11 11:28:59

问题


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.


回答1:


The question does not make sense: floor and ceil round a floating number to an integer. And guess what: BigInteger can only represent Integers. So no rounding required. (Esp. the RSA context emphasize the integer character).




回答2:


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).

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



来源:https://stackoverflow.com/questions/9987155/java-biginteger-floor-and-ceil-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!