Results 1 to 4 of 4

Thread: multiplication of floating number in php

  1. #1

    Thread Starter
    Lively Member sridharao's Avatar
    Join Date
    Feb 2007
    Posts
    106

    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?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width