Home>

### c ++ - how to convert from a floating decimal value to a fixed minority value

Question summary

Float<->I don't understand how the algorithm that allows the conversion of fixed decimal values ​​works. I would like to know the reason why the method shown below works.

Question details

In the first part of this block, the situation is explained using actual code to make the question easier to understand.
The second part explains what you don't understand.

When converting a floating decimal value to a fixed decimal value and holding it, it seems that the following algorithm is generally used.
For the sake of clarity, the conversion shall be performed according to the following rules.
The fractional part of the fixed decimal value is the lower 8 bits.
Fixed decimal values ​​are stored in int type variables (fix).
Floating point numbers are float type variables (floats).

Floating point → fixed point conversion algorithm

int float_to_fixed (float floats)
{
int ret;
ret = (int) (roundf (floats * (1<<8)));// Given floating point number ☓ 2 ^ (bit number of fixed decimal part)
return (ret);
}

Fixed decimal → floating point conversion algorithm

float fixed_to_float (int fixed)
{
float ret;
ret = ((float) fixed/(float) (1<<8));// Cast a fixed minority value to float and divide by 2 ^ (the number of bits in the fractional part of the fixed decimal value)
return (ret);
}

References
1: https://embeddedartistry.com/blog/2018/07/12/simple-fixed-point-conversion-in-c/
2: https://medium.com/incredible-coder/converting-fixed-point-to-floating-point-format-and-vice-versa-6cbc0e32544e

I don't know why floating point can be converted to fixed decimal in this way for the two functions shown above.

Here's why I don't understand.

In my understanding, float and fixed decimal have completely different structures when considering bits.
Floating decimal values ​​are divided into a sign part, an exponent part, and a mantissa part. However, fixed decimal numbers have bits divided into a sign part, an integer part, and a decimal part. So, no matter how much bit shift you make, you will think that floating point cannot be converted to a fixed decimal. This is because the roles of each bit are completely different in the first place.

It is understandable to convert an integer value such as an int type to a fixed decimal value.
Integer values ​​can be understood as having a binary point (decimal point) to the right of the first bit, so if I want to fix to a fixed decimal, you only have to shift the bit to the left by the decimal part.

Did you convey the intent of the question?

In my understanding, float and fixed decimal have completely different structures when considering bits.

I agree

Floating decimal values ​​are divided into a sign part, an exponent part, and a mantissa part. However, fixed minority values ​​have bits divided into a sign part, an integer part, and a decimal part.

I agree

So, no matter how much bit shift you make, you will think that floating point cannot be converted to a fixed decimal. This is because the roles of each bit are completely different in the first place.

That's exactly right.
Bit-shifting a floating-point number type does not give it the correct form as a fixed decimal type.

Now,

ret = (int) (roundf (floats * (1<<8)));

Is this a bit shift in floating point numbers, isn't it?
Floating point to integer256 (= 1<<8)It just multiplies and rounds the decimal part and casts it to an integer.