note 97549 modified in function.pow by danbrown
| From: | danbrown@php.net | Date: | Mon, 26 Apr 2010 14:35:41 +0000 |
| Subject: | note 97549 modified in function.pow by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168657@lists.php.net to get a copy of this message | ||
You can increase the 'precision' php.ini setting a little to work with larger float
numbers here, but this comes at at cost of sacrificing decimal accuracy. The default
'precision' is 14. 5 is about the threshhold that php can handle for decimal accuracy
before at least some number corruption starts showing or it cannot output the actual number, and 16
for large number accuracy, as demonstrated by throwing this into the table below:
<?php
echo "<td>".pow(10, $i) - 1)."</td>";
?>
See the table below for an example, and adjust your php.ini 'precision' setting according
to what your OS and PHP version can handle and what number size you want to work with.
Alternatively, you can use the bc math functions for more accuracy all around, and not have to rely
on the 'precision' ini setting at all, but this moves out of the realm of strictly floats
and into strings.
Also, PHP just prefers to display the 'E' notation of float values where possible after
about 5 decimal places rather than the actual decimal number (1.0E-5 vs 0.00005).
<?php
echo "<table>";
for($i = 0; $i < 50; $i++) {
$precision = $i + 1;
ini_set('precision', $precision);
echo "<tr>";
echo "<td>".$precision."</td>";
echo "<td>".pow(10, $i)."</td>";
echo "<td>".pow(10, (-1 * $i))."</td>";
echo "<td>".bcpow('10', (string) $i, $precision)."</td>";
echo "<td>".bcpow('10', (string) (-1 * $i),
$precision)."</td>";
echo "</tr>";
}
echo "</table>";
?>
Bottom line though is, if you're working with larger numbers or require very fine decimal
precision or prefer displaying the full decimal number, use the bc math functions instead. And, do
check out PHP's considerations about the float type here: http://php.net/manual/en/language.types.float.php
--was--
You can increase the 'precision' php.ini setting a little to work with larger float
numbers here, but this comes at at cost of sacrificing decimal accuracy. The default
'precision' is 14. 5 is about the threshhold that php can handle for decimal accuracy
before at least some number corruption starts showing or it cannot output the actual number, and 22
for large number accuracy. See the table below for an example, and adjust your php.ini
'precision' setting according to what your OS and PHP version can handle and what number
size you want to work with. Alternatively, you can use the bc math functions for more accuracy all
around, and not have to rely on the 'precision' ini setting at all, but this moves out of
the realm of strictly floats and into strings.
<?php
echo "<table>";
for($i = 0; $i < 50; $i++) {
$precision = $i + 1;
ini_set('precision', $precision);
echo "<tr>";
echo "<td>".$precision."</td>";
echo "<td>".pow(10, $i)."</td>";
echo "<td>".pow(10, (-1 * $i))."</td>";
echo "<td>".bcpow('10', (string) $i, $precision)."</td>";
echo "<td>".bcpow('10', (string) (-1 * $i),
$precision)."</td>";
echo "</tr>";
}
echo "</table>";
?>
http://php.net/manual/en/function.pow.php