22: Floating-Point Types in Visual C#.NET

Generate Denso QR Bar Code in Visual C#.NET 22: Floating-Point Types
TABLE 22: Floating-Point Types
Printing QR Code In Visual C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create QR Code image in Visual Studio .NET applications.
Type
Encode Barcode In Visual C#
Using Barcode generation for .NET framework Control to generate, create bar code image in .NET applications.
float
QR Code Creator In .NET
Using Barcode maker for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications.
Size
Encode QR Code ISO/IEC18004 In VS .NET
Using Barcode printer for VS .NET Control to generate, create Denso QR Bar Code image in .NET applications.
Range (Inclusive)
Drawing QR-Code In Visual Basic .NET
Using Barcode maker for .NET Control to generate, create QR Code image in Visual Studio .NET applications.
BCL Name
Barcode Printer In Visual C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create bar code image in Visual Studio .NET applications.
SystemSingle
Print Bar Code In C#
Using Barcode encoder for .NET Control to generate, create bar code image in .NET framework applications.
Significant Digits
Code 3 Of 9 Creator In C#
Using Barcode printer for Visual Studio .NET Control to generate, create Code 39 Extended image in .NET framework applications.
32 bits 64 bits
Draw Code 128 Code Set A In C#.NET
Using Barcode generator for VS .NET Control to generate, create Code 128A image in .NET applications.
15 10 45 to 34 1038 50 10 324 to 17 10308
ECC200 Creator In Visual C#
Using Barcode generator for VS .NET Control to generate, create Data Matrix image in VS .NET applications.
7 15 16
Print Code39 In VS .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Code 3/9 image in .NET applications.
double
Paint Code 128A In VB.NET
Using Barcode drawer for VS .NET Control to generate, create ANSI/AIM Code 128 image in VS .NET applications.
SystemDouble
Code 39 Full ASCII Drawer In Java
Using Barcode drawer for Java Control to generate, create Code 39 image in Java applications.
Binary numbers appear as base 10 (denary) numbers for human readability The number of bits (binary digits) converts to 15 decimal digits,
Print Code 3/9 In .NET
Using Barcode generator for ASP.NET Control to generate, create Code-39 image in ASP.NET applications.
2: Data Types
Bar Code Generator In Java
Using Barcode drawer for Java Control to generate, create bar code image in Java applications.
with a remainder that contributes to a sixteenth decimal digit as expressed in Table 22 Specifically, numbers between 17 * 10307 and less than 1 * 10308 have only 15 significant digits However, numbers ranging from 1 * 10308 to 17 * 10308 will have 16 significant digits A similar range of significant digits occurs with the decimal type as well
GS1-128 Creator In VS .NET
Using Barcode generator for .NET Control to generate, create USS-128 image in VS .NET applications.
Decimal Type C# contains a numeric type with 128-bit precision (see Table 23) This is suitable for large and precise calculations, frequently financial calculations
Making Code 128 Code Set B In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create Code 128 image in ASP.NET applications.
TABLE 23: decimal Type
Scan Data Matrix 2d Barcode In .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
Type
Making Data Matrix In Java
Using Barcode printer for Java Control to generate, create DataMatrix image in Java applications.
decimal
Barcode Maker In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create barcode image in .NET applications.
Size
Range (Inclusive)
BCL Name
SystemDecimal
Significant Digits
128 bits
10 10 28 to approximately 79 1028
28 29
Unlike floating-point numbers, the decimal type maintains exact accuracy for all denary numbers within its range With the decimal type, therefore, a value of 01 is exactly 01 However, while the decimal type has greater precision than the floating-point types, it has a smaller range Thus, conversions from floating-point types to the decimal type may result in overflow errors Also, calculations with decimal are slightly slower
ADVANCED TOPIC Floating-Point Types and Decimals Dissected Unless they are out of range, decimal numbers represent denary numbers exactly In contrast, the floating-point representation of many denary numbers introduces a rounding error This is analogous to how 1/3 is not exact in any finite number of decimal digits and 11/10 is not precise in any finite number of binary digits In both cases, we end up with a rounding error of some kind The difference between the decimal type and the C# floating-point types is that the base of a decimal type is a denary and the base of floating-point types is binary
Fundamental Numeric Types
A decimal is represented by N * 10k where
N, the mantissa, is a positive integer represented by 96 bits k, the exponent, is given by -28 <= k <= 0
In contrast, a float is any number N * 2k where
N is a positive integer represented by a fixed number of bits (24 for float and 53 for double) k is any integer ranging from -149 to +104 for float and -1075 to +970
for double
Literal Values A literal value is a representation of a constant value within source code For example, if you want to have SystemConsoleWriteLine() print out the integer value 42 and the double value 1618034 (Phi), you could use the code shown in Listing 21
Listing 21: Specifying Literal Values
SystemConsoleWriteLine(42); SystemConsoleWriteLine(1618034);
Output 21 shows the results of Listing 21
OUTPUT 21:
42 1618034
BEGINNER TOPIC Use Caution When Hardcoding Values The practice of placing a value directly into source code is called hardcoding, because changing the values means recompiling the code Developers must carefully consider the choice between hardcoding values within their code and retrieving them from an external source, such as a configuration file, so that the values are modifiable without recompiling
2: Data Types
By default, when you specify a literal number with a decimal point, the compiler interprets it as a double type Conversely, a literal value with no decimal point generally defaults to an int, assuming the value is not too large to be stored in an integer If the value is too large, then the compiler will interpret it as a long Furthermore, the C# compiler allows assignment to a numeric type other than an int, assuming the literal value is appropriate for the target data type short s = 42 and byte b = 77 are allowed, for example However, this is appropriate only for literal values; b = s is not appropriate without additional syntax, as discussed in the section Conversions between Data Types, later in this chapter As previously discussed in the section Fundamental Numeric Types, there are many different numeric types in C# In Listing 22, a literal value is placed within C# code Since numbers with a decimal point will default to the double data type, the output, shown in Output 22, is 161803398874989 (the last digit, 5, is missing), corresponding to the expected accuracy of a double