converting 32-bit Q1.31 type to double

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

converting 32-bit Q1.31 type to double

2,018件の閲覧回数
fixed-term_apar
Contributor II

Is there any possible way to convert 32-bit Q1.31 type (tFrac32) variable to a double variable in C language?

0 件の賞賛
返信
3 返答(返信)

1,831件の閲覧回数
kef2
Senior Contributor V

Sure. Fraction type is real value times power of two (31 in your case), then convert result from FP to integer. So to convert it back you need to convert your fraction back to float and divide by power of two.

float f = (float)frac / 2147483648.0;

or

float f = (float)frac * 0.4656612873077392578125E-9; // use multiply by reciprocal of 2^31

or even more faster using ldexp() function from math.h

float f = ldexp(frac, -31);

ldexp multiplies first argument by 2^N, where N is second ldexp argument. ldexp() should be lot faster than multiply on MCUs without FP coprocessor.

Before using it please check ldexp() works well with 0.0 as first argument. I saw few not cheap compilers having implemented it wrong, unexpected results doing something like ldexp(0.0, -30). When first argument is 0.0 should return 0.0 no matter what is 2nd argument.

0 件の賞賛
返信

1,831件の閲覧回数
fixed-term_apar
Contributor II

Thank you for your reply!

But can we do a direct type casting like

tFrac32 y;

double x ;

x = (double)( y);

0 件の賞賛
返信

1,831件の閲覧回数
kef2
Senior Contributor V

No, unless compiler supports nonstandard extension and nonstandard tFrac32 type. In C++ you could probably create tFrac32 class and define typecast operator for it.