Java common type calculation error problem

The problem arises

Today my classmate asked me a question, gave me a piece of code, and asked the running result:
write picture description here
At first glance, it seems like 0.8, which is fine, but why would you ask me such a simple question? It feels like a trap. At this time, I thought Up, the storage of floating-point numbers is not accurate, but I still can't get the answer, so I compile and run it on the IDE.
write picture description here
Result: 0.79999995
is actually not just subtraction, this can also happen in addition, you can refer to here . Isn't it weird, let's explore it together.

reason

Floating-point numbers are stored in memory according to the IEEE754 standard. For a detailed introduction, please Google or Baidu, or refer to this article to get the binary of floating-point numbers .
The reason is that the computer stores data in binary, that is, 0 and 1, and floating-point numbers are no exception. However, some floating-point numbers cannot be stored accurately. After reading the above article, it should be understood that only the powers of 2 are formed. numbers can be stored accurately.

2.0f can be accurately stored in memory, this is the binary storage map of 2.0f:
write picture description here

1.2f cannot be stored accurately:
write picture description here

After conversion, it can be obtained that 1.2f is actually stored in memory. 1.2000000476837158203125E0
Here , it can reflect whether the floating point number is accurate within a certain precision.

Because there are many extra numbers after 1.2f, subtraction is equivalent to calculation:
2.0 - 1.2000000476837158203125E0
its result is that 0.7999999523162842
after reservation, we get the result of our output 0.79999995

How to solve

Such problems can be solved by the BigDecimal class provided in the java.math package.
write picture description here

Also be sure to use the constructor of the BigDecimal class with a parameter of type String
write picture description here

Otherwise, the correct result will not be obtained:
write picture description here

This is because the constructor that takes String as a parameter uses string processing internally, and takes floating-point numbers as parameters, which is implemented internally by double type, so there will still be a problem of precision loss.

Binary to floating point numbers using Java

Get the binary of floating-point numbers This article uses C language to get the binary of floating-point numbers. It is easier to use java than C language because there are methods to call.

You can use the static methods floatToIntBits() or floatToRawIntBits() of the Float class:
write picture description here

The difference between floatToIntBits() and floatToRawIntBits() is that when the argument is NaN, floatToRawIntBits does not compress all bit patterns that encode NaN as a "canonical" NaN value.
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324477543&siteId=291194637