Extended Values
Extended Values
A Double-Word
Introduction
A double-word is a group of two consecutive Words. This means that a double-word combines 4 bytes or 32 bits. The bits, counted from right to left, start at 0 and end at 31. The rightest bit, bit 0, is called the low order bit or LO bit or LOBIT. The most left bit, bit 31, is called the high order bit or HI bit or HIBIT. The other bits are called using their positions.
The group of the first 8 bits (from bit 0 to bit 7), which is the right byte, is called the low order byte, or LOBYTE. The group of the last 8 bits (from bit 24 to bit 31), which is the left byte, is called the high order byte, or HIBYTE. The other bytes are called by their positions. The group of the right 16 bits, or the right Word, is called the low order word, or LOWORD. The group of the left 16 bits, or the left word, is called the high order word, or HIWORD.
The minimum binary number you can represent with a double-word is 0. The minimum decimal value of a double-word is 0. To find out the maximum decimal value of a word, you can use the base 2 formula giving a 1 value to each bit:
2n-1 | 230 | 229 | 228 | 227 | 226 | 225 | 224 |
etc | 1,073,741,824 | 536,870,912 | 268,435,456 | 134,217,728 | 67,108,864 | 33,554,432 | 16,777,216 |
223 | 222 | 221 | 220 | 219 | 218 | 217 | 216 |
8,388,608 | 4,194,304 | 2,097,152 | 1,048,576 | 524,288 | 262,144 | 131,072 | 65,536 |
215 | 214 | 213 | 212 | 211 | 210 | 29 | 28 |
32,768 | 16,384 | 8,192 | 4,096 | 2,048 | 1,024 | 512 | 256 |
27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1*231+1*230+1*229 + 1*228 + 1*227 + 1*226 + 1*225 + 1*224 + 1*223 + 1*222 + 1*221 + 1*220 + 1*219 + 1*218 + 1*217 + 1*216 + 1*215 + 1*214 + 1*213 + 1*212 + 1*211 + 1*210 + 1*29 + 1*28 + 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20
= 2,147,483,648 + 1,073,741,824 + 536,870,912 + 268,435,456 + 134,217,728 + 67,108,864 + 33,554,432 + 16,777,216 + 8,388,608 + 4,194,304 + 2,097,152 + 1,048,576 + 524,288 + 262,144 + 131,072 + 65,536 + 32,768 + 16,384 + 8,192 + 4,096 + 2,048 + 1,024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
= 4,286,578,708
The minimum hexadecimal value you can store in a double-word is 0x00000000000000000000000000000000 which is the same as 0x0. To find out the maximum hexadecimal number you can represent with a word, replace every group of 4-bits with an f or F:
1111 | 1111 | 1111 | 1111 | 1111 | 1111 | 1111 | 1111 |
f | f | f | f | f | f | f | f |
= 0xffffffff = 0xFFFFFFFF |
To declare a variable that can hold large values, you can use the var keyword and initialize the variable with the desired value. Here is an example:
using static System.Console;
public class Exercise
{
public static void Main()
{
var apartNbr = 24;
WriteLine("Apartment Rental Management");
Write("Apartment #: ");
WriteLine(apartNbr);
}
}
This would produce:
Apartment Rental Management Apartment #: 24 Press any key to continue . . .
A double-word is large enough to contain double the amount of data that can be stored in a word. This is equivalent to 32 bits or 4 bytes or 4,294,967,295. Therefore, a double-word is used for small or large numbers.
To use a variable that would hold quite large numbers, besides the var keyword, you can declare it using the int keyword. A variable declared as int can store values between -2,147,483,648 and 2,147,484,647 negative or positive, that can fit in 32 bits.
To let you display an integer to the console screen, both the Console.Write() and the Console.WriteLine() methods each has a syntax that takes an int argument. Their syntaxes are:
public static void Write(int value); public static void WriteLine(int value);
Here are examples:
using static System.Console;
public class Exercise
{
public static void Main()
{
int coordX;
int coordY;
coordX = 12;
coordY = -8;
Write("Cartesian Coordinate System: ");
Write("P(");
Write(coordX);
Write(", ");
Write(coordY);
WriteLine(")\n");
}
}
When executed, the program would produce:
Cartesian Coordinate System: P(12, -8)
If the number is large, don't use the character for thousands separators, which in English is the comma used in the English language. Here is an example:
using static System.Console;
public class Exercise
{
static int Main()
{
int yearlySalary = 82460;
WriteLine("Fun Department Store");
Write("Yearly Salary: ");
WriteLine(yearlySalary);
return 0;
}
}
The program would produce:
Fun Department Store Yearly Salary: 82460 Press any key to continue . . .
Characteristics of Integers
Creating an Integral Value
As mentioned already, an integer is a combination of digits to represent a natural number. By default, when creating the number in a C# application, use only digits. Still, if the number is large or very large and becomes difficult to read, you can separate the thousands from right to left with underscores. Here are examples:
using static System.Console; public class Exercise { static int Main() { var emplNbr = 20_931_705; int yearlySalary = 82_460; WriteLine("Fun Department Store"); Write("Employee #: "); WriteLine(emplNbr); Write("Yearly Salary: "); WriteLine(yearlySalary); return 0; } }
Declaring a Variant Integral Variable
If you declare an integer variable using the var keyword and initialize it with a value lower than 2,147,484,647, the compiler concludes that the memory needed to store that variable is 32 bits:
Hexadecimal Numbers
When initializing an integral variable, instead of a decimal number, you can also initialize it with a hexadecimal value whose decimal equivalent is less than 2,147,484,647. Here is an example:
using static System.Console;
public class Exercise
{
static void Main()
{
var number = 0xF0488EA;
Write("Number: ");
WriteLine(number);
}
}
This would produce:
Number: 251955434 Press any key to continue . . .
Remember that you can declare the variable using the int, the var, or the dynamic keyword. In this case, initialize the variable with a natural number Here are examples:
int score = 3907; var feel = 95; dynamic face = 794800;
A 32-Bit Integer
To support 32-bit integers, the .NET Framework provides a structure named Int32. You can use it to declare a variable. Here is an example:
int score = 3907;
var feel = 95;
dynamic face = 794800;
Int32 ambiance = 9284;
The .NET Framework represents the int data type with a structure named Int32.
The Int32 just like the int data type are for variables that can hold values between -2,147,483,648 and 2,147,483,647. The Int32 structure is equipped with fields and methods that use the exact same names we reviewed for the byte type. This includes the ability to parse a value et get a converted integer. That structure also allows you to get the minimum and the maximum values of a 32-bit variable. Here are examples:
using System; using System.Windows.Forms; namespace ValuesCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblByteMinValue.Text = byte.MinValue.ToString(); lblByteMaxValue.Text = Byte.MaxValue.ToString(); lblShortMinValue.Text = Int16.MinValue.ToString(); lblShortMaxValue.Text = Int16.MaxValue.ToString(); lblUShortMinValue.Text = ushort.MinValue.ToString(); lblUShortMaxValue.Text = ushort.MaxValue.ToString(); lblIntMinValue.Text = int.MinValue.ToString(); lblIntMaxValue.Text = Int32.MaxValue.ToString(); } } }
Converting a Value to an Integer
While the Int32 structure is equipped to parse a value and get an integral value, the static Convert class is equipped with an overloaded method that can be used to convert a value to an integer. To let you convert a value to an integer, the Convert class provides an overloaded method named to ToInt32. If the value to convert is a string, such as one of the values the user would type in, or select from, a Windows controller, use the following version:
public static int ToInt32 (string value);
The method is also equipped to consider any other type. Therefore, based on the data types we have seen so far, you can use one of the following versions of the method:
public static int ToInt32 (byte value); public static int ToInt32 (short value);
If you don't know the type of the value you want to convert, the class offers the following version of the method:
public static int ToInt32 (object value);
In either case, pass the desired value to the method. If the method succeeds with the conversion, it would produce the integral format of the argument.
An unsigned integer is a positive natural number that can fit in up to 32 bits of the computer memory. To let you declare a variable for such a number, the C# language provides a data type named uint. The uint keyword is used for a 32-bit positive integer that is between 0 and 4,294,967,295.
To let you display an unsigned integer to the console screen, both the Console.Write() and the Console.WriteLine() methods each has a syntax that takes a uint argument. Their syntaxes are:
public static void Write(uint value); public static void WriteLine(uint value);
Here are examples:
using static System.Console;
public class Exercise
{
static void Main()
{
uint dayOfBirth;
uint monthOfBirth;
uint yearOfBirth;
dayOfBirth = 8;
monthOfBirth = 11;
yearOfBirth = 1996;
WriteLine("Red Oak High School");
Write("Student Date of Birth: ");
Write(monthOfBirth);
Write("/");
Write(dayOfBirth);
Write("/");
Write(yearOfBirth);
}
}
This would produce:
Red Oak High School Student Date of Birth: 11/8/1996
Once again, if the number is large, you can use the underscore to separate the thousands.
Operations on Integers
Integers support all of the four regular operations: addition, subtraction, multiplication, and division. When it comes to the division:
using static System.Console;
public class Program
{
public static void Main()
{
Write("Operation 240 / 1000 = ");
WriteLine(240 / 1000);
WriteLine("==========================");
}
}
This would produce:
Operation 240 / 1000 = 0 ========================== Press any key to continue . . .
using static System.Console; public class Program { public static void Main() { Write("Operation 1000 / 240 = "); WriteLine(1000 / 240); WriteLine("--------------------------"); Write("Operation 1226 / 317 = "); WriteLine(1226 / 317); WriteLine("=========================="); } }This would produce:
Operation 1000 / 240 = 4 -------------------------- Operation 1226 / 317 = 3 ========================== Press any key to continue . . .
Introduction
To store a very large number that cannot fit in a double-word, you can consider a combination of 64 bits. The group can also be referred to as a quad-word. A quad-word is very large. It can store numbers in the range of -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
If you declare an integer variable using the var keyword and initialize it with a value between 2,147,484,647 and 9,223,372,036,854,775,807, the compiler concludes that the memory needed to store that variable is 64 bits:
To let you use a variable that can hold very large numbers that may require up to 64 bits, the C# language provides a data type named long. You can use it to declare a variable.
To let you display an unsigned integer to the console screen, both the Console.Write() and the Console.WriteLine() methods each has a syntax that takes a long argument. Their syntaxes are:
public static void Write(long value); public static void WriteLine(long value);
Here is an example:
using static System.Console;
public class Exercise
{
public static int Main()
{
long areaSqrMiles = 3705407;
WriteLine("Country Statistics");
WriteLine("Area: " + areaSqrMiles + " Sqr Miles)");
return 0;
}
}
As always, you can also use the var keyword to declare the variable. When initializing the variable, don't use the official thousands separator. Instead, you can either type the number as it is or you can use the underscore(s) to separate the thousands. Here are examples:
using static System.Console;
public class Exercise
{
static int Main()
{
long areaSqrMiles = 3705407;
long areaSqrKms = 9_596_961;
long population = 1_403_500_365;
WriteLine("Country Statistics");
Write("Area: ");
Write(areaSqrKms);
Write(" Sqr Kms (");
Write(areaSqrMiles);
WriteLine(" Sqr Miles)");
Write("Population: ");
WriteLine(population);
return 0;
}
}
As stated previously, if you initialize the variable with a value lower than 2,147,484,647, the compiler would allocate 32 bits of memory for it. If you initialize the variable with a value between 2,147,484,647 and 9,223,372,036,854,775,807, the compiler would allocate 64 bits of memory for it. If the value is higher than 9,223,372,036,854,775,807, which is too large, the compiler would present an error.
This means that you should limit the values assigned to integral variables to 64 bits, which is very significant. Here is an example:
using static System.Console;
public class Exercise
{
static void Main()
{
var countryArea = 5638648;
Write("Country Area: ");
Write(countryArea);
Write("km2");
}
}
This would produce:
Country Area: 5638648km2 Press any key to continue . . .
As mentioned for other integral types, you can initialize a long variable with a hexadecimal value.
Although the long data type is used for large numbers, it mainly indicates the amount of space available but you don't have to use the whole space. For example, you can use the long keyword to declare a variable that would hold the same range of numbers as the short, the int, or the uint data types. If you declare a variable as long but use it for small numbers that don't require 64 bits, the compiler would allocate the appropriate amount of space to accommodate the values of the variable. Consequently, the amount of space made available may not be as large as 64 bits. If you insist and want the compiler to reserve 64 bits, when assigning a value to the variable, add an L suffix to it. Here is an example that uses a long variable to store a number that would fit in 32 bits:
using static System.Console;
public class Exercise
{
static void Main()
{
long countryArea;
countryArea = 5638648L;
Write("Country Area: ");
Write(countryArea);
Write("km2\n");
}
}
Therefore, keep in mind that an int, a uint, ashort, or a ushort can fit in a long variable.
.NET Long Integers
To support long integers, the .NET Framework provides a structure named Int64. That structure holds the characteristics of the long data type. Here are examples of accessing the minimum and the maximum values:
using System; using System.Windows.Forms; namespace ValuesCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblByteMinValueNatural.Text = byte.MinValue.ToString(); lblByteMinValueHex.Text = byte.MinValue.ToString("X"); lblByteMaxValueNatural.Text = Byte.MaxValue.ToString(); lblByteMaxValueHex.Text = Byte.MaxValue.ToString("X"); lblShortMinValueNatural.Text = short.MinValue.ToString(); lblShortMinValueHex.Text = "0x" + short.MinValue.ToString("X"); lblShortMaxValueNatural.Text = Int16.MaxValue.ToString(); lblShortMaxValueHex.Text = "0x" + Int16.MaxValue.ToString("X"); lblUShortMinValueNatural.Text = ushort.MinValue.ToString(); lblUShortMinValueHex.Text = "0x" + ushort.MinValue.ToString("X"); lblUShortMaxValueNatural.Text = ushort.MaxValue.ToString(); lblUShortMaxValueHex.Text = "0x" + ushort.MaxValue.ToString("X"); lblIntMinValueNatural.Text = Int32.MinValue.ToString("N"); lblIntMinValueHex.Text = "0x" + Int32.MinValue.ToString("X"); lblIntMaxValueNatural.Text = Int32.MaxValue.ToString("N"); lblIntMaxValueHex.Text = "0x" + Int32.MaxValue.ToString("X"); lblUIntMinValueNatural.Text = uint.MinValue.ToString(); lblUIntMinValueHex.Text = "0x" + uint.MinValue.ToString("X"); lblUIntMaxValueNatural.Text = uint.MaxValue.ToString("N"); lblUIntMaxValueHex.Text = "0x" + uint.MaxValue.ToString("X"); lblLongMinValueNatural.Text = long.MinValue.ToString("N"); lblLongMinValueHex.Text = "0x" + long.MinValue.ToString("X"); lblLongMaxValueNatural.Text = long.MaxValue.ToString("N"); lblLongMaxValueHex.Text = "0x" + long.MaxValue.ToString("X"); } } }
You can use a combination of 64 bits to store positive or negative integers. In some cases, you will need a variable to hold only positive, though large, numbers. To declare such a variable, you can use the ulong data type. A variable declared as ulong can handle extremely positive numbers that range from 0 to 18,446,744,073,709,551,615 to fit in 64 bits.
To let you display an unsigned long integer to the console screen, both the Console.Write() and the Console.WriteLine() methods each has a syntax that takes a long argument. Their syntaxes are:
public static void Write(ulong value); public static void WriteLine(ulong value);
Here is an example of accessing the minimum and the maximum values of an unsigned long integer:
using System; using System.Windows.Forms; namespace ValuesCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblByteMinValueNatural.Text = byte.MinValue.ToString(); lblByteMinValueHex.Text = byte.MinValue.ToString("X"); lblByteMaxValueNatural.Text = Byte.MaxValue.ToString(); lblByteMaxValueHex.Text = Byte.MaxValue.ToString("X"); lblShortMinValueNatural.Text = short.MinValue.ToString(); lblShortMinValueHex.Text = "0x" + short.MinValue.ToString("X"); lblShortMaxValueNatural.Text = Int16.MaxValue.ToString(); lblShortMaxValueHex.Text = "0x" + Int16.MaxValue.ToString("X"); lblUShortMinValueNatural.Text = ushort.MinValue.ToString(); lblUShortMinValueHex.Text = "0x" + ushort.MinValue.ToString("X"); lblUShortMaxValueNatural.Text = ushort.MaxValue.ToString(); lblUShortMaxValueHex.Text = "0x" + ushort.MaxValue.ToString("X"); lblIntMinValueNatural.Text = Int32.MinValue.ToString("N"); lblIntMinValueHex.Text = "0x" + Int32.MinValue.ToString("X"); lblIntMaxValueNatural.Text = Int32.MaxValue.ToString("N"); lblIntMaxValueHex.Text = "0x" + Int32.MaxValue.ToString("X"); lblUIntMinValueNatural.Text = uint.MinValue.ToString(); lblUIntMinValueHex.Text = "0x" + uint.MinValue.ToString("X"); lblUIntMaxValueNatural.Text = uint.MaxValue.ToString("N"); lblUIntMaxValueHex.Text = "0x" + uint.MaxValue.ToString("X"); lblLongMinValueNatural.Text = long.MinValue.ToString("N"); lblLongMinValueHex.Text = "0x" + long.MinValue.ToString("X"); lblLongMaxValueNatural.Text = long.MaxValue.ToString("N"); lblLongMaxValueHex.Text = "0x" + long.MaxValue.ToString("X"); lblULongMinValueNatural.Text = ulong.MinValue.ToString("N"); lblULongMinValueHex.Text = "0x" + ulong.MinValue.ToString("X"); lblULongMaxValueNatural.Text = ulong.MaxValue.ToString("N"); lblULongMaxValueHex.Text = "0x" + ulong.MaxValue.ToString("X"); } } }
Floating-Point Numbers
Introduction to Real Numbers
A real number is a number that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel:
On both sides of the decimal symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.
Both Microsoft Windows and .NET Framework provides various levels of support for real numbers.
Introduction to Floating-Point Numbers
The integers we have used so far have the main limitation of not allowing decimal values. C# provides floating values that would solve this problem. The most fundamental data type you can use for a floating-point number is called float. A variable declared as float can store real numbers that range from 3.402823e38 to 3.402823e38 in 32 bits. Here is an example:
class Exercise
{
static void Main()
{
float distance;
}
}
To let you display a floating-point number with single-precision to the console screen, both the Console.Write() and the Console.WriteLine() methods each has a syntax that takes a float argument. Their syntaxes are:
public static void Write(object value); public static void WriteLine(object value);
A Number with Single-Precision
As seen with integers, to support real numbers, the .NET Framework provides various structures. All the structures are created in the System namespace.
To support numbers with single-precision, the .NET Framework provides a structure named Single. The Single structure is used to support the float data type of the C# language. This means that the characteristics of the float data type are defined in the Single structure. All the characteristics of we reviewed for the structures associated with integers are also available for real numbers. This includes the ability to display a value by calling one of the versions of the overloaded ToString() methods that every number-associated structure is equipped with. The operations also include the ability to parse or to to try parsing a value to produce an appropriate real number.
Characteristics of Floating-Point Numbers
The Minimum Value of a Type
Like integers, floating-point numbers have limits so they can fit in the computer memory allocated for them. As seen for the structures of integers, to let you get the lowest value that a type can hold, each number-associated structure is equipped with a constant field named MinValue. For the float and Single type, this field is defined as follows:
public const float MinValue = -3.40282347E+38;
To get the lowest value of a type, you can access this field. Here is an example:
using System.Windows.Forms; namespace RealNumbers { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblFloatMinValueNatural.Text = float.MinValue.ToString("N"); lblFloatMinValueExp.Text = float.MinValue.ToString("E"); } } }
The Maximum Value of a Type
To let you get the highest value that a floating-point number can hold, their structure is equipped with a constant field named MaxValue. For the float type, this field is defined as follows:
public const float MaxValue = 3.40282347E+38;
To get the highest value of a type, access this field. Here is an example:
using System.Windows.Forms; namespace RealNumbers { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblFloatMinValueNatural.Text = float.MinValue.ToString("N"); lblFloatMinValueExp.Text = float.MinValue.ToString("E"); lblFloatMaxValueNatural.Text = float.MaxValue.ToString("N"); lblFloatMaxValueExp.Text = float.MaxValue.ToString("E"); } } }
When the Value is Not-A-Number
After declaring and initializing a float (or a Single) variable, it should hold an appropriate value. If you get the variable 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 let you check whether the variable is holding a value that is not a number, the Single structure of the floating-point type is equipped with a constant named NaN. To get its information, type the float data type, followed by the period operator, and followed by the NaN constant. Here is an example:
floatnumber = 0D;
if( number == double.NaN )
MessageboxShow("The value is not a number.";
Another technique you can use to check this characteristic is to call the IsNaN() method is the Single structure.
The Epsilon Constant
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 Single structure, this constant is named Epsilon. For the single-precision type, the epsilon is equal to 1.445.
The Negative Infinity
When dealing with real numbers, some operations produce very little or very large numbers. In algebra, the smallest conceptual number is called negative infinity (in reality, infinity is not a number but a concept). In the .NET Framework, the negative infinity is represented by a constant named NegativeInfinity. To access this number, type float, followed by a period operator, followed by the name of this constant.
To let you find out whether a variable holds a negative infinity value, the structures of the floating-point number is equipped with a method named IsNegativeInfinity. The syntax of this method is:
public static bool IsNegativeInfinity(float f);
The Positive Infinity
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, a period, and this constant. To find out if a variable's value is a positive infinity, you can call its IsPositiveInfinity() method. The syntax of this method ise:
public static bool IsPositiveInfinity(float f);
To check whether the value of a variable is one of the infinities, you can call its IsInfinity() method. The syntax of this method is:
public static bool IsInfinity(float f);
Double-Precision Numbers
Introduction
When a variable is larger than the float can handle and requires more precision, you should declare it using either the var or the double keyword.
To let you display a floating-point number with double-precision to the console screen, both the Console.Write() and the Console.WriteLine() methods each has a syntax that takes a double argument. Their syntaxes are:
public static void Write(double value); public static void WriteLine(double value);
Here is an example:
using static System.Console;
public class Exercise
{
static void Main()
{
var number = 62834.9023;
WriteLine("Number: " + number);
}
}
This would produce:
Number: 62834.9023 Press any key to continue . . .
A variable declared as double uses 64 bits to store very large numbers ranging from 1.79769313486232e308 to 1.79769313486232e308 with a precision of 15 or 16 digits.
A Double-Precision Number
To support numbers with double-precision, the .NET Framework provides a structure named Double. This structure is equipped with the same members as the Single structure. These include the minimum and the maximum values. Here are examples:
using System.Windows.Forms; namespace RealNumbers { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblFloatMinValueNatural.Text = float.MinValue.ToString("N"); lblFloatMinValueExp.Text = float.MinValue.ToString("E"); lblFloatMaxValueNatural.Text = float.MaxValue.ToString("N"); lblFloatMaxValueExp.Text = float.MaxValue.ToString("E"); lblDoubleMinValueNatural.Text = double.MinValue.ToString("N"); lblDoubleMinValueExp.Text = double.MinValue.ToString("E"); lblDoubleMaxValueNatural.Text = double.MaxValue.ToString("N"); lblDoubleMaxValueExp.Text = double.MaxValue.ToString("E"); } } }
Characteristics of a Double-Precision Value
The Epsilon Constant
The double data type uses the same characteristics we reviewed for the float data type and its accompanying Single structure. The members of their structures are the same, Only some values are different. As we saw, the smallest positive number is the epsilon. For a double-precision type, the epsilon constant is equivalent to 4.94065645841247-324.
A Better Floating-Point Number
Because the double data type provides a better result with better precision than the float, whenever you declare a variable using either the var or the float keyword and assign it a value, the compiler allocates 64 bits to store the values of the variable. If you insist on the variable being treated as float, when assigning it a value, add an f or an F suffix to the value. Here is an example:
using static System.Console;
public class Exercise
{
static void Main()
{
float distance;
distance = 248.38F;
Write("Distance = ");
Write(distance);
WriteLine("km\n");
}
}
This would produce:
Distance = 248.38km
Remember that if you declare the variable as var and want to treat it as a value with single precision, add an f or an F suffix to the value assigned to it. Here is an example:
using static System.Console;
public class Exercise
{
static void Main()
{
var number = 62834.9023F;
Write("Number: ");
WriteLine(number);
}
}
On the other hand, if you want a value to be treated with double-precision, add a d or a D suffix to it. Here is an example:
class Exercise
{
static void Main()
{
var number = 62834.9023D;
Write("Number: ");
WriteLine(number);
}
}
Introduction
To support small to extremely large numbers, the C# language provides a data type named decimal. Use that keyword to declare a variable that would hold significantly large values that can be stored in a combination of 128 bits. The values stored in a decimal variable can range from -79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335.
Initializing a Decimal
After declaring a decimal variable, you can initialize it with a natural number. To indicate that the variable holds a decimal value, when initializing it, add an m or an M suffix to its value.
To let you display a decimal number to the console screen, both the Console.Write() and the Console.WriteLine() methods each has a syntax that takes a decimal argument. Their syntaxes are:
public static void Write(decimal value); public static void WriteLine(decimal value);
Here is an example:
class Exercise { static void Main() { decimal hourlySalary; hourlySalary = 24.25M; Write("Hourly Salary = "); WriteLine(hourlySalary); WriteLine(); } }
This would produce:
Hourly Salary = 24
Decimal Number Support
To support numeric values with high precision, the .NET Framework provides a structure named Decimal. This structure provides the characteristics of the C#'s decimal type. The structure has the same members we reviewed for the Single and the Double structures.
Distinguishing the Floating-Point Variables
As seen in previous sections and this one, when declaring and initializing a real variable, the suffix you give to its assigned value indicates to the compiler the actual type of value and the type of memory that would be allocated for the variable:
Consider the following program:
using static System.Console; public class Program { static void Main() { Write("560 / 672 = "); WriteLine(560 / 672); } }
The purpose of this program is to get the division of 560 by 672. This would produce:
560 / 672 = 0 Press any key to continue . . .
Notice that, when such numbers are submitted to the program, the compiler decides that these are integers and the result produces 0. If you write such an equation and want the result to appear as a real number, you must explicitly indicate it to the compiler. One way you can do this consists of adding decimal places to at least one of the numbers. Here is an example:
using static System.Console; public class Program { static void Main() { Write("560 / 672 = "); WriteLine(560.00 / 672.00); } }
This time, the compiler will conclude that the value is a floating-point number; but which one? By default, the compiler would apply the double data type. This version of the program would produce:
560 / 672 = 0.833333333333333 Press any key to continue . . .
If you want to indicate that the value is a float, a double, or a decimal, add the appropriate prefix to it: f, F, d, D, m, or M. Here are examples:
using static System.Console; public class Program { static void Main() { Write("560 / 672 = "); WriteLine(560F / 672f); } }
This version of the program would produce:
560 / 672 = 0.8333333 Press any key to continue . . .
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2001-2021, FunctionX | Next |
|