' Visual Basic 2008 9.0 .NET Examples - Floating Point Notation - Single Double Decimal - Introduction to Visual Basic : Module Example1 Sub Main() Dim units As Single() = _ {500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01} ' Dim amount As Single = 0.06 Console.Write(amount & " : ") ' Dim index As Integer Do While amount > 0 Do While amount - units(index) >= 0 Console.Write(units(index) & " ") amount -= units(index) Loop index += 1 Loop Console.WriteLine() ' Console.ReadLine() End Sub End Module Module Example2 Sub Main() Console.WriteLine(2.0 Mod 0.2 = 0) Console.WriteLine(2.0 Mod 0.2) ' Dim someSingle As Single = 4.99 Console.WriteLine(someSingle * 17 = 84.83) Console.WriteLine(someSingle * 17) ' someSingle = 1 / 107.0 Console.WriteLine(someSingle * 107 = 1) Console.WriteLine(someSingle * 107) ' Console.ReadLine() End Sub End Module Module Example3 Public Sub Main() Console.WriteLine("seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm") Console.WriteLine(GetBinary(1.17549435E-38F) & " : " & _ 1.17549435E-38F.ToString()) Console.WriteLine(GetBinary(0.5F) & " : " & 0.5F.ToString()) Console.WriteLine(GetBinary(0.6F) & " : " & 0.6F.ToString()) Console.WriteLine(GetBinary(Single.MaxValue) & " : " & _ Single.MaxValue.ToString()) Console.WriteLine(GetBinary(-1.17549435E-38F) & " : " & _ -1.17549435E-38F.ToString()) Console.WriteLine(GetBinary(Single.MinValue) & " : " & _ Single.MinValue.ToString()) Console.WriteLine(GetBinary(0.0F) & " : " & 0.0F.ToString()) Console.WriteLine(GetBinary(-0.0F) & " : " & -0.0F.ToString()) Console.WriteLine(GetBinary(Single.Epsilon) & " : " & _ Single.Epsilon.ToString()) Console.WriteLine(GetBinary(-1.401298E-45F) & " : " & _ -1.401298E-45F.ToString()) Console.WriteLine(GetBinary(Single.PositiveInfinity) & " : " & _ Single.PositiveInfinity.ToString()) Console.WriteLine(GetBinary(Single.NegativeInfinity) & " : " & _ Single.NegativeInfinity.ToString()) ' Console.ReadLine() End Sub Public Function GetBinary(ByVal value As Byte) As String For counter As Integer = 1 To 8 GetBinary = (value Mod 2).ToString() & GetBinary value >>= 1 Next End Function Public Function GetBinary(ByVal value As Single) As String If BitConverter.IsLittleEndian Then For Each byteElement As Byte In BitConverter.GetBytes(value) GetBinary = GetBinary(byteElement) & GetBinary Next Else Throw New ApplicationException("Only Little Endian supported.") End If End Function End Module Module Example4 Sub Main() Dim singleOperands As Single() = {Single.PositiveInfinity, _ Single.NegativeInfinity, _ 123.0F, -123.0F, 0.0F, -0.0F} Dim operatorSymbols As String() = {"*", "/", "+", "-"} ' For Each operatorSymbol As String In operatorSymbols Console.WriteLine("OPERATOR " & operatorSymbol.ToString()) Console.WriteLine() For Each singleOperand1 As Single In singleOperands For Each singleOperand2 As Single In singleOperands PrintCalculation(singleOperand1, operatorSymbol, _ singleOperand2) Next Console.WriteLine() Next Console.WriteLine() Next ' Console.ReadLine() End Sub Sub PrintCalculation(ByVal operand1 As Single, _ ByVal operatorSymbol As String, _ ByVal operand2 As Single) Console.Write(GetString(operand1) & " " & operatorSymbol & " " & _ GetString(operand2) & " = ") Select Case operatorSymbol Case "*" Console.WriteLine(GetString(operand1 * operand2)) Case "/" Console.WriteLine(GetString(operand1 / operand2)) Case "+" Console.WriteLine(GetString(operand1 + operand2)) Case "-" Console.WriteLine(GetString(operand1 - operand2)) End Select End Sub Function GetString(ByVal value As Single) As String If IsPositiveZero(value) Then GetString = "+0" ElseIf IsNegativeZero(value) Then GetString = "-0" ElseIf Single.IsNegativeInfinity(value) Then GetString = "-Infinity" ElseIf Single.IsPositiveInfinity(value) Then GetString = "+Infinity" ElseIf Single.IsNaN(value) Then GetString = "NaN" Else GetString = value.ToString() End If End Function Public Function IsPositiveZero(ByVal value As Single) As Boolean If BitConverter.GetBytes(value)(0) = 0 AndAlso _ BitConverter.GetBytes(value)(1) = 0 AndAlso _ BitConverter.GetBytes(value)(2) = 0 AndAlso _ BitConverter.GetBytes(value)(3) = 0 Then _ IsPositiveZero = True End Function Public Function IsNegativeZero(ByVal value As Single) As Boolean If BitConverter.GetBytes(value)(0) = 0 AndAlso _ BitConverter.GetBytes(value)(1) = 0 AndAlso _ BitConverter.GetBytes(value)(2) = 0 AndAlso _ BitConverter.GetBytes(value)(3) = 128 Then _ IsNegativeZero = True End Function End Module ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.