Trying out floating point arithmetic in Groovy. Have no idea why/how/what groovy is doing behind the scenes to cause these different types of behaviors?
double point2 = 0.2
double point1 = 0.1
double point3 = 0.3
assert point2 + point1 == point3 // false, as expected
| | | | |
0.2 | 0.1 | 0.3
| false
0.30000000000000004
float point2 = 0.2
float point1 = 0.1
float point3 = 0.3
assert point2 + point1 == point3 // false, as expected
| | | | |
0.2 | 0.1 | 0.3
| false
0.30000000447034836
def point2 = 0.2
def point1 = 0.1
def point3 = 0.3
assert point2 + point1 == point3 // this returns true
assert 0.2 + 0.1 == 0.3 // this returns true
I thought it had to do with BigDecimal but then I tried this.
BigDecimal point2 = 0.2
BigDecimal point1 = 0.1
BigDecimal point3 = 0.3
float point4 = 0.4
assert point1 + point3 == point4
| | | | |
0.1 | 0.3 | 0.4
0.4 false
What is causing this behavior?