gpt4 book ai didi

php - 来自微时的圆形 float

转载 作者:行者123 更新时间:2023-12-04 06:00:40 26 4
gpt4 key购买 nike

PHP 版本 5.3.6

例子

<?php
$timeStart = microtime(true);
// some code ...
$timeExecution = microtime(true) - $timeStart;
$time = round($timeExecution, 2);
file_put_contents('h:/round.txt', $timeExecution . ' = ' . $time . "\n", FILE_APPEND);
?>

文件 round.txt 包含以下几行:

131.3048491477966 = 131.3

8.340715885162354 = 8.34

8.198318004608154 = 8.199999999999999

这怎么可能?

最佳答案

http://php.net/manual/en/language.types.float.php

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

关于php - 来自微时的圆形 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com