Learn how to apply firewall mechanisms in Linux

Source: Internet
Author: User
Article Title: Learn to apply firewall mechanisms in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   Preface
  
In recent posts and discussions with the QQ group, I found that many netizens have encountered problems because of improper use of single-precision/double-precision values. So I want to talk about this topic.
  
The single-precision and double-precision numeric types first appear in the C language (more common). In the C language, the single-precision type is called the Float type ), as the name implies, data is stored by floating decimal points. These two data types were first generated for scientific computation. He was able to provide scientific computation with sufficient precision to store numbers with high precision requirements. But at the same time, he is completely in line with the concept of numerical values in scientific computing:
  
When we compare the lengths of two sticks, one way is to compare them side by side, and the other way is to separate the lengths. But in fact there are no two identical-length sticks in the world. The length accuracy we measure is limited by the human's visual ability and the accuracy of measurement tools. In this sense, it makes no sense to judge whether the two sticks are the same length, because the result must be False, but we can compare the two of them to be longer or shorter. This example summarizes the design intention and significance of Single-precision/double-precision numeric types.
  
Based on the above understanding, the single-precision/double-precision numeric type is not an accurate numerical type from the very beginning. It only ensures that it is accurate within the precision of its numerical type, accuracy is not guaranteed. For example, if a value of 5.1 is stored in a single-precision or double-precision value, the actual value is 5.100000000001 or 5.09999999999999. We can explain the cause of this phenomenon in two ways:
  
   Simple explanation:
  
You can set its width to 3.2 in the property panel of any control. After Entering the value, you will find that the value is automatically changed to 3.199, no matter how you change it, you cannot enter 3.200 CM, because in fact, what is stored in the computer is not a value in the unit of CM, but a value in the unit of "ti, the ratio between "ti" and CM is a hard-to-be-divided number. Therefore, after you enter the value, the computer automatically converts it to the nearest "ti" value, then switch to centimeter and display it on the property panel. The multiplication and division result is rounded up twice, and the error is displayed. Single-precision/double-precision is similar. In fact, in binary storage, single-precision/double-precision adopts a method similar to similar scores, and such storage cannot be accurate.
  
   In-depth explanation:
  
Let's take a look at the single-precision/double-precision values stored in the digital media. We use the following code to perform an anatomy of the single-precision type:
  
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  
Public Sub floatTest ()
Dim dblVar As Single
  
DblVar = 5.731/8
DblOutput dblVar
  
DblVar = dblVar * 2
DblOutput dblVar
  
DblVar = dblVar * 2
DblOutput dblVar
  
DblVar = dblVar * 2
DblOutput dblVar
  
DblVar = dblVar * 2
DblOutput dblVar
  
DblVar = dblVar * 2
DblOutput dblVar
  
End Sub
  
Public Sub dblOutput (ByVal dblVar As Single)
Dim bytVar (3) As Byte
Dim I As Integer, j As Integer
Dim strVar As String
  
CopyMemory ByVal VarPtr (bytVar (0), ByVal VarPtr (dblVar), 4
StrVar = dblVar &":"
For I = 3 To 0 Step-1
For j = 7 To 0 Step-1
StrVar = strVar & (bytVar (I) And 2 ^ j)/2 ^ j
Next j
StrVar = strVar &""
Next I
Debug. Print strVar
  
End Sub
After running, we get the output result (the output format is high left, low right ):
  
. 716375: 00111111 00110111 01100100 01011010
1.43275: 00111111 10110111 01100100 01011010
2.8655: 01000000 00110111 01100100 01011010
5.731: 01000000 10110111 01100100 01011010
11.462: 01000001 00110111 01100100 01011010
22.924: 01000001 10110111 01100100 01011010
  
Here, we convert the single-precision type into binary data output. here we can see that although these six numbers are completely different, their binary storage is surprisingly similar. We can see the red tag, each time we add 1, in fact, the single-precision data type uses 1st bits starting from the high position as positive and negative mark bits (green), 2nd bits to 9th bits, it is a cross-byte signed byte data. This value determines the direction and number of digits (red) of the decimal point movement. The value ranges from 10th to 32 bits to save an integer (blue) during the storage process, the computer first shifts the input value (multiplication and division 2) until the integer part of the number occupies all 24 digits, then, the number of digits to be moved is written to the floating point (red), and the result after the shift is written to the integer (blue and green). The fractional part is discarded. When the value is evaluated, It is a reverse process. First, the value is calculated based on the positive and negative digits and the integer in the red part (multiplied by the power of 2 ), the final result is the single-precision value we get. The same is true for Double Precision values, but there are more digits.
  
By anatomy of the binary storage format of Single-Precision values, we can clearly see that in fact, the storage of Single-precision/double-precision values must pass multiplication and division, where there must be rounding, if your value is rounded out in Division, the initial values you assign may be different from the values you finally store, with slight differences, it is not contrary to the single-precision/double-precision design goal.
  
When we use a single-precision/double-precision value in the database or VBA code, you may not see the difference from the interface, but in actual storage, the difference is actually there. When you compare the differences, the system simply compares them in binary format, and the tiny differences on the interface cannot be reflected, there is nowhere to hide before the binary comparison, so your equals comparison returns an unexpected False.
  
   Conclusion
  
Through this article, we have introduced the essence and characteristics (advantages and disadvantages) of single-precision/double-precision data types. Through comparison and anatomy, we have learned that single-precision/double-precision actually stores an approximate value, the floating-point feature determines that it can store a very small number or a large number. Its data accuracy is not an absolute value, but a percentage of the stored value, if you store 10 to the power of 100, the error may be 10 to the power of 80. If you store 10 to the power of 100, the error may be 10 to the power of 120. Therefore, single-precision/double-precision data types cannot be compared equally (or associated with databases ).
  
If you need to perform equivalent comparison or association, there are several solutions:
  
1. Use a currency type designed for accuracy.
  
2. Use integer storage to shift data in the code.
  
3. In certain situations, you can use text storage.
Related Article

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.