64 位银行余额 "对任何人来说都足够了"?
“640K ought to be enough for anybody.”
Supposedly Bill Gates, circa 1981.
“640K应该足够任何人使用。”
据说是比尔·盖茨,大约在1981年。
Recently at TigerBeetle, we’ve decided to use 128-bit integers to store all financial amounts and balances, retiring our previous use of 64-bit integers. While some may argue that a 64-bit integer, which can store integers ranging from zero to 264, is enough to count the grains of sand on Earth, we realized we need to go beyond this limit if we want to be able to store all kinds of transactions adequately. Let’s find out why.
最近在TigerBeetle,我们决定使用128位整数来存储所有的财务金额和余额,退役我们以前使用的64位整数。虽然有人可能会认为64位整数足以计算地球上的沙粒数量,但我们意识到如果我们想要能够充分存储各种交易,我们需要超越这个限制。让我们找出原因。
How do we represent money?
我们如何表示货币?
To represent numbers (and to be able to do math with them), computers need to encode this number in a binary system which, depending on the range and the kind of number, requires a certain amount of bits (each bit can be either 0 or 1). For example, integers (whole numbers) ranging from -128 to 127 can be represented with only 8 bits, but if we don’t need negative numbers, we can use the same bits to represent any integer from 0 to 255, and that’s a byte! Larger numbers require more bits, for example, 16-bit, 32-bit, and 64-bit numbers are the most common.
为了表示数字(并能够进行数学运算),计算机需要在二进制系统中对该数字进行编码,这取决于范围和数字的类型,需要一定数量的位(每个位可以是0或1)。例如,范围从-128到127的整数(整数)只需要8位即可表示,但如果我们不需要负数,我们可以使用相同的位来表示从0到255的任何整数,这就是一个字节!更大的数字需要更多的位,例如,16位,32位和64位数字是最常见的。
You may have noticed that we are talking about money as whole numbers and not as decimal numbers or cents. Things get more complicated with fractional numbers, which can be encoded using floating point numbers. While binary floating point may be fine for other calculations, they cannot accurately express decimal numbers. This is the same kind of problem that we humans have when we try to represent ⅓ in decimal as 0.33333…, computers have to represent ¹⁄₁₀ in binary!
您可能已经注意到,我们谈论的是整数而不是小数或美分。使用分数会使事情变得更加复杂,可以使用浮点数来编码分数。虽然二进制浮点数可能对其他计算来说还可以,但它们无法准确地表示十进制数。这与我们人类在尝试将 ⅓ 表示为小数时遇到的问题相同,即 0.33333…,计算机...