GoingMyWay
GoingMyWay

Reputation: 17468

go, for loop and break, infinite loop

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?

Upvotes: 0

Views: 2027

Answers (1)

abhink
abhink

Reputation: 9116

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

Upvotes: 4

Related Questions