In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to store floating point numbers in C language, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
In C, there are two types of floating-point numbers: 32-bit float and 64-bit double, while values stored in computers are represented by binary scientific counting (that is, cardinality 2).
For example, 100=1100100B=1.1001B*26123.456=1111011.0111010010111100011010100111111011111001110111B=1.1110110111010010111100011010100111111011111001110111B*26
Since the cardinality is fixed at 2 and the integer part of the Mantissa is fixed at 1, they can be omitted during storage, and only the other three pieces of information need to be stored: positive and negative sign + exponent + decimal part of Mantissa.
Float: symbol 1, index 8, Mantissa 23
Double: symbol 1, index 11, Mantissa 52
However, the index is also positive or negative, so an offset is added to indicate it when storing. Float uses 8 bits to represent the index, and the offset is 127; double uses 11 bits, and the offset is 1023 to see how 123.456 is stored in float: because it is a positive number, the symbol bit is 0; the index is 6, plus the offset 127.The first 23 places of the decimal part of the Mantissa is 11101101110100101111001 (the 24th bit is 1, so it needs carry). The 32-digit splicing is 0 10000101 111010101010101011001 Bene1123477881.
Let's look at a pure decimal storage, such as 0.00123456, and deduce it backwards this time.
What is stored in the computer is 983683318, and the conversion to binary is 0 01110101 01000011101000011110110 B. The first 0 indicates a positive value; the 8-digit 01110101B=117 next to it indicates the index (117127) =-10; the last 23 digits represent the decimal part of the Mantissa, preceded by 1 of the integer part, and then moved 23 digits to the left is 1 0100001110101000011110110 Benz10604790. Then the original value is 10604790 to the right 23 bits, and then left to move-10 bits, that is, 10604790 to the right 33 bits. And 233-8589934592. So the original value is 10604790max 8589934592 = 0.0012345600 407571, which shows that the first 10 decimal places are correct and the accuracy is OK.
Finally, try 123.456 of double.
The binary system is: 010000000101 111011011100101111Power0001101010011111101111100111011110111111011111111011111101111110101111110101111111010111110101111101011111010111110101111101011111010111110101111101011111101111111010111111010111111110111111110101111110101111110101111110101111110101111110101111110101111110101111110101111110101111111101111111010111111010111111010111111010111111010111111 The first 0 represents a positive value; the exponential bit is 10000000101B=1029, indicating that the index is (1029-1023) = 6; finally, there is a 52-bit Mantissa, preceded by 1 of the integer part, and after moving 52 bits to the left, 1 111011010100111100011010100111111011111111118687443681197687. Therefore, the original value is 8687443681197687 to the right, and then to the left, that is, 46 bits to the right, and 246 to 70368744177664.
8687443681197687According 70368744177664123.456
On how to store floating-point numbers in C language is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.