A detailed _php tutorial on floating-point (float) and integer (integer) data types in PHP

Source: Internet
Author: User
Tags integer division type null
This article analyzes the usage differences between floating-point (float) and integer (integer) data types in PHP and, in that case, insufficient data lengths.

The value can only be true or false, and when other types are converted to Boolean, the following values are considered FALSE:

The Boolean value of FALSE itself
The integer value 0 (0)
The floating-point value 0.0 (0)
An empty string, and the string "0"
An array that does not include any elements
Objects that do not include any member variables (PHP 4.0 only applies)
Special type NULL (including variables that have not been set)
SimpleXML object generated from an XML document without any tags (tags)
All other values are considered TRUE (including any resources).


Integer data type:


Integer values can be expressed in decimal, hexadecimal, or octal, preceded by an optional symbol (-or +).

Octal indicates that the number must be preceded by 0 (0), and hexadecimal indicates that the number must be preceded by 0x.

The word length of an integer is related to the platform, although the usual maximum value is approximately 2 billion (32-bit signed). PHP does not support unsigned integers. The length of the integer value can be expressed as a constant php_int_size, since PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed as a constant php_int_max.

If a given number exceeds the range of integers, it will be interpreted as float. Similarly, float is returned if the result of the operation is outside the integer range.

Operators that do not have an integer division in PHP. 1/2 produces a float of 0.5. You can always discard fractional parts, or use the round () function.

To explicitly convert a value to an integer, cast with (int) or (integer). However, in most cases there is no need to cast, because when an operator, function, or process control requires an integer parameter, the value is automatically converted. You can also convert a value to an integral type by using the function intval ().

From a Boolean value conversion, FALSE will yield 0 (0), and TRUE will produce 1 (one).
When converted from floating-point numbers to integers, 0 is rounded. If the floating-point number is outside the integer range (typically +/-2.15e+9 = 2^31), the result is indeterminate because there is not enough precision to give the floating-point number an exact integer result. In this case there is no warning, not even any notice!

Such as

Integer refers to {...,-2,-1, 0, 1, 2, ...} A number in the collection.

Example of defining an integral type:

$var _int = 12345;
Integer values can be specified in decimal, hexadecimal, or octal notation, and if the octal notation is used, the number must be preceded by 0 (0), which must be preceded by a hexadecimal symbol of 0x:

$var _int = 0123; Octal number (equal to 83 in decimal)
$var _int2 = 0x1A; Hexadecimal number (equals 26 in decimal)
Tip: Integer data does not require single or double quotes, otherwise it is defined as a string type.

Integer overflow
If a given number exceeds the range of integers, it will be interpreted as a float type. Similarly, float is returned if the result of the operation is outside the integer range.

The range of integers is related to the platform, usually the maximum value is approximately 2 billion (32-bit signed).

float data type


The length of the floating-point number is relative to the platform, although the usual maximum value is 1.8e308 and has a precision of 14-bit decimal digits (64-bit IEEE format).

It is obvious that a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9.

This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3.

Float, he has 1 sign bits, 8 exponent bits, and 24 significant digits (only 23 bits are saved). Of course, the binary in the binary32 indicated that he was saved in binary form. is a float in memory representation.


The 31st bit is the symbol (sign) bit, 23~30 is the exponent (exponent) bit, and the 0~22 is the active number (fraction decimal) bit. There is also a hidden bit in the active number, which is always 1. So the part of the effective digit is always 1.xxxxxxx ... (23 x). Another area to be aware of is the exponential representation, which is stated in the IEEE754 by means of an offset exponent, meaning that the number in the digits minus 127 is the final exponent. Compare the exponent portion of the above figure to 01111100 and convert it to a decimal number of 124. Then subtract 127, the result is-3, which means that the exponent part is 2-3=1/8=0.125. What about the number of effective parts? Plus the hidden bit is represented as 1.01000000000000000000000=1+ (1*2-2) =5/4=1.25, so the number represented above is 1/8 * 5/4 = 0.15625.


The index can represent the maximum and minimum classification is 127 and 126, supposedly 8 bits should be able to represent between -128~127, set to 126 is to prevent the minimum number (1/2-126) not overflow (this is a bit difficult to understand, as the document says). The 127,-here 126 is expressed as a 2 index, so how much should it be when it is represented as an exponent of 10? We know that the inverse function of the function y=10x is x = log10y, of course here the 10 can be any other number after that. So 2127=10x x = log2 (127) = 38.23 is approximately equal to, log2 (-126) =-37.99 is approximately equal to 37, by which there is an exponential range of -37~38. What about 7 valid digits? The same is true for log2 (24) = 7.22, which means that 24-bit binary valid bits are equivalent to 107 orders of magnitude, which is 7 digits of a valid number. So what is the maximum number of floating-point numbers that can be represented? We know that the index is 127 maximum, the most effective number is that each is 1, so this number should be

1.11111111111111111111111*2127≈3.4028234 * 1038.


Float type


$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

The type of float used in PHP needs to be noted: the accuracy of the float type of PHP is somewhat problematic. If you need high-precision mathematical calculations, you can use the dedicated math functions provided by PHP arbitrary precision math Functions series and GMP series functions. There is no attempt to compare variables of type float.

Note: The word length of the floating-point number is relative to the platform, although the usual maximum value is 1.8e308 and has a precision of 14-bit decimal digits (64-bit IEEE format)


Summarize:
The length of float and the platform have a phase, usually the maximum value is 1.8e308 and has 14 decimal digits precision, if given a number beyond the range of integers, will be interpreted as a float type, the range of integers and platform, usually the maximum is about 2 billion (32 Bit signed) the benefit of the whole page is that it also returns float if the result of the operation is outside the integer range.

http://www.bkjia.com/PHPjc/629172.html www.bkjia.com true http://www.bkjia.com/PHPjc/629172.html techarticle This article analyzes the usage differences between floating-point (float) and integer (integer) data types in PHP and, in that case, insufficient data lengths. The value can only be true or False when other ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.