Home php教程 php手册 PHP学习记录(浮点型)

PHP学习记录(浮点型)

Jun 13, 2016 am 11:34 AM
php pair study regular floating point Accuracy Record

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

正则表达示如下:

浮点数:     [0-9]+
双精度型:   ([0-9][.]{LNUM}) | ({LNUM}[.][0-9]*)
指数表达式: [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})

=========================
最大浮点数php代码如下:
  function float_max($mul = 2, $affine = 1) {
    $max = 1; $omax = 0;
    while((string)$max != 'INF') { $omax = $max; $max *= $mul; }

    for($i = 0; $i       $pmax = 1; $max = $omax;
      while((string)$max != 'INF') {
        $omax = $max;
        $max += $pmax;
        $pmax *= $mul;
      }
    }
    return $omax;
  }
  echo "最大浮点数:" ;var_dump(float_max());
?>
=========================
结果如下(与平台相关):
最大浮点数:float(1.79769313486E+308)

关于浮点数精度的注意事项:

简单的十进制分数如同 0.1 或 0.7 在转换为内部二进制的格式会丢失精度:
例如,floor((0.1+0.7)*10) 通常会返回 7 而不是预期中的 8,因为该结果内部的表示其实是7.9。
就是不可能精确的用有限位数表达某些十进制分数。
例如,十进制的 1/3 变成了 0.3。
所以
1。永远不要相信浮点数结果精确到了最后一位,
2。也永远不要比较两个浮点数是否相等。
3。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数。

自 PHP 5 起,如果试图将对象转换为浮点数,会发出一条 E_NOTICE 错误。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles