Haskell 中 (^) 的奇怪行为

Ran*_*ude 12 floating-point haskell ghc ghci

为什么 GHCi 在下面给出了错误的答案?

温室气体

?> ((-20.24373193905347)^12)^2 - ((-20.24373193905347)^24)
4.503599627370496e15
Run Code Online (Sandbox Code Playgroud)

蟒蛇3

>>> ((-20.24373193905347)**12)**2 - ((-20.24373193905347)**24)
0.0
Run Code Online (Sandbox Code Playgroud)

更新 我将按如下方式实现 Haskell 的 (^) 函数。

powerXY :: Double -> Int -> Double
powerXY x 0 = 1
powerXY x y
    | y < 0 = powerXY (1/x) (-y)
    | otherwise = 
        let z = powerXY x (y `div` 2)
        in  if odd y then z*z*x else z*z

main = do 
    let x = -20.24373193905347
    print $ powerXY (powerXY x 12) 2 - powerXY x 24 -- 0
    print $ ((x^12)^2) - (x ^ 24) -- 4.503599627370496e15
Run Code Online (Sandbox Code Playgroud)

尽管我的版本看起来并不比@WillemVanOnsem 在下面提供的版本更正确,但奇怪的是,它至少为这种特殊情况提供了正确的答案。

Python 类似。

def pw(x, y):
    if y < 0:
        return pw(1/x, -y)
    if y == 0:
        return 1
    z = pw(x, y//2)
    if y % 2 == 1:
        return z*z*x
    else:
        return z*z

# prints 0.0
print(pw(pw(-20.24373193905347, 12), 2) - pw(-20.24373193905347, 24))
Run Code Online (Sandbox Code Playgroud)

Wil*_*sem 14

简短回答(^) :: (Num a, Integral b) => a -> b -> a和 之间有区别(**) :: Floating a => a -> a -> a

(^)函数仅适用于积分指数。它通常会使用迭代算法,每次都会检查幂是否可以被 2 整除,然后将幂除以 2(如果不可整除,则将结果与 相乘x)。因此,这意味着对于12,它将执行总共六次乘法。如果乘法有一定的舍入误差,该误差可能会“爆炸”。正如我们在源代码中看到的,该(^)函数实现为

(^) :: (Num a, Integral b) => a -> b -> a
x0 ^ y0 | y0 < 0    = errorWithoutStackTrace "Negative exponent"
        | y0 == 0   = 1
        | otherwise = f x0 y0
    where -- f : x0 ^ y0 = x ^ y
          f x y | even y    = f (x * x) (y `quot` 2)
                | y == 1    = x
                | otherwise = g (x * x) (y `quot` 2) x         -- See Note [Half of y - 1]
          -- g : x0 ^ y0 = (x ^ y) * z
          g x y z | even y = g (x * x) (y `quot` 2) z
                  | y == 1 = x * z
                  | otherwise = g (x * x) (y `quot` 2) (x * z) -- See Note [Half of y - 1]
Run Code Online (Sandbox Code Playgroud)

(**)函数至少对于Floats 和Doubles 实现为在浮点单元上工作。事实上,如果我们看一下 的实现(**),我们会看到:

instance Floating Float where
    -- …
    (**) x y = powerFloat x y
    -- …
Run Code Online (Sandbox Code Playgroud)

这因此重定向到 powerFloat# :: Float# -> Float# -> Float#,这会函数,该函数通常会被编译器链接到相应的 FPU 操作。

如果我们(**)改为使用,我们也会为 64 位浮点单元获得零:

Prelude> (a**12)**2 - a**24
0.0
Run Code Online (Sandbox Code Playgroud)

例如,我们可以在 Python 中实现迭代算法:

def pw(x0, y0):
    if y0 < 0:
        raise Error()
    if y0 == 0:
        return 1
    return f(x0, y0)


def f(x, y):
    if (y % 2 == 0):
        return f(x*x, y//2)
    if y == 1:
        return x
    return g(x*x, y // 2, x)


def g(x, y, z):
    if (y % 2 == 0):
        return g(x*x, y//2, z)
    if y == 1:
        return x*z
    return g(x*x, y//2, x*z)
Run Code Online (Sandbox Code Playgroud)

如果我们然后执行相同的操作,我会在本地获得:

>>> pw(pw(-20.24373193905347, 12), 2) - pw(-20.24373193905347, 24)
4503599627370496.0
Run Code Online (Sandbox Code Playgroud)

这与我们(^)在 GHCi 中得到的值相同。

  • @Randomdude:我已经在Python中实现了该算法,并获得了与ghc中相同的值。 (2认同)