Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php floating point numbers

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use php floating point numbers, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Php floating-point numbers refer to Float floating-point numbers, which can be defined by syntax such as "$a = 1.234positionb = 1.2e3politic" c = 7eMuth10b = 1x234.567; etc.

This article operating environment: windows7 system, PHP7.4 version, DELL G3 computer

Php Float floating point type

Floating-point types (also known as floating-point float, double-precision double, or real real) can be defined in any of the following syntax:

Formal representation of floating point numbers (underscores are not supported prior to PHP 7.4.0):

LNUM [0-9] + (_ [0-9] +) * DNUM ([0-9] * (_ [0-9] +) * [\.] {LNUM}) | ({LNUM} [\.] [0-9] * (_ [0-9] +) *) EXPONENT_DNUM (({LNUM} | {DNUM}) [eE] [+ -]? {LNUM})

The word length of floating-point numbers is platform-dependent, although the usual maximum value is 1.8e308 and has the precision of 14-digit decimal numbers (64-bit IEEE format).

Warning

The accuracy of floating point numbers

The precision of floating point numbers is limited. Although PHP usually uses the IEEE 754 double-precision format depending on the system, the maximum relative error caused by rounding is 1.11e-16. Non-basic mathematical operations may give larger errors, and the error transfer in composite operations should be taken into account.

In addition, rational numbers that can be accurately represented in decimal, such as 0.1 or 0.7, no matter how many Mantissa are, cannot be accurately represented by the binary used internally, so they cannot be converted to a binary format without losing a little bit of precision. This can lead to confusing results: for example, floor ((0.1-0.7) * 10) usually returns 7 instead of the expected 8, because the internal representation of the result is actually similar to 7.999999999991118.

So never believe that floating-point results are accurate to the last digit, and never compare whether two floating-point numbers are equal. If you do need higher precision, you should use any precision mathematical function or gmp function.

Convert to floating point number

From strings

If the string is numeric or leading numeric then it will resolve to the corresponding float value, otherwise it is converted to zero (0).

From other types

For other types of values, the situation is similar to converting the value to int and then to float. See the "convert to Integer" section for more information.

Note: As certain types have undefined behavior when converting to int, this is also the case when converting to float.

Compare floating point numbers

As stated in the warning message above, it is problematic to compare whether two floating-point numbers are equal due to the internal expression. But there are circuitous ways to compare floating-point values.

To test whether floating-point numbers are equal, use a minimum error value that is only slightly larger than that value. This value, also known as the machine minimum (epsilon) or the smallest unit taking an integer, is the smallest difference that can be accepted in the calculation.

$an and $b are equal in the precision of five decimal places.

NaN

Some mathematical operations produce a result represented by the constant NAN. This result represents a value that is not defined or cannot be expressed in a floating-point operation. The result of any loose or strict comparison of this value with any other value except true is false.

Since NAN represents any different values, NAN should not be compared to other values, including itself, and should be checked with is_nan ().

Thank you for reading this article carefully. I hope the article "how to use php floating point numbers" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report