P. Ekouaghe
P. Ekouaghe

Reputation: 224

Why is a double primitive a BigDecimal in groovy

I call an overloaded method (assertThat) which has one signature with a BigDecimal parameter and another one with double primitive parameter. When I launch this snippet in groovy, it calls the one with BigDecimal parameter when I was expecting the double primitive parameter one to be called.

double[] erreur = Seg.erreur(xtab, ytab, 0, 2)
Assertions.assertThat(erreur[1]).isEqualTo(-0.3333333333333333)

Can someone explain me why ? Thanks in advance.

Upvotes: 2

Views: 297

Answers (2)

Will
Will

Reputation: 14539

Your isEqualsTo() is passing a BigDecimal as parameter, whereas your assertThat() is passing a double. Just add a d at the end of that -0.3333333333333333 and it should work:

import static org.assertj.core.api.Assertions.assertThat

class Doubles extends GroovyTestCase {

  void testAssertions() {
    double[] erreur = [0.1, -0.3333333333333333, 0.3]
    assertThat(erreur[1]).isEqualTo(-0.3333333333333333d)
  }

}

Upvotes: 0

jalopaba
jalopaba

Reputation: 8129

By default, a decimal number in groovy is a BigDecimal. If you want it to be a double, you should use the suffix D or d:

From Number type suffixes in the docs:

assert 123.45 == new BigDecimal('123.45') // default BigDecimal type used
assert 1.200065D == new Double('1.200065')

Upvotes: 1

Related Questions