Coding Cultivation Series-A Must-Learn to Write Accurate Code


Programmer Nick

1. The computer is not as accurate as imagined

  • Fundamentally speaking, a computer is a machine that uses a potential difference to perform calculation operations. The high potential is 1 and the low potential is 0. Only 0 and 1 are required for calculation. All digital machines follow this principle.
  • Can the binary system represent the decimal system 0.1, 0.1, 0.3, 0.4, 0.6? Can't!

2. Avoid floating-point arithmetic when accurate calculations are required

  • The characteristics of floating-point numbers make it impossible to perform accurate calculations, that is to say, it always has errors.
  • The float in C language can only guarantee the accuracy of 6 decimal places after the decimal point
  • The double can only guarantee 15 digits
  • If you calculate 1/3+1/3, the answer given by the computer is 0.666667 (float type)
  • But it's actually 2/3
  • Therefore, when we need accurate calculations, we should use integers instead of floating-point numbers.

Three, double type is more suitable for accurate calculation than float type

  • The speed of double operation and float operation is almost the same
  • Since the speed difference is not obvious, considering the accuracy, we should use double type arithmetic

Fourth, confirm the integer size

  • In the PC operating system, the size of the int type in a 32-bit system is 4 bytes
  • The integer size is 2 bytes in a 16-bit system
  • The size of int type in 64-bit operating system is 8 bytes
  • Of course, their respective ranges are also different
  • Therefore, we should develop a good habit of confirming the size of the data type before coding, and it is best to mark the size of the data type next to the variable
int number = 7000; //int型大小为4字节

5. The unit of calculation must be clear

  • If the variables in the program have practical meaning, you have to give their units, otherwise who will understand
sum = sum +(num / 1000); //num以元为单位,sum以千元为单位
total = total + (sum / 1000); //sum以千元为单位,total以百万元为单位

Six, try to avoid data type conversion

  • When long bytes are converted to short bytes, precision is lost
  • The way to prevent implicit conversion is to add a prefix that can represent the data type before the variable name, as follows:
double dpi = 3.14159;
long int liNum;
long int liRad;
......
liNUm = dpi * liRad * liRad;
  • After declaring and using these variables, you can discover the possible multiplication or assignment operations between variables of different data types at the first time

Seven, pay attention to the possible nonlinear calculation results

  • The butterfly effect also applies to computer operations
  • The computer is not what we think
  • After repeated calculations of floating-point numbers, they may have deviated from our expectations. These numerical deviations have led to shipwrecks, crashes, and explosions in power stations. . .
  • Therefore, if you need to repeatedly calculate subtle numerical differences in the process of coding, designing program algorithms, and designing system frameworks, please check the results of the calculations many times! ! ! !

8. Summary

There are many details in this article, mostly from the variable aspect. Take a look around, it's actually pretty good.

Guess you like

Origin blog.csdn.net/weixin_43722052/article/details/110957934
Recommended