0

I have a function:

fun sumUpStrings(strings: List<String>) = strings.map { it.toFloat() }.sum()

and a test:

@Test
fun testSumUpStrings() {
    val strings = listOf(
        "12.2", "7.0", "33.33"
    )

    val result = mapper.sumUpStrings(strings)

    assertThat(result).isEqualTo(12.2 + 7.0 + 33.33)
}

The output of the test is:

expected:<52.53[]> but was:<52.53[0003f]> Expected :52.53[] Actual
:52.53[0003f]

I was expecting the test to succeed. Can someone explain this phenomenon to me?

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • expecting an exact result is not advised with floats https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate – Raildex Nov 16 '21 at 10:27
  • 4
    Does this answer your question? [Why are floating point numbers inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Raildex Nov 16 '21 at 10:28
  • @Raildex it does, thanks! – fweigl Nov 16 '21 at 12:00

0 Answers0