kschmit90
kschmit90

Reputation: 538

Why does Groovy perform floating point arithmetic this way?

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?

Upvotes: 5

Views: 614

Answers (1)

cfrick
cfrick

Reputation: 37063

your def:s there are BigDecimals

groovy:000> p1 = 0.1
===> 0.1
groovy:000> p1.getClass()
===> class java.math.BigDecimal

And equals fails for comparsion between BigDecimal and the native float/double

groovy:000> p1.equals(0.1f)
===> false
groovy:000> p1.equals(0.1)
===> true
groovy:000> p1==0.1f
===> false
groovy:000> p1==0.1
===> true

Not sure yet, why == works for [Dd]ouble.

groovy:000> p1.equals(0.1d)
===> false
groovy:000> p1==0.1d
===> true

My guess would be, that it's burried in DefaultTypeTransformation.compareToWithEqualityCheck. As both sides are Number:s.

Upvotes: 5

Related Questions