Home > Backend Development > C++ > body text

How are floating point numbers stored in the C compiler?

WBOY
Release: 2023-08-28 11:41:08
forward
656 people have browsed it

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;
}
Copy after login

Output

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

37.6667
Copy after login

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;
}
Copy after login

Output

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

78.66999817
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!