質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1468閲覧

二分法で三乗根の近似値を求めるコード

hagi

総合スコア4

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/05/21 11:31

編集2022/05/21 12:02

x<0の場合も含めた、任意の実数値xに対して、三乗根の近似値を求めるコードを完成させるという問題なのですが、自分の書いたプログラムだと負の数の三乗根が求められません。
恐らく、探索区間が違うと思うのですが、どこが間違っているのでしょうか。

def cube_root_bisection_full_range(cube): pass epsilon = 0.000000001 count_guesses = 0 #探索区間の上限と下限(初期値 (0、cube)) low =0.0 if cube >= 1: high = cube else: high = 1 low < 0.0 if cube >= 1: high = cube else: high < 1 guess = (low + high)/2 while abs(guess**3 - cube) >= epsilon : if guess**3 < cube: low = guess else: high = guess guess = (low + high)/2 count_guesses += 1 if count_guesses >1e6: print(f'FAIL: cannot reach the cube root {cube}') break  print(' count_guess =', count_guesses )   if abs(guess**3 - cube) < epsilon:  print(guess, 'is close to the cube root of ',cube)  print(f'verify: {guess}**3 =',guess**3 )   print('') cube_root_bisection_full_range(0.1) cube_root_bisection_full_range(-0.5) cube_root_bisection_full_range(-2) cube_root_bisection_full_range(5)

【結果】
count_guess = 28
0.464158883318305 is close to the cube root of 0.1
verify: 0.464158883318305**3 = 0.09999999997222532

FAIL: cannot reach the cube root -0.5
count_guess = 1000001

FAIL: cannot reach the cube root -2
count_guess = 1000001

count_guess = 32
1.7099759465781972 is close to the cube root of 5
verify: 1.7099759465781972**3 = 4.999999999135954

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2022/05/21 11:37

ループが無いようですが。
Zuishin

2022/05/21 11:45

Python ってこんないい加減なインデントで動きましたっけ?
TakaiY

2022/05/21 11:48 編集

すでに指摘されているように、必要な部分が無いし、また、関数名がおかしかったり、インデントがおかしかったり、全角文字が混っていたり、処理内容も的と、これは実際に動いているソースではないようなので、これを見て何かアドバイスしても無駄になりそうです。 実際に動かしているソースを提示いただけますか。
guest

回答1

0

ベストアンサー

python

1def cube_root_bisection_full_range(cube): 2 epsilon = 1e-9 3 count_guesses = 0 4 5 if abs(cube) < epsilon: 6 print('0.0 is close to the cube root of ', cube) 7 return 8 9 #探索区間の上限と下限(初期値 (0、cube)) 10 if cube >= 1.0: 11 low = 1.0 12 high = cube 13 elif 0.0 <= cube < 1.0: 14 low = cube 15 high = 1.0 16 elif -1.0 < cube < 0.0: 17 low = -1.0 18 high = cube 19 else: 20 low = cube 21 high = -1.0 22 23 guess = (low + high)/2 24 while abs(guess**3 - cube) >= epsilon : 25 if guess**3 < cube: 26 low = guess 27 else: 28 high = guess 29 30 guess = (low + high)/2 31 count_guesses += 1 32 33 if count_guesses > 1e6: 34 print(f'FAIL: cannot reach the cube root {cube}') 35 break 36 37 print(' count_guess =', count_guesses ) 38 if abs(guess**3 - cube) < epsilon: 39 print(guess, 'is close to the cube root of ', cube) 40 print(f'verify: {guess}**3 =', guess**3 ) 41 print('') 42 43if __name__ == '__main__': 44 cube_root_bisection_full_range(0.1) 45 cube_root_bisection_full_range(-0.5) 46 47 cube_root_bisection_full_range(-2) 48 cube_root_bisection_full_range(5) 49 cube_root_bisection_full_range(0.0000000001)

python

1 count_guess = 26 20.4641588844358921 is close to the cube root of 0.1 3verify: 0.4641588844358921**3 = 0.10000000069455584 4 5 count_guess = 27 6-0.7937005255371332 is close to the cube root of -0.5 7verify: -0.7937005255371332**3 = -0.4999999991552862 8 9 count_guess = 28 10-1.2599210496991873 is close to the cube root of -2 11verify: -1.2599210496991873**3 = -1.999999999068104 12 13 count_guess = 29 141.7099759466946125 is close to the cube root of 5 15verify: 1.7099759466946125**3 = 5.000000000157156 16 170.0 is close to the cube root of 1e-10

投稿2022/05/21 12:42

編集2022/05/21 13:36
melian

総合スコア19771

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hagi

2022/05/21 15:04

elifを使えばよかったんですね! ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問