Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide by zero and no error? [duplicate]

Just threw together a simple test, not for any particular reason other than I like to try to have tests for all my methods even though this one is quite straightforward, or so I thought.

    [TestMethod]
    public void Test_GetToolRating()
    {
        var rating = GetToolRating(45.5, 0);
        Assert.IsNotNull(rating);
    }


    private static ToolRating GetToolRating(double total, int numberOf)
    {
        var ratingNumber = 0.0;

        try
        {
            var tot = total / numberOf;
            ratingNumber = Math.Round(tot, 2);
        }
        catch (Exception ex)
        {
            var errorMessage = ex.Message;
            //log error here
            //var logger = new Logger();
            //logger.Log(errorMessage);
        }


        return GetToolRatingLevel(ratingNumber);
    }

As you can see in the test method, I AM dividing by zero. The problem is, it doesn't generate an error. See the error window display below.

Error List View from VS2017

Instead of an error it is giving a value of infinity? What am I missing?So I googled and found that doubles divided by zero DON'T generate an error they either give null or infinity. The question becomes then, how does one test for an Infinity return value?

like image 528
dinotom Avatar asked May 30 '17 09:05

dinotom


People also ask

Does dividing by 0 cause a runtime error?

Definition. Division by zero is a logic software bug that in most cases causes a run-time error when a number is divided by zero.

What happens when a value is divided by a zero error?

Microsoft Excel shows the #DIV/0! error when a number is divided by zero (0). It happens when you enter a simple formula like =5/0, or when a formula refers to a cell that has 0 or is blank, as shown in this picture.

What is divide by 0 error called?

In ordinary arithmetic, the expression has no meaning, as there is no number that, when multiplied by 0, gives a (assuming ); thus, division by zero is undefined.


2 Answers

You are going to have DivideByZeroException only in case of integer values:

int total = 3;
int numberOf = 0;

var tot = total / numberOf; // DivideByZeroException thrown 

If at least one argument is a floating point value (double in the question) you'll have FloatingPointType.PositiveInfinity as a result (double.PositiveInfinity in the context) and no exception

double total = 3.0;
int numberOf = 0;

var tot = total / numberOf; // tot is double, tot == double.PositiveInfinity
like image 102
Dmitry Bychenko Avatar answered Oct 07 '22 15:10

Dmitry Bychenko


You may check like below

double total = 10.0;
double numberOf = 0.0;
var tot = total / numberOf;

// check for IsInfinity, IsPositiveInfinity,
// IsNegativeInfinity separately and take action appropriately if need be
if (double.IsInfinity(tot) || 
    double.IsPositiveInfinity(tot) || 
    double.IsNegativeInfinity(tot))
{
    ...
}
like image 37
Shankar Avatar answered Oct 07 '22 15:10

Shankar