View unanswered posts | View active topics It is currently Sat Apr 27, 2024 5:02 pm



Reply to topic  [ 6 posts ] 
Division by zero error 
Author Message
New pChart user
New pChart user

Joined: Mon Feb 07, 2011 2:57 pm
Posts: 1
Post 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.


Mon Feb 07, 2011 3:12 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Thu Dec 02, 2010 2:31 pm
Posts: 409
Location: France
Post 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.


Tue Feb 08, 2011 10:29 am
Profile WWW
New pChart user
New pChart user

Joined: Wed Feb 16, 2011 1:23 pm
Posts: 1
Post 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).


Wed Feb 16, 2011 1:26 pm
Profile
New pChart user
New pChart user

Joined: Sat Jul 23, 2011 6:57 pm
Posts: 2
Post 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


Sat Jul 23, 2011 7:43 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Thu Dec 02, 2010 2:31 pm
Posts: 409
Location: France
Post 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.


Wed Aug 03, 2011 12:15 pm
Profile WWW
New pChart user
New pChart user

Joined: Thu Sep 29, 2011 10:08 am
Posts: 1
Post 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


Thu Sep 29, 2011 10:12 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron