I am working on some code that calculates points on a 2 axis graph and I noticed that when the X or Y coordinate was value zero it slowed down. I spit out some debug and found that the scale of my coordinate value was continually growing. I thought passing the math context would make the return bigdecimal not be able to have a larger scale. I was hoping to limit the scale to my math context precision. This code shows what I am seeing.
MathContext mc = new MathContext(32);
BigDecimal xPos = new BigDecimal("0.0", mc);
for (int i = 0; i < 8; i++)
{
System.out.println("" + xPos + " scale=" + xPos.scale());
xPos = xPos.multiply(xPos, mc);
}
The scale continues to increase with each iteration. Am I incorrect in thinking that because I pass multiply a math context that it should limit the precision of the return? Thanks for reading.
It's all explained in the BigDecimal API.
Operation Preferred Scale of Result Add max(addend.scale(), augend.scale()) Subtract max(minuend.scale(), subtrahend.scale()) Multiply multiplier.scale() + multiplicand.scale() Divide dividend.scale() - divisor.scale()
And it makes sense in a logarithmic sort of way.