Bitcoiner Post on Nostr: It's a mathematically derived numeric value. It's derived by dividing the maximum ...
It's a mathematically derived numeric value. It's derived by dividing the maximum possible target value by the target for the current block.
Here is the code of the math behind it from source code.
#include <iostream>
#include <cmath>
inline float fast_log(float val)
{
int * const exp_ptr = reinterpret_cast <int *>(&val);
int x = *exp_ptr;
const int log_2 = ((x >> 23) & 255) - 128;
x &= ~(255 << 23);
x += 127 << 23;
*exp_ptr = x;
val = ((-1.0f/3) * val + 2) * val - 2.0f/3;
return ((val + log_2) * 0.69314718f);
}
float difficulty(unsigned int bits)
{
static double max_body = fast_log(0x00ffff), scaland = fast_log(256);
return exp(max_body - fast_log(bits & 0x00ffffff) + scaland * (0x1d - ((bits & 0xff000000) >> 24)));
}
int main()
{
std::cout << difficulty(0x1b0404cb) << std::endl;
return 0;
}
Published at
2024-09-12 18:40:40Event JSON
{
"id": "6eb7dab1bbc88c15d9d2e5473df08361e71c8c1a562013940a3caf1759e9bbcd",
"pubkey": "852bce3d191aee9b84b4c6f2a8fdd7939dd94b5f244c2ccb5dba59c239a8b43a",
"created_at": 1726166440,
"kind": 1,
"tags": [
[
"t",
"include"
],
[
"t",
"include"
],
[
"e",
"758ba0fd34961b573ee5f5a3e2b77528efb94b4bd336e1e1ff70cf372fcd281b",
"",
"root"
],
[
"e",
"21a69624f6b0c2a3d63d20affed7d2cadb3662968cf0aa6dd0cfe0ae2fe34db9",
"",
"reply"
],
[
"p",
"852bce3d191aee9b84b4c6f2a8fdd7939dd94b5f244c2ccb5dba59c239a8b43a"
],
[
"p",
"b424934bcc4be1566fc5d193c5325f657f8022787a0ba505a23a30e9b29d130b"
]
],
"content": "It's a mathematically derived numeric value. It's derived by dividing the maximum possible target value by the target for the current block.\n\nHere is the code of the math behind it from source code.\n#include \u003ciostream\u003e\n#include \u003ccmath\u003e\n\ninline float fast_log(float val)\n{\n int * const exp_ptr = reinterpret_cast \u003cint *\u003e(\u0026val);\n int x = *exp_ptr;\n const int log_2 = ((x \u003e\u003e 23) \u0026 255) - 128;\n x \u0026= ~(255 \u003c\u003c 23);\n x += 127 \u003c\u003c 23;\n *exp_ptr = x;\n\n val = ((-1.0f/3) * val + 2) * val - 2.0f/3;\n return ((val + log_2) * 0.69314718f);\n} \n\nfloat difficulty(unsigned int bits)\n{\n static double max_body = fast_log(0x00ffff), scaland = fast_log(256);\n return exp(max_body - fast_log(bits \u0026 0x00ffffff) + scaland * (0x1d - ((bits \u0026 0xff000000) \u003e\u003e 24)));\n}\n\nint main()\n{\n std::cout \u003c\u003c difficulty(0x1b0404cb) \u003c\u003c std::endl;\n return 0;\n}",
"sig": "0a7be04da4048764fadc1d9aa177952b7fafe661eca9fa69057fc34e3c3e9c71b682022c3b36bd8b5b7f6a654ad46ae59ce58ed7430f97698d49f57e4d11be70"
}