我试图制作一个无限期生成Pascal三角形行的程序。我正在使用整数,单曲等,但不断得到溢出和“Infinity”的值,所以我尝试使用biginteger,现在它给了我不同的结果。我正在使用visual studio 2012 这是我的前几个整数数据类型的输出: 1, 1,1, 1,2,1, 1,3,3,1,
这是我用biginteger数据类型的前几个输出: 1, 1,1, 1,2,0, 1,3,3,0,
我唯一改变的是数据类型。以下是相关位的代码:
n = rows
val(0) = 1
For k = 1 To rows
val(k) = val(k - 1) * (n / k)
n -= 1
Next
rows是当前行(我在timer_tick上有这个,所以它可以无限期地运行,每行的行增加1) 如何让biginteger数据类型返回与整数数据类型相同的值?
答案 0 :(得分:0)
编辑,因为我意识到stackoverflow没有使你的换行符,Integer是32位而bigint是64位。当涉及更多数字时,分数可以在不同方向上四舍五入。
为了轻松避免舍入错误,您可以消除代码中的分割。
例如。)这里我们有一个正在运行的字符串和临时字符串。我们将临时字符串拆分为仅使用加法和减法来计算下一行。
Dim c As String = "1"
Dim temp As New StringBuilder
Dim f As New StringBuilder
temp.Append(c)
f.Append(temp.ToString)
For n = 1 To 10
Dim a() As String = c.Split(",")
For x = LBound(a) To UBound(a)
If UBound(a) - LBound(a) >= 1 Then
If x = UBound(a) Then
Dim v1 As UInt32 = a(x - 1)
Dim v2 As UInt32 = a(x)
temp.Append((v1) + (v2) & "," & 1)
c = temp.ToString
f.Append(vbCrLf & temp.ToString)
temp.Remove(0, temp.Length)
ElseIf x = LBound(a) Then
Dim v1 As UInt32 = a(x)
Dim v2 As UInt32 = a(x + 1)
temp.Append(1 & "," & (v1) + (v2) & ",")
ElseIf x < UBound(a) - 1 Then
Dim v1 As UInt32 = a(x)
Dim v2 As UInt32 = a(x + 1)
temp.Append((v1) + (v2) & ",")
End If
Else
temp.Remove(0, temp.Length)
temp.Append("1,1")
f.Append(vbCrLf & temp.ToString)
temp.Remove(0, temp.Length)
temp.Append("1,2,1")
f.Append(vbCrLf & temp.ToString)
c = temp.ToString
temp.Remove(0, temp.Length)
End If
Next
Next
MsgBox(f.ToString)