Home

Floating-Point Numbers

 

Introduction

As seen in Lesson 2, to support floating-point numbers, you can use the float, the double, or the decimal data types. The C# float data type originates from the Single structure of the .NET Framework. The double data type is based on the Double structure of the .NET Framework. The C# decimal data type is type-defined from the .NET Framework’s Decimal structure. To declare a floating-point variable, you can use one of these data types and initialize it.

 

When initializing a floating-point variable, you should make sure you assign it a value that can fit in the memory allocated for it. As mentioned for the integer data types, the minimum value that a float variable can hold is represented by the MinValue constant of the Single structure and the MinValue field of the Double. The maximum value that a float or a double variable can hold is named MaxValue.

After declaring and initializing a float or a double variable, it should hold an appropriate value. If you get the variable’s value some other way, at one time or another, you may not know what value is stored in the memory allocated for the variable. In fact, the variable may hold a value that is not a number. To check whether the variable is holding a value that is not a number, you can access its NaN constant. To do this, type the float or double data type, followed by the period operator, and followed by the NaN constant. Here is an example:

using System;

class Program
{
    static int Main()
    {
	double number = 0D;

	if( number == double.NaN )
	    Console.WriteLine("The value is not a number");
	return 0;
    }
}

Another technique you can use to check this characteristic is to IsNaN() method of either the Single or the Double structure.

Operations on Floating-Point Numbers

Using one integer and one floating-point number, or with two floating-point numbers, you can perform one of the routine arithmetic operations such as the addition, the subtraction, the multiplication, or the division. When it comes to the division and if performed on two constants, you can get a positive or a negative number. In highly precise calculations, you may have to deal with an approximate number whose exact value is not known. For example, the smallest positive number is called epsilon. In the Double and the Single structures, this constant is named Epsilon. For the single-precision type, the epsilon is equal to 1.445. For a double-precision type, the epsilon constant is equivalent to 4.94065645841247-324. As you can see, this number is extremely low.

When dealing with real numbers, some operations produce very little or very large numbers. In algebra, the smallest number is called negative infinity. In the .NET Framework, the negative infinity is represented by a constant named NegativeInfinity. To access this number, type either float or double, followed by a period operator, followed by the name of this constant. Here is an example:

using System;

class Program
{
    static int Main()
    {
	Console.WriteLine("Negative Infinity = {0}",
		double.NegativeInfinity);
	return 0;
    }
}

To find out if a variable holds a negative infinity value, you can call the IsNegativeInfinity() method from the variable. The syntaxes of this method are:

public static bool IsNegativeInfinity(float f);
public static bool IsNegativeInfinity(double d);

On the other extreme, the possible largest number is named positive infinity. This constant is represented in the .NET Framework by the PositiveInfinity value. To access this constant, type float or double, followed by the period, followed by the name of this constant. To find out if a variable’s value is a positive infinity, you can call its IsPositiveInfinity() method. The syntaxes of this method are:

public static bool IsPositiveInfinity(float f);
public static bool IsPositiveInfinity(double d);

To check whether the value of a variable is one of the infinities, you can call its IsInfinity() method. The syntaxes of this method are:

public static bool IsInfinity(float f);
public static bool IsInfinity(double d);

 

Comparison Operations

Because floating-point numbers can be approximate, you should be careful when comparing them, especially for equality. Consider the following examples:

using System;

class Program
{
    static int Main()
    {
	double number1 = 22.15D;
	double number2 = 22.15D;

	if( number1 == number2 )
	    Console.WriteLine("The values are equal");
	return 0;
    }
}

This would produce:

The values are equal
Press any key to continue . . .

For the sake of discussion, these values of these variables were limited to 2 decimal places. If they represented hourly salaries of employees, the comparison would be easily performed and can produce an exact result. If you want to apply more precision do the numbers, for example if the variables represent weight values, you would need more places on the right side of the decimal separator. Consider the following program:

using System;

class Program
{
    static int Main()
    {
	double number1 = 22.156D;
	double number2 = 22.157D;

    	if( number1 == number2 )
	    Console.WriteLine("The values are equal"); 
	else
	    Console.WriteLine("The values are NOT equal");
	return 0;
    }
}

This would produce:

The values are NOT equal
Press any key to continue . . .

This time, because of more precision, the variables don’t hold the same value.

Besides the equality, you can also compare floating-point variables to find out if one has a value lower than the other. To support such operations, the Single and the Double structures are equipped with a method named CompareTo. The rules of this method are functionally the same as those we reviewed for integers.

 

Home Copyright © 2006 FunctionX, Inc.