contributed by < loolofen
,shanshow
>
三進位中的其中一種,其特色是將+
與-
、0
作為基本數位的進位。
比較如下:
Truth value | Unsigned | Balanced |
---|---|---|
false | 0 | - |
unknown | 1 | 0 |
true | 2 | + |
我們曾經學過許多二進位的表達正負方式,像是一的補數、二的補數等等,而這些方式都有或多或少的問題。
這兩種補數方式最大的問題在於必須犧牲最前頭的位數來表達正負,而一的補數因此會產生兩個0的表述方式;二的補數則是在正負轉換時需額外+1。
平衡三進位則把正負的表述方式內化成一種進位,不需要額外的符號表述正負就能夠達到效果,因此在加減法與乘法方面會較二進位為高效率。
使用
其中
舉例(以T代替原本的
將
的三個數字分別拆開變成三個三進位帶入公式後的數值後代入公式,進行計算,此例使用到關於小數以及乘除法,將在下面介紹。
平衡三進位來表示十進位的整數時,長度會較十進位的數字長,但比二進位的短,且容易更改正負,不需像二進位使用一或二的補數。
舉個例子:
若要表示十進位的小數,以上述的
為例,只看小數點後面的 ,帶入公式後會得到 ,經由除法後可得到 。
根據 economical radix 爭對 radix 及 width 測量(下圖),在所有數字系統中以
+ | T | 0 | 1 |
---|---|---|---|
T | T1 | T | 0 |
0 | T | 0 | 1 |
1 | 0 | 1 | 1T |
- | T | 0 | 1 |
---|---|---|---|
T | 0 | T | T1 |
0 | 1 | 0 | T |
1 | 1T | 1 | 0 |
× | T | 0 | 1 |
---|---|---|---|
T | 1 | 0 | T |
0 | 1 | 0 | 0 |
1 | T | 0 | 1 |
÷ | T | 0 | 1 |
---|---|---|---|
T | 1 | NaN | T |
0 | 0 | NaN | 0 |
1 | T | NaN | 1 |
注意:減法與除法並沒有符合交換律,可從圖中發現並不對稱。
1TT1TT.1TT1 1TT1TT.1TT1 1TT1TT.1TT1 1TT1TT.1TT1
+ 11T1.T − 11T1.T − 11T1.T -> + TT1T.1
-------------- -------------- ---------------
1T0T10.0TT1 1T1001.TTT1 1T1001.TTT1
+ 1T + T T + T T
-------------- ---------------- ----------------
1T1110.0TT1 1110TT.TTT1 1110TT.TTT1
+ T + T 1 + T 1
-------------- ---------------- ----------------
1T0110.0TT1 1100T.TTT1 1100T.TTT1
1TT1.TT
× T11T.1
-------------
1TT.1TT multiply 1
T11T.11 multiply T
1TT1T.T multiply 1
1TT1TT multiply 1
T11T11 multiply T
-------------
0T0000T.10T
1TT1.TT quotient
0.5 × divisor T01.0 -------------
divisor T11T.1 ) T0000T.10T dividend
T11T1 T000<T010, set 1
-------
1T1T0
1TT1T 1T1T0>10T0, set T
-------
111T
1TT1T 1110>10T0, set T
-------
T00.1
T11T.1 T001<T010, set 1
--------
1T1.00
1TT.1T 1T100>10T0, set T
--------
1T.T1T
1T.T1T 1TT1T>10T0, set T
--------
0
在以前,為了配合電腦的科技,所以大多使用2進位系統,2進位系統優勢在於
不過,目前科技日新月異,硬體上的問題可被克服,平衡三進位的優勢就漸漸浮現了。
根據曾經的黑科技:三進位計算機一文,引用其中一段
隨著技術的進步,真空管和電晶體等傳統的計算機元器件逐漸被淘汰,取而代之的是速度更快、可靠性更好的鐵氧體磁芯和半導體二極體。這些電子元器件組成了一個很好的可控電流變壓器,這為三進位邏輯電路的實現提供了可能,因為電壓存在著三種狀態:正電壓(「1」)、零電壓(「0」)和負電壓(「-1」)。三進位邏輯電路非但比二進位邏輯電路速度更快、可靠性更高,而且需要的設備和電能也更少。這些原因促成了三進位計算機「Сетунь」的誕生。
原文網址:https://kknews.cc/tech/ogl8yeq.html
其實早在蘇聯時期,設備稍有進步時就有人成功研發,只是因官僚體制作祟,無疾而終。
在reddit的討論串中,有人提問,為何使用二進位系統而非最有效率的 base e 系統,底下有一連串討論。
The base e is the most economical choice of radix β > 1 (Hayes 2001), where the radix economy is measured as the product of the radix and the length of the string of symbols needed to express a given range of values.
base e has the lowest radix economy
First, note that the function
is strictly decreasing on 1 < x < e and strictly increasing on x > e. Its minimum, therefore, for x > 1, occurs at e.
Then for a constant N,will have a minimum at e for the same reason y does, meaning e is therefore the base with the lowest average radix economy.
Since 2 / ln(2) ≈ 2.89 and 3 / ln(3) ≈ 2.73, it follows that 3 is the integer base with the lowest average radix economy.
Base b | |||
---|---|---|---|
2 | 9.3 | 22.9 | 1.0615 |
e | 9 | 22.1 | 1.0000 |
3 | 9.5 | 22.2 | 1.0046 |
與
的比值,在N為極大數值下,為base 3 的數值最接近最小值。
首先,從介紹IOTA的影片中,我們得知IOTA與比特幣等等所使用的架構有所不同。
IOTA使用的是tangle架構,而比特幣使用的是blockchain架構。
以下段落摘錄自IOTA白皮書:
在一般情況下,IOTA 按如下方式運行。如前所述,不存在全域的區塊鏈,取而代之的是一個 DAG(有向無環圖),我們稱之為 Tangle。
通過節點發出的所有交易構成了這個 Tangle (存所有交易的帳本) 的集合。
這個圖的邊是這樣形成的:當一個新的交易產生,它必須驗證之前的兩個交易,這些驗證關係就通過有向的邊來表示,如圖 1 所示(在圖中,時間走向總是從左到右)。
如果從交易 A 到交易 B 之間沒有直接連接的有向邊,而有至少兩個有向邊的路徑存在,我們就說交易 A 間接地驗證了交易 B。另外,有一個創世交易 (genesis trasaction),被所有交易直接或間接驗證,如圖 2。
創世交易有以下描述:在最一開始有一個地址 (address) 擁有全部的 token,接著創世交易會把金錢轉給其他 founder 的地址。我們強調所有的 token 皆是創世交易所產生的(不會再產生新的 token),而且不會再有「挖礦」就可以收到金錢獎勵的概念了。
圖一
權重的(重新)計算
圖二
關於交易的積分計算(帶圓圈的數字)
不過這樣子看來好像無法馬上理解使用平衡三進位的必要?所以我去找了另一個討論,他們的討論為為何IOTA要採用Balanced ternary ,由IOTA 基金會的創辦人 David Sønstebø 回答:
Why ternary? As 'PuddingwithRum' has already provided a link to, ternary is the optimal radix, actually Base E (2.71…) is, but you can't make processors like that. So it comes down to Base Binary (2) vs Base Ternary (3). 3 is closer to the universal optimum 2.71 than is 2. That is the absolute most simple elevator pitch for ternary.
The benefits of ternary go beyond mere computational performance in a parochial 1:1 comparison versus binary. Another area where ternary shines is Artificial Neural Networks, Artifical Neurons and Artificial Intelligence Logic. In fact this is actually how our brain also computes Other areas where ternary shines is in graphical processing, cryptography and search, among other things.
此外,引用自IOTACHINA,優勢的探討:
IOTA的量子安全是怎么来的?
IOTA使用哈希签名而不是椭圆曲线密码学(ECC)。哈希签名不仅仅在速度上胜过ECC,还能大大简化整个协议(签名和验证)。IOTA能够实现量子安全是因为我们采用了文格尼茨签名。IOTA的三进制哈希函数称为Curl(编程语言)。
Term | Definition | Reference |
---|---|---|
Tangle | A directed acyclic graph (DAG) as a distributed ledger which stores all transaction data of the IOTA network. It is a Blockchain without the blocks and the chain (so is it really a Blockchain?). The Tangle is the first distributed ledger to achieve scalability, no fee transactions, data integrity and transmission as well as quantum-computing protection. Contrary to today’s Blockchains, consensus is no-longer decoupled but instead an intrinsic part of the system, leading to a completely decentralized and self-regulating peer-to-peer network. | iota INSTRODUCTION |
Tips | transactions which have no other transactions referencing them. | iota INSTRODUCTION |
Curl | The main hash function used in IOTA, Curl is based on well-studied sponge construction invented by the Keccak creators (SHA-3), and is specifically tailored for IoT, that also happens to be the world’s first trinary hash function. | iota INSTRODUCTION |
Kerl | as Curl is a young hash function, IOTA currently uses Keccak (SHA-3) for signing. Kerl is Keccek-384, with additional conversion of its input and output from/to 243 trits to 48 bytes using basic two’s complement. | iota INSTRODUCTION |
Winternitz one-time signature (W-OTS) | a Post Quantum Signature scheme, that is used to authorize spending from an address in IOTA (sign with your private key). Due to it's one-time nature, the security of funds in an address decreases rapidly if you sign multiple transactions using the same key. Therefore, in IOTA you never re-use an address that has been spent from (as one signature has already been shared with the network). | iota INSTRODUCTION |
IOTA the token | Total supply - There are 2,779,530,283,277,761 IOTAs(280萬GI)、Units - IOTA notation follow SI units. Pi, Ti, Gi, Mi, Ki, i - more info | iota INSTRODUCTION |
IRI | 全稱 IOTA Reference Implementation,以 Java 撰寫,為目前唯一可完整運行 Protocol 的 IOTA Node,基本上架設節點的話會使用 IRI 來用。作為 Reference Implementation,其餘的 Implementation 應該遵照 IRI 的行為來實做。 | iota INSTRODUCTION |
iota.lib.* | The IOTA library, it has all the necessary functionality to fully use IOTA, including sending transactions, cryptography-related functions and the core API. iota.lib.js is the most established and used library currently. | iota INSTRODUCTION |
首先你要记住IOTA的六个要点:
1) IOTA是为IoT而设计的区块链技术(分布式账本技术)。
2) 在感应器之间的去中心化数据交易/传输和代币支付。
3) 小微支付以及零数据交易和支付费。
4) 有助于把所有物件都归纳入分享经济之中。
5) 可扩容的分布式账本。
6) 加密信息传递和隐秘代币支付。
參考 st9007a共筆 來源 : iota library
// All possible tryte values
var trytesAlphabet = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// map of all trits representations
var trytesTrits = [
[ 0, 0, 0],
[ 1, 0, 0],
[-1, 1, 0],
[ 0, 1, 0],
[ 1, 1, 0],
[-1, -1, 1],
[ 0, -1, 1],
[ 1, -1, 1],
[-1, 0, 1],
[ 0, 0, 1],
[ 1, 0, 1],
[-1, 1, 1],
[ 0, 1, 1],
[ 1, 1, 1],
[-1, -1, -1],
[ 0, -1, -1],
[ 1, -1, -1],
[-1, 0, -1],
[ 0, 0, -1],
[ 1, 0, -1],
[-1, 1, -1],
[ 0, 1, -1],
[ 1, 1, -1],
[-1, -1, 0],
[ 0, -1, 0],
[ 1, -1, 0],
[-1, 0, 0]
];
/**
* Converts trytes into trits
*
* @method trits
* @param {String|Int} input Tryte value to be converted. Can either be string or int
* @param {Array} state (optional) state to be modified
* @returns {Array} trits
**/
var trits = function( input, state ) {
var trits = state || [];
if (Number.isInteger(input)) {
var absoluteValue = input < 0 ? -input : input;
while (absoluteValue > 0) {
var remainder = absoluteValue % 3;
absoluteValue = Math.floor(absoluteValue / 3);
if (remainder > 1) {
remainder = -1;
absoluteValue++;
}
trits[trits.length] = remainder;
}
if (input < 0) {
for (var i = 0; i < trits.length; i++) {
trits[i] = -trits[i];
}
}
} else {
for (var i = 0; i < input.length; i++) {
var index = trytesAlphabet.indexOf(input.charAt(i));
trits[i * 3] = trytesTrits[index][0];
trits[i * 3 + 1] = trytesTrits[index][1];
trits[i * 3 + 2] = trytesTrits[index][2];
}
}
return trits;
}
/* Pictures of zero, largest positive and largest negative numbers. */
static const char *pat = "┌───┐\n"
"│ │\n"
"└───┘\n"
"├─┴─┤\n"
"┤ ├\n"
"├─┬─┤\n"
"┌┬┬┬┐\n"
"├ ┤\n"
"└┴┴┴┘\n";
#define SZD 8
#define SZP 42
for (r = 0; r < SZD; ++r) {
p = &digit[r];
p->exp = r ? (3 * digit[r-1].exp) : 1;
p->val = 0;
}
for (r = 0; r < SZD; ++r) {
p = &digit[r];
printf("第 %d 次\n",r+1);
printf("expBefore:%d r:%d \n",p->exp,r);
printf("(digit)expBefore:%d r:%d \n",digit[r].exp,r);
p->exp = r ? (3 * digit[r-1].exp) : 1;
printf("expAfter: %d r:%d \n",p->exp,r);
printf("(digit)expAfter: %d r:%d \n\n",digit[r].exp,r);
p->val = 0;
}
產出結果如下:
第 1 次
expBefore:0 r:0
(digit)expBefore:0 r:0
expAfter: 1 r:0
(digit)expAfter: 1 r:0
第 2 次
expBefore:0 r:1
(digit)expBefore:0 r:1
expAfter: 3 r:1
(digit)expAfter: 3 r:1
第 3 次
expBefore:0 r:2
(digit)expBefore:0 r:2
expAfter: 9 r:2
(digit)expAfter: 9 r:2
第 4 次
expBefore:0 r:3
(digit)expBefore:0 r:3
expAfter: 27 r:3
(digit)expAfter: 27 r:3
第 5 次
expBefore:0 r:4
(digit)expBefore:0 r:4
expAfter: 81 r:4
(digit)expAfter: 81 r:4
第 6 次
expBefore:0 r:5
(digit)expBefore:0 r:5
expAfter: 243 r:5
(digit)expAfter: 243 r:5
第 7 次
expBefore:0 r:6
(digit)expBefore:0 r:6
expAfter: 729 r:6
(digit)expAfter: 729 r:6
第 8 次
expBefore:0 r:7
(digit)expBefore:0 r:7
expAfter: 2187 r:7
(digit)expAfter: 2187 r:7
/* Truncate to the largest allowed positive. */
p = &digit[SZD - 1];//SZD = 8, then digit[8 - 1] = 2187
r = 3 * p->exp / 2;
if (src > r)
src = r;
else
r = src;
/* Like 'echo "obase=3; $src" | bc'. */
do {
while (r < p->exp)
--p;
r -= p->exp;
++p->val;
} while (r);
/* Like 'echo "obase=3; $src" | bc'. */
//Test
do {
while (r < p->exp)
{
printf("pTest1: %d \n",p->exp);
--p;
printf("pTest2: %d \n",p->exp);
}
r -= p->exp;
++p->val;
} while (r);
pTest1: 2187
pTest2: 729
pTest1: 729
pTest2: 243
pTest1: 243
pTest2: 81
pTest1: 81
pTest2: 27
pTest1: 27
pTest2: 9
pTest1: 9
pTest2: 3
pTest1: 3
pTest2: 1
pTest1: 1
pTest2: 0
int x[20];
int *xPtr;
xPtr = &x[0]; // 也可以用 xPtr = x; 假設 xPtr 內的資料為 A000
xPtr++; // 也可以用 xPtr = xPtr + 1; 此運算完成後 xPtr 內之資料為 A002
// 剛好為變數 x[1] 之位址
xPtr = xPtr + 10; //此運算完成後 xPtr 內之資料為 A016,剛好為
// 變數 x[11] 之位址
/* Convert to the balanced form. */
while (p < &digit[SZD]) {
if (r)
++p->val;
r = p->val > 1;
if (r)
p->val -= 3;
p->val *= inv;
++p;
}
PyOTA: The IOTA Python API Library
參考資料:
wiki:平衡三進位
wiki:Balanced Ternary
百度:平衡三進位
IOTA問答論壇
知識+:關於二進位
二進位的優勢討論
三進位計算機
reddit:askscience討論:為何不用最有效率的 base e 系統
Non-integer_representation wiki
IOTA/TANGLE白皮書
IOTA BREAKDOWN: The Tangle Vs. Blockchain Explained
IOTA介紹
Tangle白皮書
How exactly is IOTA going to be infinitely scalable?
IOTACHINA