Introducing Strange pow in golang

I just stumbled upon a Pow implementation in golang :

func Pow(x, y float64) float64 {
    // ...
    case x == 0:
        switch {
        case y < 0:
            if isOddInt(y) {
                return Copysign(Inf(1), x)
            }
            return Inf(1)
        case y > 0:
            if isOddInt(y) {
                return x
            }
            return 0
        }
    //...
}

Is the part difficult case y > 0? I would just return 0. Or am I missing something?

+4
source share
2 answers

There are two types of zeros: +0and -0. return value Pow(-0,1)must -0not be+0

to create -0in golang use math.Copysign.

x := math.Copysign(0, -1)
if x == 0 {
    fmt.Println("x is zero")
}
fmt.Println("x ** 3 is", math.Pow(x, 3))

output of the above code

x is zero
x ** 3 is -0

You can check it in Go to the site

why should we distinguish between +0and -0, see https://softwareengineering.stackexchange.com/questions/280648/why-is-negative-zero-important

+4
source

, y > 0, :

Pow(±0, y) = ±0 for y an odd integer > 0
Pow(±0, y) = +0 for finite y > 0 and not an odd integer 

0 (+0), , , x=0

+1

Source: https://habr.com/ru/post/1667695/


All Articles