Numerical Conversion in C/C++

有些话、适合烂在心里 提交于 2021-01-29 18:19:25

问题


I need to convert a C/C++ double to a 64 bit two's complement, where the Radix point is at bit number 19 (inclusive).

This means that for the format I want to convert to

  • 0x0000 0000 0010 0000 is the number 1
  • 0xFFFF FFFF FFF0 0000 is the number -1
  • 0x0000 0000 0000 0001 is 0.95 x 10^-6
  • 0xFFFF FFFF FFFF FFFF is -0.95 x 10^-6

So far I've though about using the modf function from the C standard library, but that doesn't really cover my needs. I've also looked at some type conversion classes in Boost, but I couldn't find the right solution there either. Does anyone know of a library or easy way to do this conversion? Maybe someone more familiar with Boost can point me in the right direction.

If this helps at all, here is some documentation of how doubles are stored.

Edit:

I have a follow up question, this is really for my own interest. What is 'Radix'? Here it's sort of like a decimal point. But, the only other time I've heard the term Radix was when I was learning about the Discrete Fast Fourier Transform. If I remember correctly, the Radix-II method is fast because there are fewer multiplies need to calculate the DFT.


回答1:


This should give the results you want:

double d = someValue();
int64_t fixed_point = static_cast<int64_t>(d * (1024*1024));



回答2:


Multiply by 2^20 and then convert to the integer type you target (probably long long)



来源:https://stackoverflow.com/questions/1179006/numerical-conversion-in-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!