This question has already been answered:

Instead of 27.0, Math.pow (3.0,3.0) returns 26.999999999999993. What is the problem?

Reported as a duplicate by members of Pavel Mayorov , iksuy , Kromster , Nofate ♦ java Oct 13 '16 at 10:59 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 2
    IEEE 754 error - floating point binary calculations - vikttur

1 answer 1

This is not a problem, but a completely acceptable behavior. The fact is that the set of real numbers is not only infinite, but also continuous, therefore, no matter how many bits we take, we will inevitably encounter numbers that do not have an exact representation.

Therefore, you get a fairly accurate answer, which is close to 27.0

  • I do not agree. This is a problem that needs to be taken into account and how to get around it. This is okay, but what about accountants who have a balance because of such “acceptable behavior”? - vikttur
  • Each solution has its own limits of applicability and limitations. Numbers with a floating comma allow us to represent a sufficiently large subset of real numbers, from 10 ^ –308 to 10 ^ 308, but for this you have to pay with precision. If you have a purely accounting problem in which the numbers are strictly decimal, with two decimal places, then, obviously, this solution does not suit you and you need to use another. - akashihi
  • I commented on your answer. I do not have such a task, but I wrote about the standard in the first comment. - vikttur
  • Why then pow (9.0,1.5) returns 27.0? - Peter
  • habrahabr.ru/post/112953 On available HabrĂ© painted. In short: an error may or may not appear. Not by chance, depends on the values ​​and operations on them - vikttur