Detailed explanation of floating point (float) and integer (integer) data types in php_PHP tutorial

WBOY
Release: 2016-07-13 17:13:35
Original
1271 people have browsed it

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629172.htmlTechArticleThe article analyzes the usage differences between floating point (float) and integer (integer) data types in PHP and In that case there will be insufficient data length. The value can only be True or False, when other...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!