梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
Fractional number one and a half
Public Sub AddFractionalNumbersToWhole�┌�
Dim total As Integer = CType��1。5�察�Integer�� �� _
CType��1。2�察�Integer��
If ��total 2。7�� Then
Console。WriteLine�─�Oops�察�something went wrong;��
End If
End Sub
Using Ctype�┌� means to cast a Double
to an Integer。 This results in a
roundoff where 1。2 = 1 and 1。5 = 2。
Had DirectCast�┌� been used�察�a
pilation error would result�察�since
converting a Double to an Integer
requires coercion。
Figure 2´15。 Adding fractions using the Integer data type
Declaring a number with a decimal
A really small number
�─�0�� automatically creates a floating
point value
Public Sub AddFractionalNumbers�┌�
Dim value As Single = 10000。0 �� 0。000001
Console。WriteLine�─�Value �┌�0����;�察�value��
End Sub
The console displays the number
The variable value is 10��000 because the small number is
the addition of a big insignificant from the perspective of
number to a small Single。 Single can remember only 7
number digits。 10000。000001 is 10 digits of
precision。
Figure 2´16。 Adding fractions using the Single data type
´´´´´´´´´´´´´´´´´´´´´´Page 69´´´´´´´´´´´´´´´´´´´´´´´
CH A PT E R 2 * L E A R N I N G A B OU T 。 N E T N U M B E R AN D V A L U E T Y P E S 47
Finishing the Calculator
The original declaration of the Add�┌� method for the calculator worked�察�but had some serious
limitations on what kinds of numbers could be added。 To finish the calculator�察�we need to
declare the Add�┌� method using a different type�察�and then add the remaining operations。
We could use one of three types to declare the Add�┌� method�此�
o Long�此�Solves the problem of adding two very large numbers like 2 billion�察�but has the
problem that you cannot add fractional numbers like 1。5 plus 1。5。
o Double�此�Solves the problem of adding two very large or small numbers�察�and can be used
to add fractional numbers。 Generally speaking�察�Double is a good choice�察�but can suffer
from significance problems if a very large number is manipulated by a very small number。
o Decimal�此�A generally good approach and suitable for all types of precision�察�but also the
slowest when adding�察�subtracting�察�or performing other mathematical operations。
The simplest all´around numeric data type to use is Double�察�as it provides good precision
and is relatively fast。 The plete implementation of the calculator is as follows�此�
Public Class Operations
Public Shared Function Add��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 �� number2
End Function
Public Shared Function Divide��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 / number2
End Function
Public Shared Function Multiply��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 * number2
End Function
Public Shared Function Subtract��ByVal number1 As Double�察�ByVal number2 As _
Double�� As Double
Return number1 number2
End Function
End Class
The four operations are methods with different identifiers�察�but identical method signa
tures�察�making it easy to remember how to use each method。 Each of the operations would have
an appropriate set of tests verifying the correctness of the implementation。 The tests are not
reproduced here�察�but they are implemented in the sample source code。 I advise you to take a
quick look at the tests to make sure you understand the individual pieces。
´´´´´´´´´´´´´´´´´´´´´´Page 70´´´´´´´´´´´´´´´´´´´´´´´
48 CH AP T E R 2 * L E A R N IN G AB OU T 。 N E T N U M B E R A N D V A L U E T Y P E S
The Important Stuff to Remember
In this chapter�察�you learned about developing a class library that is used to perform some
calculations。 The following are the key points to remember�此�
o Organization of your thoughts�察�projects�察�and features makes all the difference when
writing software。
o When writing software�察�stay focused。 It is very easy to drift around in software development�察 �
because software lets you stray easily。 A successful developer will always be organized
and focused。
o Software is designed using an architecture that could be implemented top´down or
bottom´up。
o Within an architecture�察�individual pieces are called ponents�察�and they fit together to
create