Python (3.8) code:
#!/usr/bin/env python3
from decimal import Decimal
from decimal import getcontext
x = Decimal('0.6666666666666666666666666667')
y = x;
print(getcontext().prec)
print(y)
print(y == x)
y += x; y += x; y += x;
y -= x; y -= x; y -= x;
print(y)
print(y == x)
Python output:
28
0.6666666666666666666666666667
True
0.6666666666666666666666666663
False
Java code:
import java.math.BigDecimal;
public class A
{
public static void main(String[] args)
{
BigDecimal x = new BigDecimal("0.6666666666666666666666666667");
BigDecimal y = new BigDecimal("0.6666666666666666666666666667");
System.out.println(x.precision());
System.out.println(y.precision());
System.out.println(y);
System.out.println(y.equals(x));
y = y.add(x); y = y.add(x); y = y.add(x);
y = y.subtract(x); y = y.subtract(x); y = y.subtract(x);
System.out.println(y);
System.out.println(y.equals(x));
}
}
Java output:
28
28
0.6666666666666666666666666667
true
0.6666666666666666666666666667
true
What would be the way to achieve arbitrary precision in Python? By setting a very large prec?