C-language real data (floating-point number)

Source: Internet
Author: User

Real data is also known as floating-point numbers or real numbers. In the C language, the real number is only used in decimal. It has two types: decimal decimal form and exponential form.

Representation of a real number

1) Decimal number form
Consists of a digital 0~ 9 and a decimal point. For example: 0.0, 25.0, 5.789, 0.13, 5.0, 300., 267.8230, etc. are all valid real numbers.

Note that there must be a decimal.

2) Exponential form
Consists of a decimal number, an additional code symbol "E" or "E", and a Step code (integer only, can be signed). The general form is:
A E N (A is a decimal number and n is a decimal integer)
Its value is a*10n. Such as:
2.1E5 (equals 2.1*105)
3.7E-2 (equals 3.7*10-2)
0.5E7 (equals 0.5*107)
-2.8E-2 (equals -2.8*10-2)

The following are not valid real numbers:
345 (no decimal point)
E7 (no digits before the order symbol E)
-5 (no order symbol)
53.-e3 (negative position is not correct)
2.7E (no order code)

"Example 3-5" outputs a real number.

  1. #include <stdio.h>
  2. int main(void){
  3. printf("%f\ n ",356.);
  4. printf("%f\ n ",356);
  5. printf("%f\ n ",356.0);
  6. return 0;
  7. }

3) Real number in-memory storage form
Real numbers typically account for 4 bytes (32 bits) of memory space. stored as an exponential. The real number 3.14159 is stored in memory in the following form:


Description

    • The greater the number of bits in the small part, the more effective the number, the higher the accuracy.
    • The more digits the exponent part occupies, the greater the range of values that can be expressed.
Real variables

The real variables are divided into: single-precision (float), double-precision (dual-type) and long double-precision (long double) class three.

The single-precision type in the VC6.0 occupies 4 bytes (32-bit) of memory space, with a value range of 3.4e-38~3.4e+38 and only seven digits of valid digits. The double is a 8-byte (64-bit) memory space with a value range of 1.7e-308~1.7e+308 and a 16-bit valid number.

Type descriptor Number of bits (bytes) valid numbers the range of the number
Float 32 (4) 6~7 10-37~1038
Double 64 (8) 15~16 10-307~10308
Long double 128 (16) 18~19 10-4931~104932

A real variable defines the same formatting and writing rules as an integral type. For example:

    1. float x, y; //x, Y is a single-precision real amount
    2. Double A, b, C; //A,b,c is a double-precision real quantity
Rounding error of real numbers

Since real numbers are made up of a finite number of storage units, the valid numbers available are always limited. The following example.

The rounding error of the "example 3-6" real number.

  1. #include <stdio.h>
  2. int main(void){
  3. float a, b;
  4. A=123456.789e5;
  5. b=a+20;
  6. printf("a=%f\ n", a);
  7. printf("b=%f\ n", b);
  8. return 0;
  9. }

Note: The result of 1.0/3*3 is not equal to 1.

"Example 3-7"

Copy Plain Text New Window
  1. #include <stdio.h>
  2. int main(void){
  3. float a;
  4. Double b;
  5. A=33333.33333;
  6. b=33333.33333333333333;
  7. printf("a=%f\b=%f\ n", a, b);
  8. return 0;
  9. }


As you can see from this example:

    • Because A is a single-precision floating-point type, the number of valid digits is only seven bits. And the integer is five bits, so the decimal two digits after the number is invalid.
    • B is a double type with a valid bit of 16 bits. However, the VC6.0 specifies a maximum of six digits after the decimal, and the remainder is rounded.

C-language real data (floating-point number)

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.