multiplication of floating number in php
Multiplication of floating numbers in php is giving unacceptable result. For example, multiplication of 0.023* 0.0034 * 0.11 is yielding 0.
I have an array containing 48 floating numbers less than 1. I want the product of all 48 numbers, any way to achieve this in PHP?
Re: multiplication of floating number in php
0.023*0.0034*0.11 = 8.602E-6
PHP Code:
<?php
echo "0.023*0.0034*0.11 = " . 0.023*0.0034*0.11;
?>
If you typecast it to int it will give 0, but otherwise it works.
Re: multiplication of floating number in php
Bear in mind that floating point arithmetic is imprecise, because of the way floating point numbers are represented in memory and manipulated in the FPU. If you require absolute precision, consider using the bcmath library.
Re: multiplication of floating number in php
Quote:
Originally Posted by sridharao
I have an array containing 48 floating numbers less than 1. I want the product of all 48 numbers, any way to achieve this in PHP?
PHP Code:
function sum($x, $y) { return $x + $y; }
$sum = array_reduce($numbers, "sum", 0);