PHP Learning record (floating-point type)

Source: Internet
Author: User
Tags mathematical functions mul php code

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

The regular expressions are as follows:

Floating point numbers: [0-9]+
Double type: ([0-9][.] {Lnum}) | ({lnum}[.] [0-9]*)
Exponential expression: [+-]? (({lnum} | {Dnum}) [EE] [+-]? {Lnum})

=========================
Maximum floating-point number PHP code is as follows:
<?php
function Float_max ($mul = 2, $affine = 1) {
$max = 1; $omax = 0;
while ((string) $max!= ' INF ') {$omax = $max; $max *= $mul;}

for ($i = 0; $i < $affine; $i + +) {
$pmax = 1; $max = $omax;
while ((string) $max!= ' INF ') {
$omax = $max;
$max + + $pmax;
$pmax *= $mul;
}
}
return $omax;
}
echo "Maximum floating-point number:"; Var_dump (Float_max ());
?>
=========================
The results are as follows (platform-related):
Maximum floating-point number: float (1.79769313486E+308)

Considerations about floating-point precision:

A simple decimal fraction can lose precision as 0.1 or 0.7 in a format converted to an internal binary:
For example, floor (0.1+0.7) *10 usually returns 7 rather than the expected 8, because the internal representation of the result is actually 7.9.
It is impossible to accurately express certain decimal points with a finite number of digits.
For example, the decimal 1/3 becomes 0.3.
So
1. Never believe that floating-point numbers are accurate to the last one,
2. And never compare two floating-point numbers for equality.
3. If you do need higher precision, you should use arbitrary precision mathematical functions or GMP functions.

Since PHP 5, if you try to convert an object to a floating-point number, a e_notice error is emitted.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.