IEEE标准754浮点数转换【代码篇】

目录

编写一个程序,找出32位单精度IEEE 754浮点表示的给定实数,反之亦然。

示例

Input: real number = 16.75
Output: 0 | 10000011 | 00001100000000000000000

Input: floating point number = 0 | 10000011 | 00001100000000000000000
Output: 16.75

方法:
该实现基于C语言中的Union数据类型,并使用了位字段的概念。

当我们不需要通常分配给某些变量的全部内存,但希望限制这些变量占用的内存数量时,就会分配位字段。在C语言中,Union的成员共享公共内存空间,每次只能访问一个成员。

以下是上述方法的实施情况:
程序1:将实值转换为它的浮点表示形式

	// C program to convert a real value 
// to IEEE 754 floating point representaion 
  
#include <stdio.h> 
  
void printBinary(int n, int i) 
{ 
  
    // Prints the binary representation 
    // of a number n up to i-bits. 
    int k; 
    for (k = i - 1; k >= 0; k--) { 
  
        if ((n >> k) & 1) 
            printf("1"); 
        else
            printf("0"); 
    } 
} 
  
typedef union { 
  
    float f; 
    struct
    { 
  
        // Order is important. 
        // Here the members of the union data structure 
        // use the same memory (32 bits). 
        // The ordering is taken 
        // from the LSB to the MSB. 
        unsigned int mantissa : 23; 
        unsigned int exponent : 8; 
        unsigned int sign : 1; 
  
    } raw; 
} myfloat; 
  
// Function to convert real value 
// to IEEE foating point representation 
void printIEEE(myfloat var) 
{ 
  
    // Prints the IEEE 754 representation 
    // of a float value (32 bits) 
  
    printf("%d | ", var.raw.sign); 
    printBinary(var.raw.exponent, 8); 
    printf(" | "); 
    printBinary(var.raw.mantissa, 23); 
    printf("\n"); 
} 
  
// Driver Code 
int main() 
{ 
  
    // Instantiate the union 
    myfloat var; 
  
    // Get the real value 
    var.f = -2.25; 
  
    // Get the IEEE floating point representation 
    printf("IEEE 754 representation of %f is : \n", 
           var.f); 
    printIEEE(var); 
  
    return 0; 
} 

输出

IEEE 754 representation of -2.250000 is : 
1 | 10000000 | 00100000000000000000000

程序2:将浮点表示形式转换为它的实际值

// C program to convert 
// IEEE 754 floating point representaion 
// into real value 
  
#include <math.h> 
#include <stdio.h> 
  
typedef union { 
  
    float f; 
    struct
    { 
  
        // Order is important. 
        // Here the members of the union data structure 
        // use the same memory (32 bits). 
        // The ordering is taken 
        // from the LSB to the MSB. 
  
        unsigned int mantissa : 23; 
        unsigned int exponent : 8; 
        unsigned int sign : 1; 
  
    } raw; 
} myfloat; 
  
// Function to convert a binary array 
// to the corresponding integer 
unsigned int convertToInt(int* arr, int low, int high) 
{ 
    unsigned f = 0, i; 
    for (i = high; i >= low; i--) { 
        f = f + arr[i] * pow(2, high - i); 
    } 
    return f; 
} 
  
// Driver Code 
int main() 
{ 
  
    // Get the 32-bit floating point number 
    unsigned int ieee[32] 
        = { 1, 
            1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
  
    myfloat var; 
  
    // Convert the least significant 
    // mantissa part (23 bits) 
    // to corresponding decimal integer 
    unsigned f = convertToInt(ieee, 9, 31); 
  
    // Assign integer representation of mantissa 
    var.raw.mantissa = f; 
  
    // Convert the exponent part (8 bits) 
    // to a corresponding decimal integer 
    f = convertToInt(ieee, 1, 8); 
  
    // Assign integer representation 
    // of the exponent 
    var.raw.exponent = f; 
  
    // Assign sign bit 
    var.raw.sign = ieee[0]; 
  
    printf("The float value of the given"
           " IEEE-754 representation is : \n"); 
    printf("%f", var.f); 
} 

输出

The float value of the given IEEE-754 representation is : 
-2.250000

你可能感兴趣的:(#,通信协议---Modbus,通讯协议)