C language float

First, the concept of floating point numbers

Also known as fractional or floating-point real number. For example, 0.0,75.0,4.023,0.27, -937.198 are legitimate decimals.

C language used to define float and double keywords decimal, float called single-precision floating-point, double precision floating-called double, long double longer double precision floating point.

(E.g., between 1.0 and 2.0) at any interval there are infinitely many real numbers, the computer can not represent all floating point value within the interval.

Second, the point with the case of memory

Let's test the number of bytes float, double and long double three kinds of memory-intensive floating-point data types.

Example (book71.c)

/*
 * 程序名:book71.c,此程序测试float、double和long double占用内存的字节数
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  printf("sizeof float is %d\n",sizeof(float));
  printf("sizeof double is %d\n",sizeof(double));
  printf("sizeof long double is %d\n",sizeof(long double));
}

running result

Here Insert Picture Description

Third, the floating point precision

Standard C, float type must represent at least six significant digits, and the range is at least 10 ^ -37 ^ + ^ 37 ^ 10 ~.

type type float and double the minimum value in the same range, but must be able to represent at least 10 significant digits.

long double, in order to meet higher than the double precision type. However, C can only guarantee at least the same type of long double type double precision.

Read the above paragraph, it is estimated we are a bit dizzy, integer in the previous section, long occupied by more than int memory, the greater the value of the stored data and an accurate range, however, why the various floats points value of how to store data so vague it? I will not explain why, floating-point storage complex, not yet discussed, first with several programs to test their characteristics.

1, a float test

Example (book73.c)

/*
 * 程序名:book73.c,此程序测试float的特征
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h> 

int main()
{
  float ff2=9.9;   // 测试2位的浮点数
  printf("ff2=%f\n",ff2);
  if (ff2==9.9) printf("ff2==9.9\n");

  float ff5=99.999;   // 测试5位的浮点数
  printf("ff5=%f\n",ff5);
  if (ff5==99.999) printf("ff5==99.999\n");

  float ff6=999.999;   // 测试6位的浮点数
  printf("ff6=%f\n",ff6);
  if (ff6==999.999) printf("ff6==999.999\n");

  float ff7=9999.999;   // 测试7位的浮点数
  printf("ff7=%f\n",ff7);
  if (ff7==9999.999) printf("ff7==9999.999\n");

  float ff8=99999.999;   // 测试8位的浮点数
  printf("ff8=%f\n",ff8);
  if (ff8==99999.999) printf("ff8==99999.999\n");
}

running result

Here Insert Picture Description

Run the program from the two features we can see that the number of float:

1) float data type is expressed by an approximate number, is not accurate, n is erroneous bits after the decimal point, the greater the number of bits of the floating-point number, the greater the error, to 8, when the error is 1, not substantially used.

2) a "==" to compare two integers or characters are equal, however, seems to equal two floating point numbers is not equal.

2, a double test

Example (book74.c)

/*
 * 程序名:book74.c,此程序测试double的特征
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h> 

int main()
{
  double ff2=9.9;   // 测试2位的浮点数
  printf("ff2=%lf\n",ff2);
  if (ff2==9.9) printf("ff2与9.9相等。\n");

  double ff12=999999999.99;   // 测试12位的浮点数
  printf("ff12=%lf\n",ff12);
  if (ff12==999999999.99) printf("ff12与999999999.999相等。\n");

  double ff13=9999999999.99;   // 测试13位的浮点数
  printf("ff13=%lf\n",ff13);
  if (ff13==9999999999.99) printf("ff13与9999999999.999相等。\n");

  double ff14=99999999999.99;   // 测试14位的浮点数
  printf("ff14=%lf\n",ff14);
  if (ff14==99999999999.99) printf("ff14与99999999999.999相等。\n");

  double ff15=999999999999.99;   // 测试15位的浮点数
  printf("ff15=%lf\n",ff15);
  if (ff15==999999999999.99) printf("ff15与999999999999.999相等。\n");

  double ff16=9999999999999.99;   // 测试16位的浮点数
  printf("ff16=%lf\n",ff16);
  if (ff16==9999999999999.99) printf("ff16与9999999999999.999相等。\n");

  double ff17=99999999999999.99;   // 测试17位的浮点数
  printf("ff17=%lf\n",ff17);
  if (ff17==99999999999999.99) printf("ff17与99999999999999.999相等。\n");

  double ff18=999999999999999.99;   // 测试17位的浮点数
  printf("ff18=%lf\n",ff18);
  if (ff18==999999999999999.99) printf("ff17与99999999999999.999相等。\n");
}

running result

Here Insert Picture Description

Run the program from the two features we can see that the number of double:

1) Expression of double data type is an approximation of the number, it is not accurate, n is erroneous bits after the decimal point, the greater the number of bits of the floating-point number, the greater the error, to 17 when the error is 1, not substantially used.

2) "==" double compare two values ​​are equal.

3, the test long double type

Example (book75.c)

/*
 * 程序名:book75.c,此程序测试long double的特征
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h> 

int main()
{
  long double ff2=9.9;   // 测试2位的浮点数
  printf("ff2=%Lf\n",ff2);
  if (ff2==9.9) printf("ff2与9.9相等。\n");

  long double ff12=999999999.99;   // 测试12位的浮点数
  printf("ff12=%Lf\n",ff12);
  if (ff12==999999999.99) printf("ff12与999999999.999相等。\n");

  long double ff13=9999999999.99;   // 测试13位的浮点数
  printf("ff13=%Lf\n",ff13);
  if (ff13==9999999999.99) printf("ff13与9999999999.999相等。\n");

  long double ff14=99999999999.99;   // 测试14位的浮点数
  printf("ff14=%Lf\n",ff14);
  if (ff14==99999999999.99) printf("ff14与99999999999.999相等。\n");

  long double ff15=999999999999.99;   // 测试15位的浮点数
  printf("ff15=%Lf\n",ff15);
  if (ff15==999999999999.99) printf("ff15与999999999999.999相等。\n");

  long double ff16=9999999999999.99;   // 测试16位的浮点数
  printf("ff16=%Lf\n",ff16);
  if (ff16==9999999999999.99) printf("ff16与9999999999999.999相等。\n");

  long double ff17=99999999999999.99;   // 测试17位的浮点数
  printf("ff17=%Lf\n",ff17);
  if (ff17==99999999999999.99) printf("ff17与99999999999999.999相等。\n");

  long double ff18=999999999999999.99;   // 测试17位的浮点数
  printf("ff18=%Lf\n",ff18);
  if (ff18==999999999999999.99) printf("ff17与99999999999999.999相等。\n");
}

running result

Here Insert Picture Description

The test results long double and double the same.

4, test summary

float only express significant figures 6-7 can not use the "==" determines whether the two numbers are equal.

capable of expressing the double-bit valid number 15-16, can be used "==" determines whether the two numbers are equal.

long double double double the amount of memory, but the same expression data precision and double.

In the actual development, the proposed abandoned float, you can only use double, long double time being it is not necessary, but do not know the future of the operating system and compiler if there is improvement in long double.

Fourth, the floating point output

float using% f placeholder.

% lf using double placeholder. The test results prove, double input can not use% f,% f output but can be used, but not recommended% f, because different compilers may differ.

% Lf using long double placeholder noted, L is capitalized.

Floating-point output by default display after the decimal point six.

% Lf using floating point output, the complete output format% m.nlf, the output data specified integer and fractional portions accounted for m bits, n which is the fractional bits. If the length is less than the value m, then fill the spaces left, if the length is greater than the value of m, at the actual number of bits output.

double ff=70001.538;
printf("ff=%lf=\n",ff);       // 输出结果是ff=70001.538000=
printf("ff=%.4lf=\n",ff);     // 输出结果是ff=70001.5380=
printf("ff=%11.4lf=\n",ff);   // 输出结果是ff= 70001.5380=
printf("ff=%8.4lf=\n",ff);    // 输出结果是ff=70001.5380=

Five commonly used library functions

In what follows, I only introduce double, no longer describes the knowledge related to the float and long double two data types.

The following are common floating-point functions, you must master.

double atof(const char *nptr);       // 把字符串nptr转换为double
double fabs(double x);                // 求双精度实数x的绝对值
double pow(double x, double y);      // 求 x 的 y 次幂(次方)
double round(double x);               // double四舍五入
double ceil(double x);                // double向上取整数
double floor(double x);               // double向下取整数
double fmod(double x,double y);      // 求x/y整除后的双精度余数
// 把双精度val分解成整数部分和小数部分,整数部分存放在ip所指的变量中,返回小数部分。
double modf(double val,double *ip);

There are some data to calculate functions such as sine, logarithmic, exponential, etc., the actual development rarely used, we use the time to re-check the information, I not introduced.

Sixth, integer Converting to float

Example (book77.c)

/*
 * 程序名:book77.c,此程序测试整数与浮点数的转换。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  int ii=3;
  int jj=4;
  double dd;

  dd=ii;        // 可以
  printf("dd is %.2lf\n",dd);

  dd=ii/jj;     // 不行
  printf("dd is %.2lf\n",dd);

  dd=(double)ii/jj;  // 可以
  printf("dd is %.2lf\n",dd);
}

running result

Here Insert Picture Description

Of particular note is dd = ii / jj this line of code, dd value 0, not 0.75, a little surprised, therefore, if converted to floating-point integer not sure, plus (double) cast is a good idea. Conversion on data types, in the "C language data type conversion" section in more detail.

Seven, application experience

Float some pit, for example, two floating point precision issues are not equal and, in the actual development, we often use integers instead of floating point, integer is as accurate and efficient.

For example a person's height seven meters Fifth, in meters, 1.75 meters with a floating point number, if in centimeters, with integer is 175.

Long integer value is -9223372036854775808 - 9223372036854775807 effective number is 19, and the double number of effective bits 15-16 was, therefore, a greater number of decimal integer that can be expressed, more practical, less troubles.

Money: 1.75 yuan, 0.01 yuan if the unit is 175, using 0.001 yuan as a unit is 1750, if you want to say more decimals how to do? You This is a dead end.

Code Agricultural Road: high-level programmers do not easily fall into a pit, note that it is not easy, does not, the best way is not to come near the pit.

Eight, scientific notation

In the actual development, we rarely use scientific notation, but it often appears in a computer system, such as floating-point numbers stored in the memory is the way scientific notation, so we still need to learn scientific notation.

Scientific notation is a method of counting. The expressed as a number and a 10 ^ n ^ multiplied form (1≤ | a | <10, n is an integer), this notation is called scientific notation. When we want to write or operation of a larger or smaller and more digits when using scientific notation eliminates waste a lot of space and time.

For example: 51400000000 = 5.14 × 10 ^ 11 ^, a power of the computer 10 is a general expression is E or e, i.e. 51400000000 = 5.14E11 or 5.14e11.

When the number represented using scientific notation, without changing the number of symbols, just change the number of written form only, can easily represent some extremely large or small numbers encountered in everyday life. Such as: speed of light is about 300,000,000 meters / second; the world's population is about: 6,100,000,000, so the display is very inconvenient and the number of writing, to write so eliminating duplicate 0, which is expressed as the form: 6,100,000,000 = 6.1 × 10 ^ 9 ^, i.e. 6.1E9 or 6.1e9.

0.00001 = 1 × 10 ^ -5 ^, i.e. the number smaller than the absolute value 1 may also be expressed in scientific notation is a multiplier 10 ^ -n ^ forms. I.e. 1E-5 or 1e-5.

Using scientific notation% e% E or the output, the output format is complete or% m.ne% m.nE, the output data specified integer and fractional portions accounted for m bits, n which is the fractional bits. If the length is less than the value m, then fill the spaces left, if the length is greater than the value of m, at the actual number of bits output.

Example (book78.c)

/*
 * 程序名:book78.c,此程序测试浮点数据的科学计数法。
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>

int main()
{
  double dd;

  dd=123000000;
  printf("dd is  %.2e\n",dd);

  dd=-123000000;
  printf("dd is %.2e\n",dd);

  dd=0.0000000123;
  printf("dd is  %.2e\n",dd);

  dd=-0.0000000123;
  printf("dd is %.2e\n",dd);
}

running result

Here Insert Picture Description

Nine, Homework

1) Write the sample program, similar to this section book71.c, book73.c, book74.c, book75.c, book77.c, book78.c, compile and run it, programmers write, not to look out for Practice makes perfect, meaning that you have to pay every day.

2) the preparation of sample programs, test floating point assignment consequences of exceeding the range.

3) on a float in memory storage, it is recommended that you go to look at Baidu (search keyword in C floating-point storage), look at the related concepts.

4) preparation of sample programs, test results of the floating-point number is assigned to an integer variable, and think about why.

5) The title work is recommended after studying "C language data type conversion" and then do it, because there are cross-knowledge, to rewrite the commonly used floating-point library functions to achieve its function, the function declaration as follows:

double FABS(const double x);          // 求双精度实数x的绝对值
double ROUND(const double x);         // double四舍五入
double CEIL(const double x);          // double向上取整数
double FLOOR(const double x);         // double向下取整数
// 把双精度val分解成整数部分和小数部分,整数部分存放在ip所指的变量中,返回小数部分。
double MODF(double val,double *ip);  

Ten, Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Guess you like

Origin www.cnblogs.com/wucongzhou/p/12498838.html