Current Codebase:
CBigNum GetBlockWork() const
{
CBigNum bnTarget;
// expand the bits value to the current target
bnTarget.SetCompact(nBits);
// if the target is negative, return an undefined difficulty value (0)
if (bnTarget <= 0)
return 0;
// return (2 ^ 256) / (current target + 1)
return (CBigNum(1)<<256) / (bnTarget+1);
}
Original Commit:
CBigNum GetBlockWork() const
{
return (CBigNum(1)<<256) / (CBigNum().SetCompact(nBits)+1);
}
The documented wiki maximum target is 0xffff * 2 ^ 208, therefore the numerator should be CBigNum(0xFFFF) << 208; but this shouldn't cause any problems as the number used is larger than the documented target.
The routine returns 0 when the target is 0, the calling function should check for this return value and halt bitcoin transactions (the system can no longer determine who controls 51% of the resources of the network if the target is 0).
This makes adding 1 to the target unnecessary.
OpenSSL states that their bignums (from which the bitcoin bignums are derived) are integer only, so the fractional component displayed on the wiki is ignored in the client.