Float and integer data types in php

Source: Internet
Author: User
Tags constant mathematical functions type null valid

The value can only be True or False. When other types are converted to the boolean type, the following values are considered FALSE:

The Boolean value is FALSE.
The integer value 0 (0)
The floating point value is 0.0 (0)
Empty string and string "0"
Array that does not contain any elements
Objects that do not include any member variables (applicable only to PHP 4.0)
Special type NULL (including unset variables)
SimpleXML object generated from an XML document without any tags
All other values are considered to be TRUE (including any resources ).


Integer data type:


The integer value can be expressed in decimal, hexadecimal, or octal format. You can add an optional symbol (-or +) to the front ).

The octal value indicates that 0 (zero) must be added before the number, and the hexadecimal value indicates that 0x must be added before the number.

The length of the integer is related to the platform, although the maximum value is usually about 2 billion (32-bit signed ). PHP does not support unsigned integers. The length of the Integer value can be expressed by the constant PHP_INT_SIZE. After PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed by the constant PHP_INT_MAX.

If a given number exceeds the integer range, it is interpreted as float. Similarly, if the execution result exceeds the integer range, float is returned.

PHP does not have the division operator. 1/2 generates 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) to force the conversion. However, in most cases, no forced conversion is required, because the value is automatically converted when an integer parameter is required for the operator, function, or flow control. You can also convert a value to an integer using the intval () function.

To convert values from a Boolean value, FALSE will generate 0 (zero), and TRUE will generate 1 (one ).
To convert from a floating point number to an integer, it is rounded to zero. If the floating point number exceeds the integer range (usually +/-2.15e + 9 = 2 ^ 31), the result is uncertain, because there is not enough precision to make the floating point number give an exact integer result. In this case, there is no warning or even no notification!

For example

Integer refers to a number in the {...,-2,-1, 0, 1, 2,...} set.

Example of defining an integer:

$ Var_int = 12345;
The integer value can be specified in decimal, hexadecimal, or octal notation. If octal notation is used, 0 (zero) must be added before the number, and 0x must be added before the hexadecimal notation:

$ Var_int = 0123; // Number of octal bytes (equal to 83 in decimal format)
$ Var_int2 = 0x1A; // hexadecimal number (equal to 26 in decimal format)
Tip: no single or double quotation marks are required for integer data. Otherwise, the data is defined as a string.

Integer overflow
If a given number exceeds the integer range, it is interpreted as a float type. Similarly, if the execution result exceeds the integer range, float is returned.

The range of integer values is related to the platform. Generally, the maximum value is about 2 billion (32-bit signed ).

 

Float data type


The length of the floating point is related to the platform, although the maximum value is usually 1.8e308 with 14-bit decimal digits (64-bit IEEE format ).

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

This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. For example, the value of decimal 1/3 is 0.3.

Float, which has 1 symbol bit, 8 exponent bits, and 24 valid bits (only 23 bits are saved ). of course, binary in binary32 just now indicates that it is saved in binary format. the following figure shows a float in the memory.


The third digit is the sign (sign), 23 ~ 30 is the exponent bit, 0 ~ 22 is the fraction decimal digit. there is also a hidden bit in the valid number, which is always 1. so the part of the valid digit is always 1. xxxxxxx... (23 x ). another thing to note is the exponential representation, which is specified in IEEE754 as the offset index, this means that the number in the index bit minus the number after 127 represents the final index. in the above figure, the index part is 01111100, converted to a decimal number of 124, and then subtracted 127. The result is-3, that is, the index part is 2-3 = 1/8 = 0.125. what about the valid number? After the hidden bits are added, the value is 1.01000000000000000000000 = 1 + (1*2-2) = 5/4 = 1.25. Therefore, the number shown above is 1/8*5/4 = 0.15625.


The maximum and minimum values represented by the exponent are classified as 127 and-126. It is reasonable to say that 8 digits can represent-128 ~ Between 127, the purpose of setting to-126 is to prevent the smallest number (1/2-126) from overflow (this is hard to understand, as mentioned in the document ). here, 127 and-126 are represented as the index of 2. What is the value when the index is expressed as 10? We know that the inverse function of function y = 10x is x = log10y. Of course, here 10 can be any other number. therefore, 2127 = 10x => x = log2 (127) = 38.23 is approximately 38, and log2 (-126) =-37.99 is approximately-37 ~ 38. What about the seven-digit valid number? Similarly, log2 (24) = 7.22, that is, the 24-bit binary valid bits are equivalent to the order of 107, that is, 7-bit valid numbers. What is the maximum number of floating point numbers that can be expressed? We know that the maximum index is 127, and the maximum number of valid values is 1 for all, so this number should be

1.11111111111111111111111*2127 & asymp; 3.4028234*1038.


Float type


<? Php
$ A = 1.234;
$ B = 1.2e3;
$ C = 7E-10;
?>

When using the float type of PHP, you must note that the float type of PHP is somewhat inaccurate. If you need high-precision mathematical computing, you can use the dedicated mathematical functions provided by php, arbitrary precision math functions series and gmp series functions. Also, do not try to compare variables of the float type.

Note: the length of the floating point is related to the platform, although the maximum value is usually 1.8e308 with 14-bit decimal digits (64-bit IEEE format)


Summary:
The float length is consistent with that of the platform. Generally, the maximum value is 1.8e308 with a 14-digit decimal number precision. If a given number exceeds the integer range, it will be interpreted as the float type, the range of integer values is related to the platform. Generally, the maximum value is about 2 billion (32-bit signed). An advantage is that if the calculation result exceeds the integer range, float is also returned.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.