|
-
Jun 15th, 2008, 06:52 AM
#1
Thread Starter
Lively Member
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?
-
Jun 15th, 2008, 07:10 AM
#2
Hyperactive Member
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.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jun 15th, 2008, 09:25 AM
#3
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.
-
Jun 15th, 2008, 09:28 AM
#4
Re: multiplication of floating number in php
 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);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|