Float to binary in C++

后端 未结 7 1397
孤独总比滥情好
孤独总比滥情好 2020-12-09 07:29

I\'m wondering if there is a way to represent a float using a char in C++?

For example:

int main()  
{  
    float test = 4.7567;  
    char result =         


        
7条回答
  •  天命终不由人
    2020-12-09 07:58

    A C char is only 8 bits (on most platforms). The basic problem this causes is two-fold. First, almost all FPUs in existence support IEEE floating point. That means floating point values either require 32 bits, or 64. Some support other non-standard sizes, but the only ones I'm aware of are 80 bits. None I have ever heard of support floats of only 8 bits. So you couldn't have hardware support for an 8-bit float.

    More importantly, you wouldn't be able to get a lot of digits out of an 8-bit float. Remember that some bits are used to represent the exponent. You'd have almost no precision left for your digits.

    Are you perhaps instead wanting to know about Fixed point? That would be doable in a byte.

提交回复
热议问题