views:

112

answers:

4

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  A: 

BigDecimal?

zim2001
+4  A: 

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.

Michael Borgwardt
+1  A: 

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

mdma
+2  A: 

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

starblue
Yah.. int -> long -> BigInteger, float -> double -> BigDecimal
bwawok