pChart 2.x forum
http://wiki.pchart.net/forum/

Division by zero error
http://wiki.pchart.net/forum/viewtopic.php?f=5&t=190
Page 1 of 1

Author:  chdd [ Mon Feb 07, 2011 3:12 pm ]
Post subject:  Division by zero error

I love pChart. I found a possible bug. I don't know if this issue has been reported or not.

Basically, I had a series of very small numbers, e.g. 0.00012, 0.00013, 0.00014, 0.00015. The code I am talking about is in pDraw.class.php:

Code:
$RowHeight = $ScaleHeight / $Rows;


I tried to plot a simple line chart. I got the division by zero error. I changed the code above into:

Code:
// see: pChart2.1.0\class\pDraw.class.php

      // -------- avoid division by zero errors below: added 06 Feb 2011
      if ( $Rows != 0 ) {
        $RowHeight = $ScaleHeight / $Rows; // line 2479 of pDraw.class.php
      }
      if ( $Rows == 0 ) {
        $RowHeight = 1;
      }
      // -------- avoid division by zero errors above


I get no more errors.

I think all the divisors may be treated in similar ways to prevent such errors.

Many thanks.

Author:  jean-damien [ Tue Feb 08, 2011 10:29 am ]
Post subject:  Re: Division by zero error

Thanks for having reported this issue. I've added an error detection test to the next sub release.

Kind regards,
JD.

Author:  char101 [ Wed Feb 16, 2011 1:26 pm ]
Post subject:  Re: Division by zero error

Hi,

There is another case of division by zero in this function

Code:
function modulo($Value1,$Value2)
    {
     if (floor($Value2) == 0) { return(0); }
     if (floor($Value2) != 0) { return($Value1 % $Value2); }

     $MinValue = min($Value1,$Value2); $Factor = 10;
     while ( floor($MinValue*$Factor) == 0 )
      { $Factor = $Factor * 10; }

     return(($Value1*$Factor) % ($Value2*$Factor));
    }


If $Value2 ie 1E19, then the % operator causes division by zero error.

By the way, I was creating a horizontal barchart. My data values is just array(0 => 23).

Author:  gregorio [ Sat Jul 23, 2011 7:43 pm ]
Post subject:  Re: Division by zero error

Hi,

Here's another division by zero.

(I posted this elswhere and thought it'd be good to have it here)

It triggers upon using writeLabel() function. I'm running pChart 2.1.1.

It's on line 2750, in the file: pDraw.class.php.

(No labels display, even after turning off the warnings)

I'm just drawing a line chart, and needed some labels.

Code:
 
if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT )
      {
       $Height      = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Data["Axis"][$AxisID]["Margin"]*2;
       $ScaleHeight = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"];
       $Step        = $Height / $ScaleHeight;

       if ( $ReturnOnly0Height )
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } }
       else
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaY2 - $Data["Axis"][$AxisID]["Margin"] - ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } }
      }
     else
      {
       $Width      = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Data["Axis"][$AxisID]["Margin"]*2;
       $ScaleWidth = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"];
       $Step       = $Width / $ScaleWidth;

       if ( $ReturnOnly0Height )
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } }
       else
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaX1 + $Data["Axis"][$AxisID]["Margin"] + ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } }
      }


Line 2750 is: $Step = $Width / $ScaleWidth;

(After hack attempts on the class, it appears pChart is having a tough time positioning the labels correctly.)

Thank you!

Greg

Author:  jean-damien [ Wed Aug 03, 2011 12:15 pm ]
Post subject:  Re: Division by zero error

Hi Greg,

Sorry but I cannot reproduce the issue you've described. Is it possible to show me the code you're using (and the dataset worked on) ?

Kind regards,
JD.

Author:  choko [ Thu Sep 29, 2011 10:12 am ]
Post subject:  Re: Division by zero error

Hi,

I think, that modulo function could be implemented like this:
Code:
    function modulo( $Value1, $Value2 ) {
        if ($Value2 == 0) {
            return 0;
        } else {
            $quotient = floor( $Value1 / $Value2 );

            return $Value1 - ( $quotient * $Value2 );
        }
    }


I hope it helps.

Choko

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/