Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Division By Zero Doesn't Always Raise An Exception

, 17 Apr 2013
Rate this:
Please Sign up or sign in to vote.
This tip shows the different results of dividing different type numbers with Zero

Introduction

In this tip, I would like to point out that dividing a number by zero doesn't always raise an exception as is generally taken for granted. The raising of exception depends upon the type of the number being divided by zero.

DivideByZeroException

As the name itself suggests, this exception is raised at run time when the compiler tries to divide a number by zero. But this exception is not raised at all times. For instance, take the code below:

int num1 = 150;
int num2 = 0;
int result = num1/num2; 

The above piece of code, when executed will raise the DivideByZeroException.

But this is not the case when we divide a number of type double with zero. For instance, if you execute the code below, it will not raise any exception and will just execute normally.

double num1 = 15.8
int num2 = 0 
double result = num1/num2;   

Now, this is not a bug but in fact, a very normal scenario. The result of this division will be "Infinity", positive infinity, to be precise. the double type has a property for infinity. This infinity is just like any other number and can also be used in calculations. For instance, the following lines of code can be executed without any compilation errors or run time exceptions.

Console.WriteLine(15 + infinity);       //prints infinity
Console.WriteLine(15 - infinity);       //prints negative infinity
Console.WriteLine(infinity * 3);        //prints infinity again
Console.WriteLine(infinity * 0);        //prints NaN
Console.WriteLine(infinity / 1023);     //prints infinity
Console.WriteLine(1281 / infinity);     //prints zero
Console.WriteLine(infinity + infinity); //prints infinity
Console.WriteLine(infinity / infinity); //prints NaN
Console.WriteLine(infinity * infinity); //prints infinity

The double type provides methods to check if a number is infinity or not. Some of those properties are:

  1. double.IsInfinity(double num)
  2. double.IsPositiveInfinity(double num)
  3. double.IsNegativeInfinity(double num)

The type also provides properties like:

  1. double.PositiveInfinity
  2. double.NegativeInfinity
  3. double.NaN

This can be used in calculations where infinity is being used.

Please provide your valuable suggestions and comments to improve this tip. I have also uploaded the source code (.CS file) for a sample application along with this tip.

Hope this helps!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amogh Natu
Software Developer
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionIs there any sound strategies to avoid Division by zero? Pinmemberwu_zhang7-Sep-14 16:40 
QuestionMy vote of 5. [modified] PinmemberBill_Hallahan29-Nov-13 11:28 
AnswerRe: My vote of 5. PinprofessionalAmogh Natu8-Dec-13 2:14 
GeneralMy vote of 4 PingroupPaul @ The Computer Station26-Apr-13 2:20 
GeneralRe: My vote of 4 PinprofessionalAmogh Natu29-Apr-13 19:36 
GeneralMy vote of 5 PinmemberPaul Tait22-Apr-13 0:01 
GeneralRe: My vote of 5 PinprofessionalAmogh Natu22-Apr-13 0:04 
GeneralMy vote of 5 PinmemberL337Cthulhu19-Apr-13 2:44 
GeneralRe: My vote of 5 PinprofessionalAmogh Natu19-Apr-13 4:50 
QuestionShort and informative PinmemberAnotherKen18-Apr-13 14:58 
GeneralRe: Short and informative PinprofessionalAmogh Natu18-Apr-13 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Apr 2013
Article Copyright 2013 by Amogh Natu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid