All 'bogus' about the float in PHP you should know
PHP is a weakly typed language. Such a feature must have Seamlessly transparent implicit type conversion, PHP uses zval internally to save any type of value. The structure of zval is as follows (5.2 as an example):
struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount; zend_uchar type; /* active type */ zend_uchar is_ref; };
In the above structure, the actual value itself is the zvalue_value union. :
typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj; } zvalue_value;
Today’s topic, we only focus on two members, lval and dval. We must realize that long lval has an indefinite length depending on the word length of the compiler and OS. It has It may be 32bits or 64bits, and double dval (double precision) is stipulated by IEEE 754. It is fixed length and must be 64bits.
Please remember this, which makes some PHP codes "non-platform independent" "property". Our following discussion, unless otherwise specified, assumes that long is a 64bits
IEEE 754 floating-point counting method. I will not quote it here. If you are interested, you can check it out for yourself. The key One point is that the mantissa of double is stored in 52 bits. Counting the hidden 1 significant bit, the total is 53 bits.
Here, an interesting question arises. Let’s use c code as an example (assuming long is 64bits):
long a = x; assert(a == (long)(double)a);
Excuse me, when the value of a is within what range, can the above code be asserted to be successful? (Leave the answer at the end of the article)
Now we return to the topic, PHP Before executing a script, you first need to read the script and analyze the script. This process also includes zvalizing the literals in the script. For example, for the following script:
<?php $a = 9223372036854775807; //64位有符号数最大值 $b = 9223372036854775808; //最大值+1 var_dump($a); var_dump($b);
Output:
int(9223372036854775807) float(9.22337203685E+18)
In other words, during the lexical analysis phase, PHP will judge whether a literal value exceeds the long table value range of the current system. If not, it will use lval to save it, and zval will be IS_LONG. Otherwise, it will use lval to save it. Just use dval to represent it, zval IS_FLOAT.
We must be careful with any value larger than the largest integer value, because it may cause loss of accuracy:
<?php $a = 9223372036854775807; $b = 9223372036854775808; var_dump($a === ($b - 1));
The output is false.
Now to continue the discussion at the beginning, as mentioned before, PHP's integers may be 32-bit or 64-bit, so it is decided that some codes that can run normally on 64-bit may fail due to invisible Type conversion causes precision loss, causing the code to not run properly on 32-bit systems.
So, we must be wary of this critical value. Fortunately, this critical value has been defined in PHP:
<?php echo PHP_INT_MAX; ?>
Of course, to be safe, we should use strings to save large integers, and use mathematical function libraries such as bcmath to perform calculations.
In addition, there is a key configuration that will make We are confused. This configuration is php.precision. This configuration determines how many significant bits PHP outputs when it outputs a float value.
Finally, let’s look back at the question raised above, which is a long What is the maximum value of an integer to ensure that the precision will not be lost when converted to float and then converted back to long?
For example, for an integer, we know that its binary representation is, 101. Now, let us Shift two bits to the right to become 1.01, discard the implicit significant bit 1 of the high bit, and we get the binary value of 5 stored in the double:
0/*符号位*/ 10000000001/*指数位*/ 0100000000000000000000000000000000000000000000000000
5’s binary representation, which is saved in the The mantissa part, in this case, is converted from double back to long without loss of precision.
We know that double uses 52 bits to represent the mantissa. Counting the implicit first 1, there is a total of 53 bits of precision. Then it can be concluded that if the value of a long integer is less than:
2^53 - 1 == 9007199254740991; //牢记, 我们现在假设是64bits的long
, then this integer will not lose precision when the long->double->long value conversion occurs.
Recommended: "PHP Tutorial"
The above is the detailed content of Things you should know about PHP floating point numbers. For more information, please follow other related articles on the PHP Chinese website!

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
