Author:水如烟
大数的运算考虑只限于加、乘,暂不考虑减、除。
除比较麻烦。
上文方法中,将字串分解为字符来运算,耗内存,速度也慢。
现在考虑将字串分解为特定字串来运算。利用系统的加、乘运算。也就是确定运算单元。
利用.NET提供的数据类型,来确定特定字串的字符数。
Public
Class
Form1
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TestSystemType.Test()
End Sub
End Class
' 寻找最大的数值类型,其中某个数的平方其结果仍表示为整数
Public Class TestSystemType
Private Sub New ()
End Sub
Public Shared Sub Test()
Dim mTypeName As String
Dim mType As Type
Dim mFieldInfo As Reflection.FieldInfo
Dim b As New System.Text.StringBuilder
For Each c As TypeCode In [ Enum ].GetValues( GetType (TypeCode))
mTypeName = c.ToString
mType = Type.GetType( String .Format( " System.{0} " , mTypeName))
mFieldInfo = mType.GetField( " MaxValue " )
If mFieldInfo Is Nothing Then Continue For
b.AppendLine()
b.AppendLine(mTypeName)
b.AppendLine(mFieldInfo.GetValue( Nothing ).ToString)
Next
Console.Write(b.ToString)
End Sub
End Class
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TestSystemType.Test()
End Sub
End Class
' 寻找最大的数值类型,其中某个数的平方其结果仍表示为整数
Public Class TestSystemType
Private Sub New ()
End Sub
Public Shared Sub Test()
Dim mTypeName As String
Dim mType As Type
Dim mFieldInfo As Reflection.FieldInfo
Dim b As New System.Text.StringBuilder
For Each c As TypeCode In [ Enum ].GetValues( GetType (TypeCode))
mTypeName = c.ToString
mType = Type.GetType( String .Format( " System.{0} " , mTypeName))
mFieldInfo = mType.GetField( " MaxValue " )
If mFieldInfo Is Nothing Then Continue For
b.AppendLine()
b.AppendLine(mTypeName)
b.AppendLine(mFieldInfo.GetValue( Nothing ).ToString)
Next
Console.Write(b.ToString)
End Sub
End Class
测试结果:
Char
?
SByte
127
Byte
255
Int16
32767
UInt16
65535
Int32
2147483647
UInt32
4294967295
Int64
9223372036854775807
UInt64
18446744073709551615
Single
3.402823E+38
Double
1.79769313486232E+308
Decimal
79228162514264337593543950335
DateTime
9999-12-31 23:59:59
可以确定,利用的是Decimal数据类型。
Public
Class
Form1
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Decimal = Decimal .MaxValue
x = CDec (Math.Sqrt(x))
Console.WriteLine(x)
Dim mDigits As Integer = x.ToString.Length
Console.WriteLine(mDigits)
mDigits -= 1
Console.WriteLine(mDigits)
Dim mMaxUnitString As String = New String ( " 9 " c, mDigits)
Console.WriteLine(mMaxUnitString)
x = Decimal .Parse(mMaxUnitString)
x = x * x
Console.WriteLine(x)
End Sub
End Class
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Decimal = Decimal .MaxValue
x = CDec (Math.Sqrt(x))
Console.WriteLine(x)
Dim mDigits As Integer = x.ToString.Length
Console.WriteLine(mDigits)
mDigits -= 1
Console.WriteLine(mDigits)
Dim mMaxUnitString As String = New String ( " 9 " c, mDigits)
Console.WriteLine(mMaxUnitString)
x = Decimal .Parse(mMaxUnitString)
x = x * x
Console.WriteLine(x)
End Sub
End Class
结果:
281474976710656
15
14
99999999999999
9999999999999800000000000001
现在确定,运算单元利用Decimal类型,定义字串为14位。