zoukankan      html  css  js  c++  java
  • 分离浮点数的整数和小数部分

         问题描述:如何将获取一个浮点数的整数部分以及小数部分

     
    方法一:
     1 #include <iostream>
     2 using namespace std;
     3  
     4 void main()
     5 {
     6      float f = -23.04f;     
     7      int i = f;
     8      float ff = f - i;
     9  
    10      cout << "i=" << i << ", ff=" << ff << endl;
    11 }

    output:

    i=-23, ff=-0.0400009
     
    缺点:不能够精确小数的位数,因为小数部分是由差求得,会丢失部分精度。
     
    方法二:
     1 #include <iostream>
     2 #include <sstream>
     3 #include <string>
     4 using namespace std;
     5  
     6 void main()
     7 {
     8      float f = -23.04f;     
     9      int i = 0;
    10      float ff = 0.0f;
    11  
    12      stringstream ss;
    13      ss << f;
    14      ss >> i >> ff;
    15      cout << "i=" << i << ", ff=" << ff << endl;
    16 }

    output:

    i=-23, ff=0.04
    缺点:小数部分无法获取浮点数的正负号
     
    方法三:
     1 void FloatData(float f)
     2 {
     3      float    fp = f;
     4      unsigned char *p = ( unsigned char *)&fp;
     5      int        nSign = 1;
     6  
     7      /*符号位*/
     8      if(*(p+ 3) & 0x80) /*获取最高位,如果是1则为负*/
     9      {
    10            nSign = - 1;
    11      }
    12      cout << "符号位:" << nSign << endl;
    13      
    14      /*获取阶码*/
    15      int hex = (*(p+ 2) & 0x80) >> 7;
    16      hex |= ((*(p+ 3)& 0x7f) << 1);
    17      hex -= 127;
    18      cout << "阶码:" << hex << endl;
    19  
    20      /*数据部分*/
    21      unsigned long int l_int = 0L;
    22      unsigned char *q = ( unsigned char *)&l_int;
    23  
    24      /*数据拷贝*/
    25      *q     = *p;
    26      *(q+ 1) = *(p+ 1);
    27      *(q+ 2) = *(p+ 2) | 0x80;
    28      *(q+ 3) = 0x00;
    29  
    30      /*数据存储区*/
    31      l_int <<= 8;
    32      cout << "整数部分:" ;
    33      if(hex >= 0)
    34      {
    35            cout << (l_int>>( 32- 1-hex)) << endl;
    36      }
    37      else
    38      {
    39            l_int >>= -hex;
    40            cout << 0 << endl;
    41      }
    42  
    43      /*小数部分*/
    44      unsigned long int tmp = 0L;
    45      if(hex >= 0)
    46            tmp = 1 << ( 32- 1-hex);
    47      else
    48            tmp = 1 << 31;
    49      float sum = 0.0f;
    50      for(int i= 0;i< 31;i++)
    51      {
    52             if((tmp & l_int) && i!= 0)
    53                 sum += 1 * ( float)pow( 2.0f,-i);
    54            tmp >>= 1;
    55      }
    56      cout << "小数部分:" << sum << endl;
    57 }

     

    方法四:
     1 #include <iostream>
     2 #include <sstream>
     3 #include <string>
     4 #include <cmath>
     5 #include <iomanip>    // just for setw()
     6 void splitFloat(float f)
     7 {
     8      stringstream ss;
     9      ss << setprecision( 8) << f; // 如果不设置为8,用默认是值,则会截断小数的位数
    10  
    11      string str;
    12      ss >> str;
    13  
    14      size_t pos = str.find( "-");
    15      int nSign = 1;
    16      if (pos != string::npos)
    17      {
    18            nSign = - 1;
    19            str = str.substr(pos+ 1, str.length()-pos- 1);
    20      }
    21      cout << "nSign = " << nSign << endl;
    22  
    23      // 整数
    24      pos = str.find( ".");
    25      if (pos != string::npos)
    26      {
    27            string tstr = str.substr( 0, pos);
    28            cout << "整数:" << atof(tstr.c_str()) << endl;
    29            str = str.substr(pos, str.length()-pos);
    30      }
    31  
    32      // 小数
    33      cout << "小数:o" << atof(str.c_str()) << endl;
    34 }
     
     
  • 相关阅读:
    数据结构Java版之基数排序(四)
    数据结构Java版之递归与迭代算法(五)
    SpringBoot项目在新电脑上的配置运行,包括JDK+MAVEN+Git+SpringBoot配置等
    .NET 控制Windows文件和目录访问权限研究(FileSystemAccessRule)
    Navicat Premium 12.0.18安装与激活
    Spring Data JPA方法定义规范
    在c/c++中调用Java方法
    在Java中调用C/C++本地库
    XP环境下C# 调用Pocess.start()时提示文件找不到的错误解决办法
    C++ 获取当前正在执行的函数的相关信息
  • 原文地址:https://www.cnblogs.com/520zijuan/p/2886777.html
Copyright © 2011-2022 走看看