Reputation: 195
I am doing Arithmetic Coding now, and I have got the final start position and distance, then I add them. How can I convert the result to binary mode?
For example, how can I convert 0.125 decimal
to 0.001 binary
in C++?
void CArithmeticCoding::Encode()
{
if ( 0 == m_input )
return;
printf("The input is [%s].\n", this->m_input);
while (*m_input)
{
if ( *m_input == m_MPS )
{
DOMPS();
}
else
{
DOLPS();
}
++m_input;
}
double ret = m_start + m_dis;
return;
}
Upvotes: 2
Views: 660
Reputation: 112394
Arithmetic coding is done with integer data types for efficiency and predicability. There are no advantages and only disadvantages to using floating point types. You can simply consider the integer of n bits to be an n-bit fraction. As you take bits off the top, you renormalize the fraction to use those bits.
See Practical Implementations of Arithmetic Coding, and Introduction to Arithmetic Coding - Theory and Practice.
Upvotes: 3
Reputation: 70422
Converting anything to binary means finding out how many of each kind of power of 2 is involved. In the case of a decimal number, the powers involved are negative.
For .125
, the sequence is like this:
.125 x 2 = .250 (< 1)
.250 x 2 = .500 (< 1)
.500 x 2 = 1.000 (>= 1)
.000 = 0 done
So, the binary representation is 0
x2^-1
+ 0
x2^-2
+ 1
x2^-3
= .001
binary. As an exercise, contrast this technique with converting a normal integer into binary representation.
Just as regular decimals can have non-terminating patterns (like 1/3 or pi/4), the same can happen for the binary representations. In those cases, you have to stop the calculation when you reach your desired precision.
Upvotes: 2
Reputation: 8182
You should investigate IEEE 754
This is the standard for binary representation of floating point formats, both single and double precision
Upvotes: 1