Title: slight error in difficulty computation Post by: Enzo on May 26, 2011, 02:06:21 PM I don't understand this:
$ curl --url http://***:***@127.0.0.1:8332 -H 'content-type: text/plain;' --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getwork", "params": [] }' {"result":{"midstate":"df92c2b8aac64fc5d15a0ff08a8950453ce9e8fb1ecfa39cb8aeaca78bae173a","data":"0000000174dae8b4ae722300d81693b97993f4a57dc7c8151b980d4a00003bfc00000000b79f998 e1df8db02f7d2ba1676e7060a57fa8a5764990629f53173f8b61d8c1f4dde5b591a44b9f2000000 0000000080000000000000000000000000000000000000000000000000000000000000000000000 0000000000080020000","hash1":"0000000000000000000000000000000000000000000000000000000000000000000000800000000 0000000000000000000000000000000000000000000010000","target":"0000000000000000000000000000000000000000000000f2b944000000000000"},"error":null,"id":"curltest"} $ curl --url http://***.***@127.0.0.1:8332 -H 'content-type: text/plain;' --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getdifficulty", "params": [] }' {"result":244139.48158254,"error":null,"id":"curltest"} $ echo "base=10;ibase=16;scale=8;00000000FFFF0000000000000000000000000000000000000000000000000000 / 00000000000044B9F20000000000000000000000000000000000000000000000" |bc 244112.48777433 Why I get 244112.48777433 instead of 244139.48158254 ? Enzo Title: Re: slight error in difficulty computation Post by: realnowhereman on May 28, 2011, 10:15:49 AM Because bc is better at arbitrary precision floating point calculation than bitcoin.
Bitcoin uses an approximation: Code: double GetDifficulty() In case its not clear, it doesn't use the most significant bits, it just shifts the bignums representing the current target and the minimum target down by a fixed number of bits, and then divides those numbers to get a floating point answer. It's not the best approximation for two reasons:
Short of implementing a full arbitrary precision floating point division (which isn't trivial), this psuedo code might be better: Code: unsigned int hb1, hb2; To be honest though, there's no real reason to change it. It's only used for displaying to users, never for calculations. Other than for pride, there's no pressing need to fix it. Title: Re: slight error in difficulty computation Post by: Pieter Wuille on May 28, 2011, 10:24:41 AM There's already a patch to fix it, see https://github.com/bitcoin/bitcoin/pull/276
Title: Re: slight error in difficulty computation Post by: Enzo on May 28, 2011, 02:37:21 PM Because bc is better at arbitrary precision floating point calculation than bitcoin. So I stumbled upon a bug in bitclient (a minor bug, obviously) :)I would like to point out that there's no need of an high precision calculator: target is a big number but with few significant digits. The maximum target has only 4 significant hex digits (FFFF), I don't know how many there are in the current target but I suppose there's no need for it to have many more. |