public virtual double TargetToDifficulty(UInt256 target) { // implementation is equivalent of HighestTarget / target // perform the division UInt256 remainder; var result = UInt256.DivRem(HighestTarget, target, out remainder); // count the leading zeros in the highest target, use this to determine significant digits in the division var insignificant = HighestTarget.ToByteArray().Reverse().TakeWhile(x => x == 0).Count(); // take only as many significant digits as can fit into a double (8 bytes) to calculate the fractional value var remainderDouble = (double)(Bits.ToUInt64((remainder >> (8 * insignificant)).ToByteArray().Take(8).ToArray())); var divisorDouble = (double)(Bits.ToUInt64((target >> (8 * insignificant)).ToByteArray().Take(8).ToArray())); // return the difficulty whole value plus the fractional value return((double)result + (remainderDouble / divisorDouble)); }
public virtual double TargetToDifficulty(UInt256 target) { // difficulty is HighestTarget / target // since these are 256-bit numbers, use division trick for BigIntegers return(Math.Exp(BigInteger.Log(HighestTarget.ToBigInteger()) - BigInteger.Log(target.ToBigInteger()))); }