2017-03-31 5 views
1

私は数の平方根を無差別に計算するプログラムを書いています。しかし、特定の数字(毎回同じ)。ここでPython Skips Numbers

はコードです:

toCalc = 3 

guess = toCalc 
toCalc = round(toCalc, 2) 
while 1+1==2: 

     print "Trying ", guess, "..." 
     calc = guess * guess 
     calc = round(calc, 2) 
     print calc 
     guess = guess - 0.01 
     if calc == toCalc: 
       break 

そして、ここでは、出力されます。

Trying 1.22 ... 
1.49 
Trying 1.21 ... 
1.46 
Trying 1.2 ... 
1.44 
Trying 1.19 ... 
1.42 
Trying 1.18 ... 
1.39 
Trying 1.17 ... 
1.37 
Trying 1.16 ... 
1.35 
Trying 1.15 ... 
1.32 
Trying 1.14 ... 
1.3 
Trying 1.13 ... 
1.28 
Trying 1.12 ... 
1.25 
Trying 1.11 ... 
1.23 
Trying 1.1 ... 
1.21 
Trying 1.09 ... 
1.19 
Trying 1.08 ... 
1.17 
Trying 1.07 ... 
1.14 
Trying 1.06 ... 
1.12 
Trying 1.05 ... 
1.1 
Trying 1.04 ... 
1.08 
Trying 1.03 ... 
1.06 
Trying 1.02 ... 
1.04 
Trying 1.01 ... 
1.02 
Trying 1.0 ... 
1.0 
Trying 0.99 ... 
0.98 
Trying 0.98 ... 
0.96 
Trying 0.97 ... 
0.94 
Trying 0.96 ... 
0.92 
Trying 0.95 ... 
0.9 
Trying 0.94 ... 
0.88 
Trying 0.93 ... 
0.86 
Trying 0.92 ... 
0.85 
Trying 0.91 ... 
0.83 

数の推測である "しよう" の後CALCと番号である "しよう" の下に。

+0

なぜmath.sqrtを使用しないのですか –

+3

あなたの質問は何ですか? –

+0

あなたのアルゴリズムは動作しません。より良いものを探してみてください。 – Daniel

答えて

0
>>> round(1.73 * 1.73, 2) 
2.99 
>>> round(1.732 * 1.732, 2) 
3.0 
>>> round(1.74 * 1.74, 2) 
3.03 

9 * 9 = 81,10 * 10 = 100であり、81から100までの数字はスキップされているとは限りません。

あなたは1.73と1.74で試していますが、どちらも2乗で正確な値3を生成できません。

この動作は「数値をスキップしません」ですが、精度に関するものです。

浮動小数点数は扱いが簡単ではありません。この特定の問題では、0.001の差を使って問題を解決しましたが、他のすべての数値ではうまくいかない場合があります。

ただし、以下は問題を解決するコードです。

toCalc = 3 

guess = toCalc 
toCalc = round(toCalc, 2) 


while 1+1==2: 

    print "Trying ", guess, "..." 
    calc = guess * guess 
    calc = round(calc, 2) 
    print calc 
    guess = guess - 0.001 
    if calc == toCalc: 
      break 
1

old guess - 0.01とそれを平方とすると、next squareは約old square - 0.02(二項式を使用)です。これは、推測の二乗列のステップが約0.02であることを意味し、したがって数字は欠落しています。

これは意味ですか?

より良いアルゴリズムは、二分法(google it)を使用することです。

1

これよりも短い方法があります。

to_calc = 3 
guess = to_calc 
epsilon = 1e-13 

while abs(guess**2 > to_calc) > epsilon: 
    guess = 0.5*(guess + to_calc/guess) 

print(guess) 
# 1.73205080757 
print(guess**2) 
# 3.0 
0

私はあなたが求めているものの完全にはわからないが、以下の機能が網羅近似解を使用しています。それだけ、sqrt(3)の11の正しい小数の計算にNewton's iterationのおかげで5つのステップが必要です。

  • は、あなたがより多くを与えるだろうイプシロンの
  • def squareRootExhaustive(x, epsilon): 
    """Assumes x and epsilon are positive floats & epsilon < 1.""" 
    
        # x = number for which to calculate the square root 
        # epsilon = how close you want to get to the answer 
    
        increment = epsilon**2 # Number of steps it will take to find a good enough approximation 
    
        guess = 0.0 # start the answer at 0 
    
        # while the difference between guess^2 and the num 'x', for which 
        # we are trying to find the square root of, is greater or equals epsilon 
        # and guess * guess is less or equals x, increment the answer 
        while abs(guess**2 - x) >= epsilon and guess*guess <= x: 
         # print(ans)to check the number of exhaustive guesses it generates 
         # before returning the most accurate answer 
         guess += increment 
         if guess*guess > x: 
          raise ValueError 
        return "{} is the sqaure root of {}".format(round(guess, 2), x) 
    
    squareRootExhaustive(3, 0.01) 
    

    低い値が近い十分にあるかどうかを確認するための推測に

  • チェックを生成するために、小さなステップを取る網羅的な列挙と

    • 開始:それは、次の処理を行い正確な回答は得られますが、プログラムは遅くなります。値を大きくするほど応答は速くなりますが、精度は低くなります。

      このアルゴリズムには複雑な問題があります。そのため、二分法アルゴリズムが優れている理由です。