Generally, the Linux kernel does not support floating-point operations. In the process of data processing, we often encounter the following embarrassment: the data bit length in the calculation process conflicts with the error in the calculation result. for example, for example, an expression like 102448/7*1024 can be expressed as follows in the kernel :...
Generally, the Linux kernel does not support floating-point operations. In the process of data processing, we often encounter the following embarrassment: the data bit length in the calculation process conflicts with the error in the calculation result.
Example: 102448/7*1024
Such expressions can be expressed as follows in the kernel:
Long result = 102448/7*1024;
Of course, as an actual operation, the literal constants in an expression are usually some variables. If the result has a cumulative effect, the above expression will produce a fatal error.
In mathematical expression, the value of 102448/7*1024 is 14986678.
The value of the expression result = 102448/7*1024 is 14986240.
Obviously, this error is huge. how can we adjust it?
We can resort to the mathematical equations:
{A/B * C} = [A/B * C + A % B * C/B]
We will verify:
Result = 102448/7*1024 + 102448% 7*1024/7;
Result = 14986240 + 438
Result = 14986678.
To be continued
Author: "www.475551408.qq.com"