C language double and float instance analysis _c language

Source: Internet
Author: User

Decimals are also called real numbers or floating-point numbers. For example, 0.0, 75.0, 4.023, 0.27, and 937.198 are all legal decimals. This is a common representation of decimals, called decimal forms.

In addition to the decimal form, can also be used in exponential form, such as 7.25x102, 0.0368x105, 100.22x10-2 and so on. Any decimal can be expressed in exponential form.

There are also two representations of decimals in the C language. In writing, the decimal form is the same as in mathematics, and the exponential form is different.

In the C language the exponential form of the medium and small number is:

Aen or Aen

A is the tail part, is a decimal number, n is the exponent part, is a decimal integer, E or E is a fixed character whose value is ax10n. For example:

2.1E5 = 2.1x105, where 2.1 is the mantissa and 5 is the exponent.

3.7E-2 = 3.7x10-2, where 3.7 is the mantissa,-2 is the exponent.

0.5E7 = 0.5x107, where 0.5 is the mantissa and 7 is the exponent.

The C language Medium or small number of data types are float or double:float called single-precision floating-point numbers, double is called double-precision floating-point numbers. Unlike integers, the length of a decimal is always fixed, float occupies 4 bytes, and double occupies 8 bytes.

10 is fixed and does not need to be reflected in memory. The positive sign, exponent (n), and Mantissa (a) are variable and require memory space to be represented.

The form of float and double in memory is as follows:

Output float uses the%f control character, the output double uses the%LF control character, as follows:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  float a=128.101;
  float b=0.302f;
  float c=1.23002398f;
  Double d=123;
  Double e = 78.429;
  printf ("a=%f \nb=%f \nc=%f \nd=%lf \ne=%lf\n", A, B, C, D, e);
  System ("pause");
  return 0;
}

Run Result:

a=128.100998

b=0.302000

c=1.230024

d=123.000000

e=78.429000

Description of the code:

1)%f The default to retain six decimal places, less than six to 0, more than six digits by rounding truncated.

2 assigns an integer to a float variable, which is converted to a decimal value.

3 decimal defaults to double type, plus suffix f is float type.

4 due to limited memory, decimal precision is limited, so output a can only get an approximate number.

The above is the C language in double and float in the detailed analysis, comparison, hoping to help learn this piece of content students.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.