Float and integer data types in php

Source: Internet
Author: User
Tags mathematical functions type null
This article analyzes the 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 the boolean type, the following values are considered FALSE.

This article analyzes the 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 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 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 ).

"Octal" indicates that 0 (zero) must be added before the number, and "0x" must be added before the number in hexadecimal notation.

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 will be interpreted as float. Similarly, if the execution result exceeds the integer range, float will be returned.

PHP does not use the divisible operator. 1/2 to generate 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 when an integer parameter is required for an operator, function, or flow control, the value is automatically converted. you can also use the intval () function to convert a value to an integer.

Convert from a Boolean value. if the value is FALSE, 0 (zero) is generated. if the value is TRUE, 1 (one) is generated ).

To convert from a floating point number to an integer, it is rounded to zero. if the floating point number is out of 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!

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 will be interpreted as the float type. Similarly, if the execution result exceeds the integer range, float will be 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 will lead to chaotic results: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of 8 in expectation, because the internal representation of the 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 decimal 1/3 is changed to 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. is a float representation 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 ≈ 3.4028234*1038.

Float type

  1.  
  2. $ A = 1.234;
  3. $ B = 1.2e3;
  4. $ C = 7E-10;
  5. ?>

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, will also return float.

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.