S
Sideswipe
I am having an issue with BigDecimal and compareTo() not being
consistent with equals. I have read the documentation and know why
it's behaving that way, but I don't understand why they would
intentionally make compareTo() inconsistent with equals()
More importantly though, When I perform an operation (division in this
case) I get results with different scale even though the MathContext
is identical for all steps. Code below. EXPECTED has a scale of 15 and
result has a scale of 16.
BigDecimal A = new BigDecimal(3.21, MathContext.DECIMAL64);
BigDecimal TEN = new BigDecimal(10, MathContext.DECIMAL64);
BigDecimal EXPECTED = new BigDecimal(.321,MathContext.DECIMAL64);
BigDecimal result = A.divide(TEN,MathContext.DECIMAL64);
System.out.println("equals? " + result.equals(EXPECTED));
System.out.println("compareTo() ? " + result.compareTo(EXPECTED));
equals? false
compareTo() ? 0
How can I control this process so that compareTo() is consistent with
equals()?
Christian Bongiorno
http://christian.bongiorno.org
consistent with equals. I have read the documentation and know why
it's behaving that way, but I don't understand why they would
intentionally make compareTo() inconsistent with equals()
More importantly though, When I perform an operation (division in this
case) I get results with different scale even though the MathContext
is identical for all steps. Code below. EXPECTED has a scale of 15 and
result has a scale of 16.
BigDecimal A = new BigDecimal(3.21, MathContext.DECIMAL64);
BigDecimal TEN = new BigDecimal(10, MathContext.DECIMAL64);
BigDecimal EXPECTED = new BigDecimal(.321,MathContext.DECIMAL64);
BigDecimal result = A.divide(TEN,MathContext.DECIMAL64);
System.out.println("equals? " + result.equals(EXPECTED));
System.out.println("compareTo() ? " + result.compareTo(EXPECTED));
equals? false
compareTo() ? 0
How can I control this process so that compareTo() is consistent with
equals()?
Christian Bongiorno
http://christian.bongiorno.org