In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use floating-point numbers in C language. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
Floating-point numbers in memory
Floating-point numbers are stored in memory as follows: symbol bits, exponents, Mantissa
Type symbol bit index Mantissa float1 bit (31st bit) 8 bit (23-30 bit) 23 bit (0-22 bit) double1 bit (63 bit) 11 bit (52-62 bit) 52 bit (0-51 bit)
Note: float and double data are represented in the same way in the computer, but because of the different storage space, they can represent different numerical range and precision respectively.
Floating-point data storage instance
Conversion of floating point numbers
Convert floating point numbers to binary
Representation of binary floating-point numbers by scientific counting
Calculate the value after exponential offset
Note: the offset is required when calculating the index, and the value of the offset is related to the type.
Example: for exponent 6, the offset value is as follows:
Float:127 + 6-> 133
Double:1023 + 6-> 1029
The in-memory float representation of the real number 8.25
1000.01-> 1.00001 * (2 ^ 3)
Symbol bit: 0
Index: 127 + 3-> 130 10000010
Decimal: 00001
The float of 8.25 in memory says:
0 10000010 00001000000000000000000000000-> 0x41040000
Let's take a look at whether 8.25 is represented as 0x41040000 in memory:
# include int main () {float f = 8.25; unsigned int* p = (unsigned int*) & f; printf ("0xX\ n", * p); return 0;}
The following is the output:
Third, the secret of floating point type
First take a look at the scope of the int type and the float type:
Range of int type: [- 2 ^ 31-1]
Range of float type: [- 3.410 ^ 38d3.4 * 10 ^ 38]
This leads to a problem: both int and float take up 4 bytes of memory, so why is float so much larger than int?
The explanation is as follows:
The number of specific numbers that float can represent is the same as that of int
The numbers that can be represented by float are not continuous and there is a gap.
Float is only an approximate representation and cannot be used as an exact number.
Because of the relatively complex memory representation, float is much slower than int
Note: double has the same memory representation as float, so double is also imprecise. Because double takes up more memory, the accuracy of representation is higher than that of float.
Let's take a look at an imprecise sample code of type float:
# include int main () {float f = 3.1415f; float fl = 123456789; printf ("% 0.10f\ n", f); printf ("% 0.10f\ n", fl); return 0;}
The following is the output:
This example represents the last 10 digits of f and fl decimal points. The results show that float is only an approximate representation and cannot be used as an exact number, and there is a gap between the numbers that can be represented by float.
These are all the contents of the article "how to use floating-point numbers in C language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.