我試圖找出爲什麼1e-10
在該代碼使用。我正在通過練習,並陷於Next, change the loop condition to stop once the value has stopped changing (or only changes by a very small delta). See if that's more or fewer iterations
。在沒有搞清楚之後,我搜索並找到了其他人的解決方案。Golang遊鍛鍊; Tibial平方根
在很多解決方案中,我在for
循環內看到1e-10
作爲if
語句的一部分。在Golang中,這是否意味着1e已經升到了(-10)的能力?是不是像Golang自動的?所以基本上通常我會認爲它會寫成1e^(-10)
?
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(2.)
s := float64(0)
for i := 0; i < 10; i++ {
z = z - (z*z - x)/(2 * z)
if math.Abs(z-s) < 1e-10 {
break
}
s = z
}
return z
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(math.Sqrt(2))
}
感謝您提供的任何/所有幫助!
那符號是在許多編程語言相當普遍表達以科學計數法浮點數。它意味着1 x 10 ^( - 10),或者.0000000001。 – 2014-09-13 16:50:00
不要假設。遵循Go編程語言的規則。 – peterSO 2014-09-13 17:05:57
peterSO,我對Golang來說是全新的,所以如果可能的話,請給我打電話。似乎沒有像'php'這樣的資源。 – Kenny 2014-09-13 17:18:52