


Detailed explanation of floating point (float) and integer (integer) data types in php_PHP tutorial
The article analyzes the usage differences between float and integer data types in PHP and the insufficient data length in that case.
The value can only be True or False. When other types are converted to boolean types, the following values are considered FALSE:
the Boolean value FALSE itself
the integer value 0 (zero)
the floating point value 0.0 (zero)
Empty string, and string "0"
Array containing no elements
An object that does not include any member variables (only applicable to PHP 4.0)
Special type NULL (including variables that have not been set)
SimpleXML object generated from an XML document without any tags
All other values are considered TRUE (including any resources).
integer data type:
Integer values can be represented in decimal, hexadecimal or octal notation, preceded by an optional sign (- or +).
Octal means 0 (zero) must be added before the number, and hexadecimal means 0x must be added before the number.
The word size of integers is platform-dependent, although the usual maximum is about two billion (32-bit signed). PHP does not support unsigned integers. The word length of an Integer value can be represented by the constant PHP_INT_SIZE. Since PHP 4.4.0 and PHP 5.0.5, the maximum value can be represented by the constant PHP_INT_MAX.
If a given number exceeds the range of integer, it will be interpreted as float. Similarly, if the result of the operation exceeds the range of integer, float will also be returned.
There is no integer division operator in PHP. 1/2 yields float 0.5. You can always discard the fractional part, or use the round() function.
To explicitly convert a value to an integer, use (int) or (integer) cast. In most cases, however, casting is not necessary because when an operator, function, or flow control requires an integer parameter, the value is automatically converted. You can also use the function intval() to convert a value to an integer type.
Converting from a Boolean value, FALSE will produce 0 (zero), TRUE will produce 1 (one).
Converting from a floating point number. When converting from a floating point number to an integer, rounding towards zero is performed. If the floating point number is outside the integer range (usually +/- 2.15e+9 = 2^31), the result is undefined because there is not enough precision for the floating point number to give an exact integer result. There is no warning in this case, not even any notification!
as
Integer type refers to a number in the set {..., -2, -1, 0, 1, 2, ...}.
Example of defining integer type:
$var_int = 12345;
Integer values can be specified in decimal, hexadecimal or octal notation. If octal notation is used, 0 (zero) must be added before the number. If hexadecimal notation is used, 0x must be added before the number:
$var_int = 0123; // Octal number (equal to decimal 83)
$var_int2 = 0x1A; // Hexadecimal number (equal to decimal 26)
Tip: Integer data does not require single quotes or double quotes, otherwise it will be defined as a string type.
Integer overflow
If a given number exceeds the range of integer, it will be interpreted as float type. Similarly, if the result of the operation exceeds the range of integer, float will also be returned.
The range of integers is platform-dependent, usually the maximum value is about two billion (32-bit signed).
float data type
The word length of floating point numbers is platform-dependent, although typically the maximum value is 1.8e308 with a precision of 14 decimal digits (64-bit IEEE format).
Apparently simple decimal fractions like 0.1 or 0.7 cannot be converted to the internal binary format without losing a bit of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will often return 7 instead of the expected 8 because the internal representation of the result is something like 7.9.
This is related to the fact that it is impossible to express certain decimal fractions accurately with a finite number of digits. For example, 1/3 in decimal becomes 0.3.
float, it has 1 sign bit, 8 exponent bits and 24 significant digits (only 23 bits are saved). Of course, the binary in binary32 just now indicates that it is saved in binary form. The picture below shows a float in In-memory representation.
The 31st bit is the sign bit, 23~30 are the exponent bits, and 0~22 are the fraction bits. There is also a hidden bit in the significant number, which is always 1. So the significant bits That part is always 1.xxxxxxx...(23 x). Another thing to pay attention to is the representation of the exponent. In IEEE754, it is stipulated that it is expressed by offset exponent, which means that the number in the exponent bit is subtracted The number after 127 represents the final exponent. Compare the exponent part in the above figure is 01111100, which is converted to decimal number 124, and then subtract 127, the result is -3, that is to say, the exponent part is 2-3=1/8 =0.125. So what about the significant part? After adding the hidden bits, it is expressed as 1.01000000000000000000000=1+(1*2-2)=5/4=1.25, so the number represented above is 1/8 * 5/4 = 0.15625 .
The maximum and minimum values that the index can represent are 127 and -126. Logically speaking, 8 bits should be able to represent between -128~127. The purpose of setting it to -126 is to prevent the smallest number (1/2-126) from not being Overflow (this is a bit difficult to understand, this is what the document says). 127 and -126 here are expressed as exponents of 2, so what should they be when expressed as exponents of 10? We know that the function y=10x The inverse function is x = log10y. Of course, 10 here can be followed by any other number. So 2127=10x => So there is an exponent range of -37~38. So what about 7 significant figures? The same reason is log2(24) = 7.22, which means that 24 binary significant digits are equivalent to the order of magnitude of 107, which is 7 significant figures. . So what is the maximum number that can be represented by a floating point number? We know that the maximum exponent is 127. The maximum significant number is that all bits are 1, so this number should be
1.11111111111111111111111*2127 ≈ 3.4028234 * 1038 .
float type
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
What you need to pay attention to when using PHP's float type is that there is a problem with the accuracy of PHP's float type. If you need high-precision mathematical calculations, you can use the arbitrary precision math functions series and gmp series of functions provided by PHP. Also, don't try to compare float type variables.
Note: The word length of floating point numbers is platform dependent, although typically the maximum is 1.8e308 with 14 decimal digits of precision (64-bit IEEE format)
Summary:
The length of float depends on the platform. Usually the maximum value is 1.8e308 and has a precision of 14 decimal digits. If a given number exceeds the range of integer, it will be interpreted as float type, the range of integer and platform Relevant, usually the maximum value is about 2 billion (32-bit signed). The advantage of page alignment is that if the result of the operation exceeds the integer range, a float will also be returned.

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
