Matters needing attention in numerical calculation in PHP
This article mainly talks about "matters needing attention in numerical calculation in PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "points for attention in numerical calculation in PHP".
One: rounded
1.round-rounding floating point numbers
Float round (float $val [, int $precision])
2:floor-rounding (rounding down)
Float floor (float $value)
3.ceil-one-in rounding (rounding up)
Float ceil (float $value)
Pit points: when the value is an integer, for example, 11, then floor (11) = 10, ceil (11) = 12; the problem is obvious, so pay special attention to this when calculating.
Solution: floor (11 + 0.01)
Two: multiplication and division of integers and decimals
This is true in the php world. If you use the +-* / of php to calculate floating-point numbers, you may encounter some incorrect results, such as echo intval (0.58-100). It prints 57 instead of 58, which is actually a bug in which floating-point numbers cannot be accurately represented by the underlying binary of the computer, which is cross-language, and I have the same problem with python. So basically most languages provide accurate computing class libraries or function libraries, such as php has a BC high-precision function library
The solution is to change the integer into a floating-point type 3.5x (float) 100 = 350.
Three: the weirdness of floating point numbers
Pothole: 8.50-8 = 0.500000001 actually the reason is similar to 2.
Solution: round () the result to improve the accuracy.
At this point, I believe you have a deeper understanding of the "matters needing attention in numerical calculation in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!