Floating-Point Types


The floating-point types can represent numbers that have fractional components. There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. The type float is 32 bits wide and has an approximate range of 1.5E45 to 3.4E+38. The double type is 64 bits wide and has an approximate range of 5E324 to 1.7E+308.

Of the two, double is the most commonly used. One reason for this is that many of the math functions in C#’s class library (which is the .NET Framework library) use double values. For example, the Sqrt( ) method (which is defined by the standard System.Math class) returns a double value that is the square root of its double argument. Here, Sqrt( ) is used to compute the radius of a circle given the circle’s area:

 // Find the radius of a circle given its area. using System; class FindRadius {   public static void Main() {     Double r;     Double area;     area = 10.0;     r = Math.Sqrt(area / 3.1416);     Console.WriteLine("Radius is " + r);   } }

The output from the program is shown here:

 Radius is 1.78412203012729

One other point about the preceding example. As mentioned, Sqrt( ) is a member of the Math class. Notice how Sqrt( ) is called; it is preceded by the name Math. This is similar to the way Console precedes WriteLine( ). Although not all standard methods are called by specifying their class name first, several are, as the next example shows.

The following program demonstrates several of C#’s trigonometric functions, which are also part of C#’s math library. They also operate on double data. The program displays the sine, cosine, and tangent for the angles (measured in radians) from 0.1 to 1.0.

 //  Demonstrate Math.Sin(), Math.Cos(), and Math.Tan(). using System; class Trigonometry {   public static void Main() {     Double theta; // angle in radians     for(theta = 0.1; theta <= 1.0; theta = theta + 0.1) {       Console.WriteLine("Sine of " + theta + "  is " +                         Math.Sin(theta));       Console.WriteLine("Cosine of " + theta + "  is " +                         Math.Cos(theta));       Console.WriteLine("Tangent of " + theta + "  is " +                         Math.Tan(theta));       Console.WriteLine();     }   } }

Here is a portion of the program’s output:

 Sine of 0.1  is 0.0998334166468282 Cosine of 0.1  is 0.995004165278026 Tangent of 0.1  is 0.100334672085451 Sine of 0.2  is 0.198669330795061 Cosine of 0.2  is 0.980066577841242 Tangent of 0.2  is 0.202710035508673 Sine of 0.3  is 0.29552020666134 Cosine of 0.3  is 0.955336489125606 Tangent of 0.3  is 0.309336249609623

To compute the sine, cosine, and tangent, the standard library methods Math.Sin( ), Math.Cos( ), and Math.Tan( ) are used. Like Math.Sqrt( ), the trigonometric methods are called with a double argument, and they return a double result. The angles must be specified in radians.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net