I have a number in the decimal system. Type double. I translate it using the fractional part of the cycle the odds, it looks like this:
double part;
part = part - int(part);
for (auto i = 0; i < ACCURACY; i++) //Точность
{
part *= typeEncode;
result += std::to_string(int(part));
if (part >= typeEncode / 2)
{
part -= int(part);
}
}
And I transfer such number:
double E1 = 0.15625;
And it turns out I find the number of elements equal ACCURACY. How do I calculate the number of ACCURACY that for each number it is unique? And then there was the extra zeros or circumcision binary numbers.
The internal representation of double
is not decimal, it's already binary, and it's defined by an international standard, IEEE 754. So double
is 64-bit and consists of 3 parts: the sign (1 bit), the exponent (11 bits) and the significand (52 bits).
Roughly saying, this separation allows to store both very small and very large numbers with the same precision: exponent contains information about the magnitude and significand stores the actual value.
And that's why we immediately see the problem of your program: first you take only the fractional part, here some information is lost and you don't know how much is lost. Then you try to do some kind of conversion a-la "divide and conquer", but the problem is the scale: if you divide the interval [0, 1] into two equal parts [0, 0.5) and [0.5, 1], there will be much more numbers in the first of them.
A good point to start with is probably this article (it's in Russian), or the English Wikipedia article on double-precision numbers. After understanding the internal representation, you'll probably be able to extract the bits you want by simple logical operations (like &
and >>
). If you need production-quality code for decimal-to-binary conversion, I would recommend this library: https://github.com/floitsch/double-conversion.