<p>The code you're citing does not actually deal with difficulty. It deals with <em>targets</em>. The target is the maximum 256-bit value than the block hash is allowed to have.</p>

<p>The difficulty is defined as <strong>2^208 * 65535 / target</strong>.</p>

<p>As for the book, that looks wrong indeed.</p>

CC BY-SA 3.0
2017-06-06T04:57:19.867Z
Thanks for answer, as you said, the description of the book is wrong?
Ello
CC BY-SA 3.0
2017-06-06T04:57:32.493Z
The book is wrong.
Pieter Wuille
CC BY-SA 3.0
2017-06-06T04:50:12.387Z
2017-06-06T04:57:47.693Z
2017-06-06T04:57:47.693Z
Pieter Wuille
<p>When I read this book <a href="https://github.com/bitcoinbook/bitcoinbook" rel="nofollow noreferrer">Master Bitcoin</a>, I have a problem in understanding the "Difficulty" in Chapter 8.  </p>

<p>There is a equation of difficulty:</p>

<p><code>New Difficulty = Old Difficulty * (Actual Time of Last 2016 Blocks / 20160 minutes)</code></p>

<p>I feel confused.<br>
According to the formula, if <strong>Actual Time</strong> was longer than <strong>20160 minutes</strong>, the <strong>New Difficulty</strong> would be bigger than <strong>Old Difficulty</strong>, and then <strong>Actual Time</strong> would be longer and longer.</p>

<p>It looks very strange. For me the equation should be:  </p>

<p><code>New Difficulty = Old Difficulty * (20160 minutes / Actual Time of Last 2016 Blocks)</code></p>

<p>However, I found this fomula is consistent with that in bitcoin code.  </p>

<pre><code>unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&amp; params)
{
    if (params.fPowNoRetargeting)
        return pindexLast-&gt;nBits;

    // Limit adjustment step
    int64_t nActualTimespan = pindexLast-&gt;GetBlockTime() - nFirstBlockTime;
    if (nActualTimespan &lt; params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan &gt; params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;

    // Retarget
    const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
    arith_uint256 bnNew;
    bnNew.SetCompact(pindexLast-&gt;nBits);
    bnNew *= nActualTimespan;
    bnNew /= params.nPowTargetTimespan;

    if (bnNew &gt; bnPowLimit)
        bnNew = bnPowLimit;

    return bnNew.GetCompact();
}
</code></pre>

<p>Then I found this <a href="https://bitcoin.stackexchange.com/a/5840/51036">answer</a>.<br>
This is what I understand, but why it is different from the equation of the book and the code.</p>

<p><strong>update</strong></p>

<p>I think this was just an ambiguous expression.<br>
The better expression should be:  </p>

<p><code>New Difficulty Target = Old Difficulty Target * (Actual Time of Last 2016 Blocks / 20160 minutes)</code></p>

<difficulty><mastering-bitcoin>
A problem on difficulty equation
Ello
# A problem on difficulty equation

(Asked by: username_0 on 2017-06-06)

When I read this book [Master Bitcoin](https://github.com/bitcoinbook/bitcoinbook), I have a problem in understanding the "Difficulty" in Chapter 8.

There is a equation of difficulty:

`New Difficulty = Old Difficulty * (Actual Time of Last 2016 Blocks / 20160 minutes)`

I feel confused.  
According to the formula, if **Actual Time** was longer than **20160 minutes**, the **New Difficulty** would be bigger than **Old Difficulty**, and then **Actual Time** would be longer and longer.

It looks very strange. For me the equation should be:

`New Difficulty = Old Difficulty * (20160 minutes / Actual Time of Last 2016 Blocks)`

However, I found this fomula is consistent with that in bitcoin code.

```
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{
    if (params.fPowNoRetargeting)
        return pindexLast->nBits;

    // Limit adjustment step
    int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
    if (nActualTimespan < params.nPowTargetTimespan/4)
        nActualTimespan = params.nPowTargetTimespan/4;
    if (nActualTimespan > params.nPowTargetTimespan*4)
        nActualTimespan = params.nPowTargetTimespan*4;

    // Retarget
    const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
    arith_uint256 bnNew;
    bnNew.SetCompact(pindexLast->nBits);
    bnNew *= nActualTimespan;
    bnNew /= params.nPowTargetTimespan;

    if (bnNew > bnPowLimit)
        bnNew = bnPowLimit;

    return bnNew.GetCompact();
}

```

Then I found this [answer](https://bitcoin.stackexchange.com/a/5840/51036).  
This is what I understand, but why it is different from the equation of the book and the code.

**update**

I think this was just an ambiguous expression.  
The better expression should be:

`New Difficulty Target = Old Difficulty Target * (Actual Time of Last 2016 Blocks / 20160 minutes)`

**Tags:** difficulty, mastering-bitcoin


---

## 1 answers

### Answer by username_1 on 2017-06-06. Score: 3 | Accepted answer

The code you're citing does not actually deal with difficulty. It deals with *targets*. The target is the maximum 256-bit value than the block hash is allowed to have.

The difficulty is defined as **2^208 \* 65535 / target**.

As for the book, that looks wrong indeed.

> **Comments:**
> **username_0**: Thanks for answer, as you said, the description of the book is wrong?  
> **username_1**: The book is wrong.  

---

