質問編集履歴
2
これで大丈夫でしょうか?このプログラムで実行はできています...授業のスライドの一部を編集する課題なので、探索区間を探している部分以外は私が作ったわけではない為、まだ理解が追い付いていない段階です。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,29 +1,28 @@
|
|
1
1
|
x<0の場合も含めた、任意の実数値xに対して、三乗根の近似値を求めるコードを完成させるという問題なのですが、自分の書いたプログラムだと負の数の三乗根が求められません。
|
2
2
|
恐らく、探索区間が違うと思うのですが、どこが間違っているのでしょうか。
|
3
3
|
```ここに言語を入力
|
4
|
-
def cube_
|
4
|
+
def cube_root_bisection_full_range(cube):
|
5
|
-
|
5
|
+
pass
|
6
6
|
|
7
|
-
|
7
|
+
epsilon = 0.000000001
|
8
|
-
|
8
|
+
count_guesses = 0
|
9
9
|
|
10
10
|
#探索区間の上限と下限(初期値 (0、cube))
|
11
11
|
|
12
12
|
low =0.0
|
13
|
-
if cube
|
13
|
+
if cube >= 1:
|
14
14
|
high = cube
|
15
15
|
else:
|
16
16
|
high = 1
|
17
17
|
|
18
|
-
low
|
18
|
+
low < 0.0
|
19
|
-
if cube
|
19
|
+
if cube >= 1:
|
20
20
|
high = cube
|
21
21
|
else:
|
22
|
-
high
|
22
|
+
high < 1
|
23
23
|
|
24
24
|
guess = (low + high)/2
|
25
|
-
|
26
|
-
while abs(guess**3 - cube) >= epsilon :
|
25
|
+
while abs(guess**3 - cube) >= epsilon :
|
27
26
|
|
28
27
|
if guess**3 < cube:
|
29
28
|
low = guess
|
@@ -38,10 +37,32 @@
|
|
38
37
|
print(f'FAIL: cannot reach the cube root {cube}')
|
39
38
|
break
|
40
39
|
|
41
|
-
print(' count_guess =', count_guesses )
|
40
|
+
print(' count_guess =', count_guesses )
|
42
|
-
if abs(guess**3 - cube) < epsilon:
|
41
|
+
if abs(guess**3 - cube) < epsilon:
|
43
|
-
print(guess, 'is close to the cube root of ',cube)
|
42
|
+
print(guess, 'is close to the cube root of ',cube)
|
44
|
-
print(f'verify: {guess}**3 =',guess**3 )
|
43
|
+
print(f'verify: {guess}**3 =',guess**3 )
|
45
|
-
print('')
|
44
|
+
print('')
|
45
|
+
|
46
|
+
cube_root_bisection_full_range(0.1)
|
47
|
+
cube_root_bisection_full_range(-0.5)
|
48
|
+
|
49
|
+
cube_root_bisection_full_range(-2)
|
50
|
+
cube_root_bisection_full_range(5)
|
51
|
+
|
46
52
|
|
47
53
|
```
|
54
|
+
【結果】
|
55
|
+
count_guess = 28
|
56
|
+
0.464158883318305 is close to the cube root of 0.1
|
57
|
+
verify: 0.464158883318305**3 = 0.09999999997222532
|
58
|
+
|
59
|
+
FAIL: cannot reach the cube root -0.5
|
60
|
+
count_guess = 1000001
|
61
|
+
|
62
|
+
FAIL: cannot reach the cube root -2
|
63
|
+
count_guess = 1000001
|
64
|
+
|
65
|
+
count_guess = 32
|
66
|
+
1.7099759465781972 is close to the cube root of 5
|
67
|
+
verify: 1.7099759465781972**3 = 4.999999999135954
|
68
|
+
|
1
プログラムの書き忘れを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,4 +20,28 @@
|
|
20
20
|
high = cube
|
21
21
|
else:
|
22
22
|
high < 1
|
23
|
+
|
24
|
+
guess = (low + high)/2
|
25
|
+
|
26
|
+
while abs(guess**3 - cube) >= epsilon :
|
27
|
+
|
28
|
+
if guess**3 < cube:
|
29
|
+
low = guess
|
30
|
+
else:
|
31
|
+
high = guess
|
32
|
+
|
33
|
+
guess = (low + high)/2
|
34
|
+
count_guesses += 1
|
35
|
+
|
36
|
+
|
37
|
+
if count_guesses >1e6:
|
38
|
+
print(f'FAIL: cannot reach the cube root {cube}')
|
39
|
+
break
|
40
|
+
|
41
|
+
print(' count_guess =', count_guesses )
|
42
|
+
if abs(guess**3 - cube) < epsilon:
|
43
|
+
print(guess, 'is close to the cube root of ',cube)
|
44
|
+
print(f'verify: {guess}**3 =',guess**3 )
|
45
|
+
print('')
|
46
|
+
|
23
47
|
```
|