Northeast Forestry University Environmental Science
introduction
Expression analysis is one of the most basic functions such as software such as programming tools and spreadsheets. Different software follow certain arithmetic rules in parsing expressions. In different software, these computational rules are slightly different, resulting in different calculations. . In practice, the author found that VB and Excel were in handling the common operation of "continuous", and the rules followed, and there were unreasonable factors. This article tries to provide a more reasonable arithmetic mode by discussing, and provides guidance for the preparation of an expression parsing class.
problem analysis
The following table lists the results obtained when VB6.0 and Excel2003 are processed by the same expression about the multiplication.
Table 1 Differences between VB6.0 and Excel2003 to perform the multiplication operation
Expression VB calculation results Excel calculation results 2 ^ -2 ^ -2 .840896415253715 16 -2 ^ 2 -4 4 -2 ^ -2 ^ -2 -.840896415253715 16 2 ^ -3 ^ 4 ^ 7 0 5.16988E-26
I have been analyzed, and it is considered that the main factors caused by the results difference are the order of operational grade and continuous multiplication of the single-grade operator "-". The detailed analysis is as follows:
1. For expression 2 ^ -2 ^ -2, the calculation step of VB is 2 ^ (- 2 ^ -2) = .840896415253715, then its calculation order is from right to left, and Excel strictly follows the left To the right binding order. If we understand 2 ^ -2 ^ -2 as
So, it is obvious that the calculation results of VB are reasonable, but is not VB from right to left when processing consecutive multiplication? the answer is negative. When calculating 2 ^ 3 ^ 4, the result of VB is 4096 instead of 2.41785163922926e 24, this shows that VB does not understand 2 ^ 3 ^ 4 as but understanding, is from left to right binding order . Obviously, the VB has no convincing basis when the multiplication is handled, and there is no convincing basis and there is unreasonable.
2. For expression -2 ^ 2, the calculation process is equivalent to - (2 ^ 2) = - 4, according to the description of the "operator priority order" in the Microsoft MSDN 6.0 Simplified Chinese version VB Help System, Index The operator (^) is higher than the negative operator (-). This seems that VB will be reasonable to obtain such a law in such a law. For Excel, its calculation process is equivalent to (-2) ^ 2, according to the narrative of the Excel 2003 version of the help document, the symbol (-) is higher than the power (^) (Note: the Chinese name of the two software operators is slightly Differences), then this seems that Excel's operation is also reasonable. However, Excel places a certain issue in the "(negative)" priority before the "^" operator. For example, the result of expressing 4-2 ^ 2, 4-2 ^ 2, 4 - 2 ^ 2, 4- ... - (n -) 2 ^ 2 is 0, which is in accordance with the above method People cannot explain smoothly.
3. Due to the above two reasons, the calculation results of expression -2 ^ -2 ^ -2 are caused.
4. For expression 2 ^ -3 ^ 4 ^ 7, the VB calculation result is 0, which is unable to explain anyway. The origin about this result is still to be further studied.
Expression resolution law suggestion
Given the difference between the results of VB and Excel on the results of the multiplier operation, it is recommended to follow the following principles when writing expression parses:
1. It is recommended to precede the priority of the Power Operator "^" as for the single-purpose negative operator "-".
2. In the combination of the multiplication, it should be unified, it is recommended to use the combination of left to right.
Welcome everyone to criticize.