英文:
go, for loop and break, infinite loop
问题
以下是代码的翻译:
以下代码没有错误。
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("无法对负数进行平方根操作: %v", float64(e))
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
err := ErrNegativeSqrt(x)
return x, err
}
z := x
var delta = 1e-10
for {
n := z - (z*z - x) / (2*z)
if math.Abs(n - z) < delta {
break
}
z = n
}
return z, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-3))
}
但是,当我在func Sqrt()
中更改了for
循环时,它导致了无限循环。
func Sqrt(x float64) (float64, error) {
if x < 0 {
err := ErrNegativeSqrt(x)
return x, err
}
z := x
var delta = 1e-10
for {
n := z - (z*z - x) / (2*z)
if math.Abs(n - z) < delta {
z = n // 这里......
break // 在这里中断循环
}
}
return z, nil
}
为什么会有不同的结果?
英文:
there is no error in the following code.
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
err := ErrNegativeSqrt(x)
return x, err
}
z := x
var delta = 1e-10
for {
n := z - (z*z - x) / (2*z)
if math.Abs(n - z) < delta {
break
}
z = n
}
return z, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-3))
}
But when I change the for loop in func Sqrt()
, it led to infinite loop?
func Sqrt(x float64) (float64, error) {
if x < 0 {
err := ErrNegativeSqrt(x)
return x, err
}
z := x
var delta = 1e-10
for {
n := z - (z*z - x) / (2*z)
if math.Abs(n - z) < delta {
z = n // here ....
break // break here
}
}
return z, nil
}
Why there are different?
答案1
得分: 4
第二个循环将是无限循环,因为逻辑有误。在这段代码中:
for {
n := z - (z*z - x) / (2*z)
if math.Abs(n - z) < delta {
z = n // 这里 ....
break // 在这里跳出循环
}
}
z
的值从未更新为新计算的值。这导致n := z - (z*z - x) / (2*z)
始终在相同的z
上工作,即等于x
,因为条件math.Abs(n - z) < delta
永远不会为真。
你需要再次给z
赋值以便更新它。你可以通过在循环中记录z
的值来进行检查。示例代码:https://play.golang.org/p/9H7Uze4gip
英文:
The second loop will be infinite since the logic is flawed. In this code:
for {
n := z - (z*z - x) / (2*z)
if math.Abs(n - z) < delta {
z = n // here ....
break // break here
}
}
value of z
never updates to the newly calculated value. This results in n := z - (z*z - x) / (2*z)
always working on the same z
, that is equal to x
, since the condition math.Abs(n - z) < delta
never gets to be true.
You need to assign to z
again so it gets updated. You can check this by logging value of z
in the loop. Example code: https://play.golang.org/p/9H7Uze4gip
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论