Loading...

- Access to all 42 C# articles plus all upcoming, new content.
- Save countless hours of confusion and frustration with high-quality, easy-to-follow programming tutorials.
- Unrestricted access to 352 MultiCharts, TradingView, and MultiCharts .NET articles.

Subscriptions start at just $15 per month.

Join today and take your trading code programming to the next level!

Arithmetic operators can affect the resulting data type, but can also lead to special values like *Infinity* and NaN depending on the operands used. What causes these values and what do they signal?

## Dividing by zero and triggering an exception in C#

Dividing a number with the `/`

operator by zero triggers the `DivideByZeroException`

exception (Stellman & Greene, 2010). For example:

int numberOfDays = 0; int trades = 2; Console.WriteLine("Trades per day: {0}", trades / numberOfDays);

When a trading day hasn’t ended, the `numberOfDays`

variable is still zero and so a ‘divide by zero’ error is thrown:

The error is caused because dividing any number by zero results in infinity, which is outside the range of `int`

, `long`

, and `decimal`

types (Sharp, 2013). But other data types can deal with that special value and are okay with division by zero.

## C#’s floating point types and special values: zeros, infinities and NaN

There are two floating point types in C#: `float`

and `double`

(Hejlsberg, Torgersen, Wiltamuth, & Golde, 2011). These types support, besides regular numbers, also the following special values (Albahari & Albahari, 2012; Hejlsberg et al., 2011):

**Positive zero**(`+0`

) and**negative zero**(`-0`

). When the result of a floating point operation is too small for the destination format, the result becomes a positive or negative zero.**Positive infinity**(`+∞`

) and**negative infinity**(`-∞`

) happens when a nonzero number is divided by zero. It signals that the floating point operation led to a value too large for the destination format.*“Not a number”*(abbreviated to**NaN**) values are produced by invalid floating point operations, such as dividing zero with zero.

Because floats and doubles can hold these special values, floating point operations don’t produce exceptions (Hejlsberg et al., 2011).

## An infinite C# floating point value due to dividing by zero

Dividing zero by any number (such as `0 / 10`

) leads to zero, but the value of dividing a number by zero (like `10 / 0.0`

) is infinity (Albahari & Albahari, 2012; Sharp, 2013). Depending on the operands, dividing by zero can result in either positive or negative infinity:

Console.WriteLine(" 1.0 / 0.0 = {0}", 1.0 / 0.0); Console.WriteLine("-1.0 / 0.0 = {0}", -1.0 / 0.0); Console.WriteLine(" 1.0 / -0.0 = {0}", 1.0 / -0.0); Console.WriteLine("-1.0 / -0.0 = {0}", -1.0 / -0.0); //> 1.0 / 0.0 = Infinity //> -1.0 / 0.0 = -Infinity //> 1.0 / -0.0 = -Infinity //> -1.0 / -0.0 = Infinity Console.WriteLine("13 / 0.0 = {0}", 13 / 0.0); Console.WriteLine("-9 / 0.0 = {0}", -9 / 0.0); Console.WriteLine("1314.2 / 0.0 = {0}", 1314.2 / 0.0); //> 13 / 0.0 = Infinity //> -9 / 0.0 = -Infinity //> 1314.2 / 0.0 = Infinity

## NaN values: ‘not a number’ for numerical floating point types in C#

The expression `0.0 / 0.0`

poses a dilemma: dividing zero by anything leads to zero, but dividing any number by zero results in infinity (Sharp, 2013). Since it cannot be both values at once, dividing zero by zero instead leads to a NaN value (Sharp, 2013). Also subtracting infinity from infinity results in a NaN value (Albahari & Albahari, 2012).

An example of both is given below:

Console.WriteLine("0.0 / 0.0 = {0}", 0.0 / 0.0); Console.WriteLine("Infinity - Infinity: {0}", (1.0 / 0.0) - (1.0 / 0.0)); //> 0.0 / 0.0 = NaN //> Infinity - Infinity: NaN

NaNs are sometimes useful to represent special situations (Albahari & Albahari, 2012) or to signal that a calculation didn’t result in a valid numerical value.

## NaN and infinity in C# expressions: special values that reproduce

Both NaN and infinity propagate through expressions: when one or both operands is NaN, the result is NaN (Hejlsberg et al., 2011; Sharp, 2013). Examples of this are:

Console.WriteLine("NaN + 10 = {0}", (0.0 / 0.0) + 10); Console.WriteLine("Infinity + 25.9 = {0}", (1.0 / 0.0) + 25.9); Console.WriteLine("NaN * 0 = {0}", (0.0 / 0.0) * 0); Console.WriteLine("Infinity * 0 = {0}", (1.0 / 0.0) * 0); //> NaN + 10 = NaN //> Infinity + 25.9 = Infinity //> NaN * 0 = NaN //> Infinity * 0 = NaN

Infinite values, which are caused by a value too large for its data type, can therefore not be reduced such that they become a valid number again.

For more on these special floating point type values, see performing comparisons with zeros, NaN, and infinity.

### Summary

Dividing a number by zero results in an exception; dividing the same number by a floating point zero (`0.0`

) results in *infinity* (a value too large for the destination format). Floating point values can also be too small, in which case they result in a positive or negative zero. NaN (“not a number”) values happen due to an invalid floating point operation (like dividing zero with zero).

**References**

Albahari, J. & Albahari, B. (2012). *C# 5.0 in a Nutshell: The Definitive Reference* (5th edition). Sebastopol, CA: O’Reilly Media.

Hejlsberg, A., Torgersen, M., Wiltamuth, S., & Golde, P. (2011). *The C# Programming Language: Special Annotated Edition for C# 4.0* (4th edition). Upper Saddle River, NJ: Addison-Wesley.

Sharp, J. (2013). *Microsoft Visual C# 2013 Step by Step*. Microsoft Press.

Stellman, A. & Greene, J. (2010). *Head First C#: A Brain-Friendly Guide* (2nd edition). Sebastopol, CA: O’Reilly Media.