• R/O
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythonbathyscaphephpgamewindowsguic翻訳omegattwitterframeworktestbtronarduinovb.net計画中(planning stage)directxpreviewerゲームエンジンdom

πの計算を行う。


コミットメタ情報

リビジョン4 (tree)
日時2014-09-12 21:16:01
作者bellyoshi

ログメッセージ

大きい数値をクラス化

変更サマリ

差分

--- CALCPI/CalcPI.Test/LongNumTest.vb (revision 3)
+++ CALCPI/CalcPI.Test/LongNumTest.vb (revision 4)
@@ -59,15 +59,13 @@
5959 '''</summary>
6060 <TestMethod()> _
6161 Public Sub ldivTest1()
62- Dim target As LongNum = New LongNum() ' TODO: 適切な値に初期化してください
63- Dim d() As Integer = {2, 0, 0, 0}
62+
63+ Dim d As New LongNum(2)
6464 Dim e As Integer = 2
65- Dim f() As Integer = {0, 0, 0, 0}
66- target.ldiv(d, e, f)
67- Assert.AreEqual(1, f(0))
68- Assert.AreEqual(0, f(1))
69- Assert.AreEqual(0, f(2))
70- Assert.AreEqual(0, f(3))
65+ Dim f As New LongNum(0)
66+ d.ldiv(e, f)
67+ Assert.AreEqual(1, f.GetValue(0))
68+ Assert.AreEqual(0, f.GetValue(1))
7169 End Sub
7270
7371 '''<summary>
@@ -74,15 +72,14 @@
7472 '''ldiv のテスト
7573 '''</summary>
7674 <TestMethod()> _
77- Public Sub ldivTest2()
78- Dim target As LongNum = New LongNum() ' TODO: 適切な値に初期化してください
79- Dim d() As Integer = {4 * 239, 0, 0, 0}
75+ Public Sub ldivTest2()
76+ Dim d As New LongNum(4 * 239)
8077 Dim e As Integer = 239 * 239
81- Dim f() As Integer = {0, 0, 0, 0}
82- target.ldiv(d, e, f)
83- Assert.AreEqual(0, f(0))
84- Assert.AreEqual(1673640, f(1))
85- Assert.AreEqual(16736401, f(2))
86- Assert.AreEqual(67364016, f(3))
78+ Dim f As New LongNum(0)
79+ d.ldiv(e, f)
80+ Assert.AreEqual(0, f.GetValue(0))
81+ Assert.AreEqual(1673640, f.GetValue(1))
82+ Assert.AreEqual(16736401, f.GetValue(2))
83+ Assert.AreEqual(67364016, f.GetValue(3))
8784 End Sub
8885 End Class
--- CALCPI/CALCPI/LongNum.vb (revision 3)
+++ CALCPI/CALCPI/LongNum.vb (revision 4)
@@ -4,7 +4,7 @@
44 ''' 算出桁数
55 ''' </summary>
66 ''' <remarks></remarks>
7- Public Const L As Integer = 10000
7+ Public Const L As Integer = 1000
88
99 ''' <summary>
1010 ''' 配列サイズ
@@ -13,12 +13,6 @@
1313 Private Const L1 As Integer = (L / 8) + 1
1414
1515 ''' <summary>
16- ''' 配列サイズ + 1
17- ''' </summary>
18- ''' <remarks></remarks>
19- Public Const L2 As Integer = L1 + 1
20-
21- ''' <summary>
2216 ''' 計算項数
2317 ''' </summary>
2418 ''' <remarks></remarks>
@@ -25,22 +19,43 @@
2519 Public Const N As Integer = (L / 1.39794) + 1
2620
2721
22+ Private array(L) As Integer
2823
29- Public Function displayString(ByVal s() As Integer) As String
24+ Public Function GetValue(ByVal idx As Integer) As Integer
25+ Return array(idx)
26+ End Function
27+
28+
29+ Public Sub New(ByVal initialValue As Integer)
30+ Initialize(initialValue)
31+ End Sub
32+
33+ Private Sub Initialize(ByVal value As Integer)
34+ array(0) = value
35+ For k As Integer = 1 To array.Length - 1
36+ array(k) = 0
37+ Next
38+ End Sub
39+
40+
41+
42+ Public Function displayString() As String
3043 Dim sb As New System.Text.StringBuilder
31- sb.Append(s(0))
44+ sb.Append(array(0))
3245 sb.Append(".")
33- For i As Integer = 1 To s.Length - 1
34- sb.Append(s(i))
46+ For i As Integer = 1 To array.Length - 1
47+ sb.Append(array(i))
3548 sb.Append(" ")
3649 Next
3750 Return sb.ToString
3851 End Function
3952
40- Public Sub ladd(ByVal a() As Integer, ByVal b() As Integer, ByVal c() As Integer)
41-
53+ Public Sub ladd(opeLongNum As LongNum, outLongNum As LongNum)
54+ Dim b() As Integer = opeLongNum.array
55+ Dim c() As Integer = outLongNum.array
56+ Dim a() As Integer = array
4257 Dim carry As Integer = 0
43- For i As Integer = L2 To 0 Step -1
58+ For i As Integer = array.Length - 1 To 0 Step -1
4459 c(i) = a(i) + b(i) + carry
4560 If (c(i) < 100000000) Then
4661 carry = 0
@@ -54,9 +69,12 @@
5469
5570
5671
57- Public Sub lsub(ByVal a() As Integer, ByVal b() As Integer, ByVal c() As Integer)
72+ Public Sub lsub(opeLongNum As LongNum, outLongNum As LongNum)
73+ Dim b() As Integer = opeLongNum.array
74+ Dim c() As Integer = outLongNum.array
75+ Dim a() As Integer = array
5876 Dim borrow As Integer = 0
59- For i As Integer = L2 To 0 Step -1
77+ For i As Integer = array.Length - 1 To 0 Step -1
6078 c(i) = a(i) - b(i) - borrow
6179 If (c(i) >= 0) Then
6280 borrow = 0
@@ -66,11 +84,14 @@
6684 End If
6785 Next
6886
69- End Sub
87+ End Sub
7088
71- Public Sub ldiv(ByVal d() As Integer, ByVal e As Integer, ByVal f() As Integer)
89+ Public Sub ldiv(ByVal e As Integer, ByVal outLongNum As LongNum)
90+
91+ Dim d() As Integer = array
92+ Dim f() As Integer = outLongNum.array
7293 Dim r As Long = 0
73- For i As Integer = 0 To d.Length - 1
94+ For i As Integer = 0 To array.Length - 1
7495 Dim w As Long = d(i)
7596 f(i) = (w + r) \ e
7697 r = ((w + r) Mod e) * 100000000
--- CALCPI/CALCPI/Module1.vb (revision 3)
+++ CALCPI/CALCPI/Module1.vb (revision 4)
@@ -1,16 +1,16 @@
11 Module Module1
22
3- Private a(LongNum.L2) As Integer
4- Private b(LongNum.L2) As Integer
5- Private s(LongNum.L2) As Integer
6- Private q(LongNum.L2) As Integer
7- Private lnum As New LongNum
3+ Private a As LongNum
4+ Private b As LongNum
5+ Private s As LongNum
6+ Private q As LongNum
7+
88 Sub Main()
99
1010 Try
1111
1212 Calc()
13- Console.WriteLine(lnum.displayString(s))
13+ Console.WriteLine(s.displayString)
1414 Catch ex As Exception
1515 Console.WriteLine("例外発生")
1616 Console.Write(ex.ToString)
@@ -19,25 +19,22 @@
1919 End Sub
2020
2121 Private Sub Calc()
22- For k As Integer = 0 To LongNum.L2
23- s(k) = 0
24- a(k) = 0
25- b(k) = 0
26- q(k) = 0
27- Next
2822
29- a(0) = 16 * 5
30- b(0) = 4 * 239
3123
24+ a = New LongNum(16 * 5)
25+ b = New LongNum(4 * 239)
26+ s = New LongNum(0)
27+ q = New LongNum(0)
28+
3229 For k As Integer = 1 To LongNum.N
33- lnum.ldiv(a, 5 * 5, a)
34- lnum.ldiv(b, 239 * 239, b)
35- lnum.lsub(a, b, q)
36- lnum.ldiv(q, 2 * k - 1, q)
30+ a.ldiv(5 * 5, a)
31+ b.ldiv(239 * 239, b)
32+ a.lsub(b, q)
33+ q.ldiv(2 * k - 1, q)
3734 If k Mod 2 = 0 Then
38- lnum.lsub(s, q, s)
35+ s.lsub(q, s)
3936 Else
40- lnum.ladd(s, q, s)
37+ s.ladd(q, s)
4138 End If
4239 Next
4340