Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

6.6f+1.3f in Java! What is the process?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the process of 6.6f+1.3f! in Java". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how the process of 6.6f+1.3f! in Java is.

The printed result is: 7.8999996. What the heck, is my program a fake program?

Let's change float to double and do it again.

The result changed again: 7.8999998569488525.

Why is that? Why it's not what I expected.

To illustrate this problem, we need to start with 0 and 1 at the bottom of the computer. The computer only knows 0 and 1, so all calculations will eventually be converted to binary calculations.

Float storage principle

CPU means that the floating point number is composed of three parts, the symbol bit (sign), the exponential part (exponent) and the effective part (fraction, mantissa). Among them, float occupies a total of 32 bits, symbol bit, exponential part, and effective part each occupies 1 bit, 8 bits, 23 bits.

For real numbers, the conversion to binary is divided into two parts, the first part is the integer part, and the second part is the decimal part. The integer part of the binary calculation is very familiar to everyone.

Let's look at a fractional part of the calculation process.

Multiply the decimal by 2, take the integer part as the binary value, then multiply the decimal by 2, and then take the integer part to loop back and forth.

You will find that there will be a loop in the above calculation, with a loop body of 1001. So 0.6 to binary to 0.10011001... , 6.6 is converted to binary to 110.10011001. An infinite loop.

So what should the computer do with decimals? People are so smart that they come up with "normalization" and "exponential offset values".

Conventionalization

Conventionalization is that we convert the decimal into a prescriptive form through normalization, similar to the scientific counting method we use, that is, to ensure that there is a valid number before the decimal point.

In binary, it is guaranteed that the integer bit is a 1. Then 110.10011001 will be 1.10100110011001 * 2 ^ 2 after standardization.

Exponential offset value

Refers to the value of the exponential part of a floating-point number, whose value is a specified exponential value plus a fixed value. The fixed value of float is 127. the method of calculation is 2 ^ e-1, where e is the number of bits in the storage exponential part, float is 8 bits, and double is 11 bits. In this case, because it is 2 to the power of 2, the offset value is 127 / 2 / 129, and the conversion to binary is 10000001.

Splicing

As I said earlier, there are three parts that use binary scientific counting to calculate floating point numbers. Symbol bit, exponential part, effective part.

The exponential part is the binary 10000001 of the offset value, and the effective part is the fractional part of the specification form, so why only take the decimal part? Because the integer must be 1, there will be no error if it is removed. Let's take the first 23 places of the decimal, namely 10100110011001100110011, and finally put it together, that is, 010000001101001100110011001100110011. By the same token, we can calculate that the floating point number of 1.3 is 00111111101001100110011001100110.

Thank you for your reading, the above is the content of "what is the 6.6f+1.3f! process in Java". After the study of this article, I believe you are interested in 6.6f+1.3f in Java! What is the process of this problem has a deeper understanding, the specific use of the situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report