Double precision values

心不动则不痛 提交于 2020-01-17 03:45:08

问题


Just a day before I participated in the qualification round of Google Code Jam. This is my first experience of such an online coding contest. It was really fun.

There were three problems given of which i was able to solve two. But on one of the problems I was asked to work with values that are really huge. I am a Java guy and I thought I would go for double variable. Unfortunately, the precision of double also was not enough. Moreover, I attended this during the closing stage, I was not having the time to dig much into it (plus solving 1 is enough to qualify to the next stage).

My question is this, How to have a precision mechanism that is greater than double. My coding experience is in Java, so it would be great if you could please answer in that lines.

Thanks


回答1:


Java has BigDecimall for arbitrary-precision arithmetic - but it's much, much slower than using double.

It's also possible that the problem in question was supposed to be soved by using algebraic transformations and e.g. work with logarithms.




回答2:


If the problem requires integers you can use BigInteger.

Also, long is slightly better than double for integers, with 63 bits compared to 53 bits of precision (assuming positive numbers).




回答3:


You can use arbitrary precision numbers, such as BigDecimal - it's slower but as precise as you specify.



来源:https://stackoverflow.com/questions/2798830/double-precision-values

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