precision of float

First of all, Baidu's official explanation is:

float has 4 bytes, including a sign bit, an 8-bit binary exponent, and a 23-bit mantissa. Since the high-order bit of the mantissa is always 1, it is not stored as a number.

2^23 = 8388608, a total of seven digits, which means that there can be at most seven significant figures. It should be noted that the seven digits are all digits including integer digits. For example, if there are three digits before the decimal point, it can only be accurate to four digits after the decimal point.

float a=0.123456f;

float b=789.123456f;

 The results of printing a and b are 0.123456 and 789.1235 respectively

Floating-point numbers are stored in scientific notation in memory, and the integer part is always an implied "1", because it is unchanged, so it cannot affect the accuracy. So we often say that float can be accurate to six decimal places, which is only true when the integer bit is 1.

Guess you like

Origin blog.csdn.net/u011175163/article/details/129176034