回答編集履歴

1

ソースの追加(BigDecimal)

2018/03/27 10:19

投稿

退会済みユーザー
test CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
 
42
42
 
43
- public class Midi {
43
+ public class Main {
44
44
 
45
45
 
46
46
 
@@ -89,3 +89,81 @@
89
89
  }
90
90
 
91
91
  ```
92
+
93
+ <追記>
94
+
95
+ BigDecimalを使ったときのソースです。
96
+
97
+
98
+
99
+ ```Java
100
+
101
+
102
+
103
+
104
+
105
+ import java.util.Scanner;
106
+
107
+ import java.math.BigDecimal;
108
+
109
+
110
+
111
+ public class Main {
112
+
113
+
114
+
115
+ public static void main(String args[]) {
116
+
117
+
118
+
119
+ Scanner sc = new Scanner(System.in);
120
+
121
+ String line = sc.nextLine();
122
+
123
+
124
+
125
+ double number = Double.parseDouble(line);
126
+
127
+ BigDecimal a = new BigDecimal(number);
128
+
129
+ BigDecimal x = new BigDecimal("2.0");
130
+
131
+ BigDecimal delta = new BigDecimal("1.0");
132
+
133
+ BigDecimal sisu = new BigDecimal(1.0E-15);
134
+
135
+ int count = 0;
136
+
137
+ while (sisu.compareTo(delta) == -1) {
138
+
139
+
140
+
141
+ BigDecimal f = (x.multiply(x.multiply(x))).subtract(a);
142
+
143
+ BigDecimal df = x.multiply(x).multiply(new BigDecimal("3.0"));
144
+
145
+ x = x.subtract(f.divide((df),BigDecimal.ROUND_HALF_UP));
146
+
147
+
148
+
149
+ delta = ((x.multiply(x.multiply(x))).subtract(a)).abs();
150
+
151
+
152
+
153
+ count++;
154
+
155
+ System.out.println(count + "回目 誤差=" + delta);
156
+
157
+ }
158
+
159
+ System.out.println("x = " + x);
160
+
161
+ System.out.println("x * x * x = " + x.multiply(x.multiply(x)));
162
+
163
+ }
164
+
165
+
166
+
167
+ }
168
+
169
+ ```