Advanced C language: 3. The secret of floating-point numbers

Floating point numbers are stored in memory as:

            Sign bit, exponent, mantissa.

The representation of float and double types of data in the computer is the same, but due to the different memory space occupied, the numerical range and precision that they can represent respectively are different.

Conversion of floating point numbers:

    1. Convert floating point numbers to binary;

    2. Use scientific notation to represent binary floating point numbers;

    3. Calculate the exponent offset value.

Note: When calculating the exponent, you need to add an offset, and the value of the offset is related to the type.

float type: 127 + 6 -> 133

double type: 1023 + 6 -> 1029

Example: In-memory float representation of the real number 8.25:

The binary representation of 8.25: 1000.01 -> 1.00001*(2^3)

                            Sign bit: 0

                            Index: 127+3=130 = 10000010;

                            Decimal: 00001

So, 8.25 float in memory is represented as:

0 100 0001 0 000 0100 0000 0000 0000 0000 -> 0x41040000

Run the following code to verify the result:

#include<stdio.h>

intmain()
{
    float a = 8.25;
    
    unsigned int* p = (unsigned int *)&a;
    
    printf("0x%08X\n", *p);
    
	return 0;	
}
delphi@delphi-vm:~/will$ ./a.out
0x41040000

The range of int type [-2^31, 2^31-1]

Range of float type [-3.4*10^38, 3.4*10^38]

Both int and float occupy four bytes, why float is much larger than the range represented by int?

The reason is that float can represent the same number of specific data as int, but the numbers that float can represent are not continuous and there are gaps, so 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.

It should be noted that both double and float have the same memory representation, but because double occupies more memory, the precision that can be represented is higher than float.

Example: Observe the following code and judge the output:

#include<stdio.h>
intmain()
{float a = 3.1415f;
float b = 123456789;
printf("%0.10f\n", a);
printf("%0.10f\n", b);	
			
		


	return 0;
}

The result of compiling and running under linux Gcc is:

delphi@delphi-vm:~/will$ ./a.out
3.1414999962
123456792.0000000000

The output value is not the same as the original value, just an approximate representation.

So what we need to pay attention to is that because the memory representation of floating-point numbers and integer types is different, and the memory representation of floating-point numbers is more complex, the floating-point type can represent a larger range of data in appearance, but it is not accurate and runs faster. slow.



Guess you like

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