Treat division by zero

Asked

Viewed 1,848 times

2

I have the following function in PHP:

public function teste() {
    $conta = "0/(0+0+0)";
    eval('$result = (' . $conta . ');');
    echo $result;
}

In this case, the formula (in example 0/(0+0+0) obviously returns error because of division by zero. The problem is that this formula is built dynamically and it can happen of division by zero. In this case I would like it to return only zero and not error.

  • If this data is dynamical Voce can do a check on them to see if they are equal to zero or not, before doing this calculation.

  • 1

    could use example: a/b+c+d, would be more readable.... if (b+c+d = 0) echo '0' else ....calcula... !?

  • 1

    The big problem is that the formula may have division or not. The user can assemble several mathematical formulas.

  • Do you validate the validity of the formula? Or do you simply accept any string passed and leave it to give arithmetic problems in the last instance? For example, this formula would be invalid (1 + 2 + 3*(123), for parenthesis is lacking.

  • I validated the expression before. But the values I have no way of knowing.

  • 1

    @Felipesaraiva if you validate you are already compiling (the constants I put just for example). If you are compiling, you can assemble the expression tree and treat it internally, according to the order of precedence.

  • @Jeffersonquesado is right, I’m going to go for this idea, thank you very much! Please, how do I "approve" this suggestion?

  • 1

    @Felipesaraiva write an official reply. I am particularly lazy/lacking time to make a formal response

Show 3 more comments

2 answers

8


First, you shouldn’t wear eval(). You have to have a very strong domain in programming to use it smoothly. And whoever has this domain always finds a better solution.

Division by zero is considered a programming error, is the same thing as giving a pointer error (PHP does not give it, I think, but it treats in a way that I consider harmful), it should never occur, the solution is to ensure that there is no division by zero, so you must ensure that the divisor is not zero before using it. A simple if before it resolves. But in this case it doesn’t even need to be since you know it’s 0, you don’t have to do it in real code.

Always treating the division by 0 as resulting in 0 is a mathematical error, if this were correct the mathematics would do this. And when it makes sense for some other reason than the mathematician is using the number for a function he shouldn’t have. Again, programming errors should not be treated as if they were normal, or as if they were exceptions.

When catching exception, and in good codes this rarely occurs, never capture Exception, this exception should even be abstract. Maybe everything that serves to inherit should.

See also:

1

Putting a Try / catch would not be the solution?

public function teste() {
    $conta = "0/(0+0+0)";
    try {
      eval('$result = (' . $conta . ');');
      echo $result;
    }
    catch(Exception $e) {
      echo 0;
    }    
}
  • 4

    works... but if you can check if the value is zero before calculating, why fire an Exception ?! ps. The -1 was not me =]

  • I think not, because in the question he said that the expression was dynamic, like a calculator.

  • I understood, yet it is possible to check, it takes more work... rs but it is possible =]

  • 1

    @Rovannlinhalis, just a basic little compilation ;-)

  • 1

    A huge complication, rsss. I tried this solution but still has an error.

  • 1

    @Correct rovannlinhalis, I answered about that

  • 1

    @Marlontiedt one error does not justify the other, being dynamic is the first mistake. One makes two new ones because of it.

  • @Maniero blz, always with great answers. I just didn’t understand the comment "being dynamic is the first mistake". Example: a laboratory system, the user registers a formula to validate the result of an exam. So the formula is dynamic, would be wrong ?! that’s it ?

  • 3

    @Rovannlinhalis implmentation with eval() is, is insecure and gives a terrible experience where in practice who is registering the formula needs to be practically programmer. If you have to make a formula registration feature it is necessary to make a very complex mechanism and should allow you to indicate data constraints that this formula should have, including not dividing by 0. Of course, one can do whatever you want, but it’s a huge gambit to simplify what’s complex by nature. It has a much simpler solution. Ñ lets the person register formulas. It can be the best solution and better than they imagine

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.