为什么朱莉娅2 ^ 3 ^ 4 = 0?

时间:2015-06-10 08:39:57

标签: julia

我刚读过Quora的帖子: http://www.quora.com/Is-Julia-ready-for-production-use

在底部,有一个答案说:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>States</title>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
    <script>
    $(document).ready(function () {

        var states = new Bloodhound({
          datumTokenizer: Bloodhound.tokenizers.whitespace,
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          // `states` is an array of state names defined in "The Basics"
          //local: states
            remote: {
                url: '../search.php?q=%QUERY',
                //url: 'https://twitter.github.io/typeahead.js/data/films/post_1960.json',
                wildcard: '%QUERY'
              }
        });

        $('#the-basics .typeahead').typeahead({
            hint: true,
            highlight: true,
            minLength: 1
        }, {
            name: 'states',
            //display: 'value',
            source: states
        });
    });
</script>
</head>
<body>
    <div id="the-basics">
        <input class="typeahead" type="text" placeholder="States of USA">
    </div>

</body>

我自己尝试过:

2 ^ 3 ^ 4 = 0

我个人认为这不是语言中的错误。我们可以为朱莉娅和我们的人类添加括号以便清晰:

julia> 2 ^ 3 ^ 4
0

到目前为止一切顺利;但是,这不起作用:

julia> (2 ^ 3) ^ 4
4096

因为我正在学习,我想知道,朱莉娅如何评估这个表达式为0?什么是评估先例?

julia> 2 ^ (3 ^ 4)
0

1 个答案:

答案 0 :(得分:4)

我很惊讶我在SO上找不到关于此的重复问题。我想我会回答这个问题的方式与the FAQ in the manual略有不同,因为这是一个常见的第一个问题。糟糕,我不知道错过了:Factorial function works in Python, returns 0 for Julia

想象一下,你已经被教过加法和乘法,但从未学过任何高于99的数字。就你而言,数字大于那个根本就不存在。所以你学会了将它们带入数十列,但是你甚至不知道你所谓的列数是什么。所以你放弃它们。只要你的数字永远不会超过99,一切都会好的。一旦超过99,你就会回到0.所以99 +3≡2(mod 100)。并且52 *9≡68(mod 100)。每当你用10个以上因子进行乘法运算时,你的答案就是零:25 *32≡0(mod 100)。现在,在你做每一次计算之后,有人可能会问你“你过了99?”但这需要时间来回答......计算下一个数学问题所花费的时间!

这实际上是计算机本身如何进行算术运算,除非它们以64位的二进制形式进行。您可以使用bits函数查看各个位:

julia> bits(45)
"0000000000000000000000000000000000000000000000000000000000101101"

当我们将它乘以2时,101101将向左移动(就像十进制乘以10一样):

julia> bits(45 * 2)
"0000000000000000000000000000000000000000000000000000000001011010"
julia> bits(45 * 2 * 2)
"0000000000000000000000000000000000000000000000000000000010110100"
julia> bits(45 * 2^58)
"1011010000000000000000000000000000000000000000000000000000000000"
julia> bits(45 * 2^60)
"1101000000000000000000000000000000000000000000000000000000000000"

......直到它开始掉下来。如果将两个以上的二十二个相乘,则答案将始终为零(就像在上面的示例中一起乘以两个以上)。我们可以询问计算机是否溢出,但默认情况下每次计算都会some serious performance implications。所以在朱莉娅,你必须明确。您可以要求Julia在特定乘法后进行检查:

julia> Base.checked_mul(45, 2^60) # or checked_add for addition
ERROR: OverflowError()
 in checked_mul at int.jl:514

或者您可以将其中一个参数提升为BigInt:

julia> bin(big(45) * 2^60)
"101101000000000000000000000000000000000000000000000000000000000000"

在您的示例中,当您使用大整数算术时,您可以看到答案为1后跟81个零:

julia> bin(big(2) ^ 3 ^ 4)
"1000000000000000000000000000000000000000000000000000000000000000000000000000000000"

有关详细信息,请参阅常见问题解答:why does julia use native machine integer arithmetic?