VB.Net Tutorial/Data Type/Single
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
Calcuate Single
Public Class Tester
Public Shared Sub Main
Dim magnitude As Single
Dim radians As Single
magnitude = CSng(Math.Sqrt(100 ^ 2 + 120 ^ 2))
radians = CSng(Math.Atan2(100, 123))
Console.WriteLine(magnitude)
Console.WriteLine(radians)
End Sub
End Class
156.205 0.6826226
Calculation with Single
public class Test
public Shared Sub Main
Dim Amount As Single
Dim Discount As Single
Dim DiscAmount As Single
Amount = 24500
Discount = 0.35
DiscAmount = Amount * (1 - Discount)
Console.WriteLine("Your price is: $" & DiscAmount)
End Sub
End class
Your price is: $15925
Cast Integer to Short, Double to Single
Module Module1
Sub Main()
Dim BigInteger As Integer = 10000
Dim LittleInteger As Short
Dim BigFloat As Double = 0.123456789
Dim LittleFloat As Single
LittleInteger = BigInteger
LittleFloat = BigFloat
Console.WriteLine(LittleInteger)
Console.WriteLine(LittleFloat)
End Sub
End Module
10000 0.1234568
Get Epsilon from Single
public class Test
public Shared Sub Main
Console.WriteLine(Single.Epsilon)
End Sub
End class
1.401298E-45
Pass Single by value and by reference to a Sub
public class Test
public Shared Sub Main
Dim value As Single
value = 10
DoubleItByVal(value)
Console.WriteLine(value)
value = 10
DoubleItByRef(value)
Console.WriteLine(value)
End Sub
Shared Sub DoubleItByVal(ByVal X As Single)
X *= 2
End Sub
Shared Sub DoubleItByRef(ByRef X As Single)
X *= 2
End Sub
End class
10 20
Precision Error
Module Module1
Sub Main()
Dim A As Single
"Dim A As Double
For A = 0.01 To 0.1 Step 0.01
If (A = 0.05) Then
Console.WriteLine("Reached 0.05")
End If
Next
Console.WriteLine("Done with loop")
End Sub
End Module
Done with loop
Show difference precision of Single and Double
Module Module1
Sub Main()
Dim A As Single = 0.123456789012345
Dim B As Double = 0.123456789012345
Console.WriteLine("Single: " & A)
Console.WriteLine("Double: " & B)
End Sub
End Module
Single: 0.1234568 Double: 0.123456789012345
Single Double MaxValue and Memory Size
Public Class Tester
Public Shared Sub Main
Dim result As New System.Text.StringBuilder
Dim maxSingle As Single = Single.MaxValue
Dim maxDouble As Double = Double.MaxValue
Dim sizeOfSingle As Integer = Runtime.InteropServices.Marshal.SizeOf(maxSingle.GetType)
Dim sizeOfDouble As Integer = Runtime.InteropServices.Marshal.SizeOf(maxDouble.GetType)
result.Append("Memory size of a Single (bytes): ")
result.AppendLine(sizeOfSingle)
result.Append("Maximum value of a Single: ")
result.AppendLine(maxSingle)
result.AppendLine()
result.Append("Memory size of a Double (bytes): ")
result.AppendLine(sizeOfDouble)
result.Append("Maximum value of a Double: ")
result.AppendLine(maxDouble)
Console.WriteLine(result.ToString())
End Sub
End Class
Memory size of a Single (bytes): 4 Maximum value of a Single: 3.402823E+38 Memory size of a Double (bytes): 8 Maximum value of a Double: 1.79769313486232E+308