Home  >  Article  >  Backend Development  >  How are floating point numbers stored in the C compiler?

How are floating point numbers stored in the C compiler?

WBOY
WBOYforward
2023-08-28 11:41:08639browse

How are floating point numbers stored in the C compiler?

In C language, float is the abbreviation of floating point number.

  • Floating point numbers are generally represented in the Institute of Electrical and Electronics Engineers (IEEE) format.

  • The IEEE format uses the sign bit, mantissa, and exponent to represent powers of 2.

  • The sign bit represents the sign of the number: 0 represents a positive value, 1 represents a negative value.

  • The standardized form of the mantissa represented after conversion to binary. After normalizing the mantissa, the most significant bit is always 1.

  • The exponent is an integer stored in unsigned binary format with a positive integer bias added.

  • This ensures that the stored exponent is always positive.

  • The bias is 127 for floats and 1023 for doubles.

Example

The following is a C program that uses C language to round floating point numbers to four decimal places -

Live demonstration p>

#include <stdio.h>
int main(){
   float var = 37.66666;
   printf("%.4f", var);// rounding to four decimal points
   return 0;
}

Output

When the above program is executed, the following results are produced-

37.6667

The following is a C program to round a floating point number to eight decimal places in C language-

Program

Live Demonstration

#include <stdio.h>
int main(){
   float var = 78.67;
   printf("%.8f", var);
   return 0;
}

Output

When the above program is executed, the following results will be produced-

78.66999817

The above is the detailed content of How are floating point numbers stored in the C compiler?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete