Question

I understand that double might not be precise enough. However, I couldn't find a way to do so using BigDecimal. I tried

while(Math.log10(Math.abs(a.subtract(new BigDecimal(1.41421356237309504880168872420969807856967187537694))) >= -50 || Math.log10(Math.abs(b.subtract(new BigDecimal(1.41421356237309504880168872420969807856967187537694)))) >= -50 )
{
    \\ stuff goes here
}

but it said I can't use log 10 or absolute value for BigDecimals.

Was it helpful?

Solution

new BigDecimal(1.41421356237309504880168872420969807856967187537694)

The 1.414... there is a double literal, so it will be truncated to double precision before the program is even run. You will need to pass it as a string:

new BigDecimal("1.41421356237309504880168872420969807856967187537694")

As for the comparison, you should be able to use something along the lines of:

if(a.subtract(new BigDecimal("1.41421356237309504880168872420969807856967187537694")).abs().scaleByPowerOfTen(50).doubleValue() < 1 || ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top