Integers and Floating point numbers in PHP

WBOY
풀어 주다: 2016-06-23 14:33:01
원래의
1326명이 탐색했습니다.

原文:http://www.ebrueggeman.com/blog/integers-and-floating-numbers/ Background

PHP is not a strictly typed language, and many programmers often overlook problems that can be caused with not paying attention to numeric types. The problem is that PHP does not alert you when you have gone outside of the bounds of what an integer can hold. Instead, it silently converts your value to a float (floating point number.)

Why should you care? Mathematic expressions with floats are not always exact ? sometimes they can be way off. PHP, like most programming languages, uses shortcuts to estimate the result of floating point mathematic expressions. The rule of thumb is that the larger the numbers you are working with, the greater amount of estimations involved in finding a result. If you were programming a PHP-based financial application, this could be a huge liability.

The best way to handle large numbers in PHP is to be aware of exactly what type of number you are dealing by knowing the PHP bounds of integers. PHP supplies a constant PHP_INT_MAX which will tell you what the maximum integer is for your instance of PHP. Not all computers/operating systems/versions of PHP are the same, so it is good to find the PHP_INT_MAX for each setup. Note that the PHP_INT_MAX also determines the smallest negative number ? just multiply it by -1 to get the PHP integer minimum.

PHP Integer Testing Script

Below is a testing script to help you determine the PHP_INT_MAX:

<?php$intMax = PHP_INT_MAX;   echo "$intMax<br />";   if (is_int($intMax)) {	echo "$intMax is an Int<br />";}else {	echo "$intMax is NOT an Int<br />";}   if (is_int($intMax + 1)) {	echo "$intMax+1 is an Int<br />";}else {	echo "$intMax+1 is NOT an Int<br />";}   if (is_int($intMax * (-1))) {	echo "$intMax*(-1) is an Int<br />";}else {	echo "$intMax*(-1) is NOT an Int<br />";}?>
로그인 후 복사

The result of this script:

21474836472147483647 is an Int2147483647+1 is NOT an Int2147483647*(-1) is an Int
로그인 후 복사
PHP Float Testing Script

Now that you know the bounds of integers, what do you do? Truth be told, there is not a whole lot you can do to guarantee the integrity of your floating point numbers. It may be more important to know which figures are the result of math involving floating point numbers, so you can disclose this to your audience, and act accordingly. See the simple example below that shows how poorly PHP can do math involving floating point numbers:

<?php//TEST 1$number = 216897510871;$original_number = $number;   $number *= 11123.74;$number /= 11123.74;   if ($original_number == $number) {	echo "Test 1: Are Equal<br />";}else {	echo "Test 1: Are NOT Equal<br />";}   //TEST 2$number = 216897510871;$original_number = $number;   $number *= 11123.75;$number /= 11123.75;   if ($original_number == $number) {	echo "Test 2: Are Equal<br />";}else {	echo "Test 2: Are NOT Equal<br />";}?>
로그인 후 복사

The result of this script:

Test 1: Are EqualTest 2: Are NOT Equal
로그인 후 복사
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!