回答編集履歴
2
修正
test
CHANGED
@@ -100,6 +100,8 @@
|
|
100
100
|
|
101
101
|
for(int count = 1; !isNearlyZero(delta); ++count) {
|
102
102
|
|
103
|
+
// x <- x - f(x) / f'(x)
|
104
|
+
|
103
105
|
x = x.subtract(
|
104
106
|
|
105
107
|
func(x).divide(devFunc(x), RoundingMode.HALF_UP)
|
@@ -134,7 +136,7 @@
|
|
134
136
|
|
135
137
|
catch(ArrayIndexOutOfBoundsException | NumberFormatException e) {
|
136
138
|
|
137
|
-
System.err.println("USAGE:
|
139
|
+
System.err.println("USAGE: java Newton n");
|
138
140
|
|
139
141
|
System.err.println("to compute sqrt(n)");
|
140
142
|
|
1
追記
test
CHANGED
@@ -15,3 +15,133 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
また、定数はfinal指定した方が読みやすいですよ。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
コメントを受けて
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
> しかし、ソースの[x][j]に引数3.0を指定すると、誤差の表示がループします。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
`x = x.subtract((x.multiply(x).subtract(j)).divide((j.multiply(x))));`
|
30
|
+
|
31
|
+
『指数の2』と『定数項の-2』では絶対値は同じでも役割が全く異なります。
|
32
|
+
|
33
|
+
数値が同じだからと言って、双方を同じjにしていることが問題です。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
書いてみた
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
なんとなく無理矢理感の漂うコードではありますが。
|
42
|
+
|
43
|
+
```Java
|
44
|
+
|
45
|
+
import java.math.BigDecimal;
|
46
|
+
|
47
|
+
import java.math.RoundingMode;
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
class Newton {
|
52
|
+
|
53
|
+
//
|
54
|
+
|
55
|
+
static final BigDecimal epsilon = BigDecimal.valueOf(1.E-16);
|
56
|
+
|
57
|
+
static boolean isNearlyZero(BigDecimal num) {
|
58
|
+
|
59
|
+
return epsilon.compareTo(num.abs()) > 0;
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
//
|
66
|
+
|
67
|
+
final BigDecimal n;
|
68
|
+
|
69
|
+
BigDecimal func(BigDecimal x) {
|
70
|
+
|
71
|
+
// x^2 - n
|
72
|
+
|
73
|
+
return x.pow(2).subtract(n);
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
static BigDecimal devFunc(BigDecimal x) {
|
78
|
+
|
79
|
+
// 2x
|
80
|
+
|
81
|
+
return BigDecimal.valueOf(2).multiply(x);
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
//
|
88
|
+
|
89
|
+
Newton(int n) {
|
90
|
+
|
91
|
+
this.n = BigDecimal.valueOf(n);
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
BigDecimal x = BigDecimal.valueOf(2.);
|
96
|
+
|
97
|
+
BigDecimal delta = BigDecimal.valueOf(1.);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
for(int count = 1; !isNearlyZero(delta); ++count) {
|
102
|
+
|
103
|
+
x = x.subtract(
|
104
|
+
|
105
|
+
func(x).divide(devFunc(x), RoundingMode.HALF_UP)
|
106
|
+
|
107
|
+
);
|
108
|
+
|
109
|
+
delta = func(x);
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
System.out.println(count + "回目 誤差=" + delta);
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
System.out.println("x = " + x);
|
120
|
+
|
121
|
+
System.out.println("x * x = " + x.pow(2));
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
public static void main(String[] args) {
|
128
|
+
|
129
|
+
try {
|
130
|
+
|
131
|
+
new Newton(Integer.parseInt(args[0]));
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
catch(ArrayIndexOutOfBoundsException | NumberFormatException e) {
|
136
|
+
|
137
|
+
System.err.println("USAGE: .java n");
|
138
|
+
|
139
|
+
System.err.println("to compute sqrt(n)");
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
```
|