<small id='8n3qG'></small><noframes id='8n3qG'>

        <tfoot id='8n3qG'></tfoot>
        <i id='8n3qG'><tr id='8n3qG'><dt id='8n3qG'><q id='8n3qG'><span id='8n3qG'><b id='8n3qG'><form id='8n3qG'><ins id='8n3qG'></ins><ul id='8n3qG'></ul><sub id='8n3qG'></sub></form><legend id='8n3qG'></legend><bdo id='8n3qG'><pre id='8n3qG'><center id='8n3qG'></center></pre></bdo></b><th id='8n3qG'></th></span></q></dt></tr></i><div id='8n3qG'><tfoot id='8n3qG'></tfoot><dl id='8n3qG'><fieldset id='8n3qG'></fieldset></dl></div>
        <legend id='8n3qG'><style id='8n3qG'><dir id='8n3qG'><q id='8n3qG'></q></dir></style></legend>
          <bdo id='8n3qG'></bdo><ul id='8n3qG'></ul>

        C++中double/float类型的二进制序列化的可移植性

        Portability of binary serialization of double/float type in C++(C++中double/float类型的二进制序列化的可移植性)

        <small id='pNliI'></small><noframes id='pNliI'>

        <i id='pNliI'><tr id='pNliI'><dt id='pNliI'><q id='pNliI'><span id='pNliI'><b id='pNliI'><form id='pNliI'><ins id='pNliI'></ins><ul id='pNliI'></ul><sub id='pNliI'></sub></form><legend id='pNliI'></legend><bdo id='pNliI'><pre id='pNliI'><center id='pNliI'></center></pre></bdo></b><th id='pNliI'></th></span></q></dt></tr></i><div id='pNliI'><tfoot id='pNliI'></tfoot><dl id='pNliI'><fieldset id='pNliI'></fieldset></dl></div>
        <legend id='pNliI'><style id='pNliI'><dir id='pNliI'><q id='pNliI'></q></dir></style></legend>

                <tbody id='pNliI'></tbody>
              • <tfoot id='pNliI'></tfoot>
                  <bdo id='pNliI'></bdo><ul id='pNliI'></ul>
                  本文介绍了C++中double/float类型的二进制序列化的可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  C++ 标准不讨论 float 和 double 类型的底层布局,只讨论它们应该表示的值范围.(对于有符号类型也是如此,是两个的恭维还是别的什么)

                  The C++ standard does not discuss the underlying layout of float and double types, only the range of values they should represent. (This is also true for signed types, is it two's compliment or something else)

                  我的问题是:用于以可移植方式序列化/反序列化诸如 double 和 float 之类的 POD 类型的技术有哪些?目前看来,唯一的方法是将值按字面表示(如123.456"),double 的 ieee754 布局并不是所有架构的标准.

                  My question is: What the are techniques used to serialize/deserialize POD types such as double and float in a portable manner? At the moment it seems the only way to do this is to have the value represented literally(as in "123.456"), The ieee754 layout for double is not standard on all architectures.

                  推荐答案

                  Brian "Beej Jorgensen" Hall 在他的 网络编程指南 一些将float(对应double)打包到uint32_t 的代码code> (resp. uint64_t) 能够安全地通过网络在两台机器之间传输它,这台机器可能不同意他们的表示.它有一些限制,主要是不支持 NaN 和无穷大.

                  Brian "Beej Jorgensen" Hall gives in his Guide to Network Programming some code to pack float (resp. double) to uint32_t (resp. uint64_t) to be able to safely transmit it over the network between two machine that may not both agree to their representation. It has some limitation, mainly it does not support NaN and infinity.

                  这是他的打包函数:

                  #define pack754_32(f) (pack754((f), 32, 8))
                  #define pack754_64(f) (pack754((f), 64, 11))
                  
                  uint64_t pack754(long double f, unsigned bits, unsigned expbits)
                  {
                      long double fnorm;
                      int shift;
                      long long sign, exp, significand;
                      unsigned significandbits = bits - expbits - 1; // -1 for sign bit
                  
                      if (f == 0.0) return 0; // get this special case out of the way
                  
                      // check sign and begin normalization
                      if (f < 0) { sign = 1; fnorm = -f; }
                      else { sign = 0; fnorm = f; }
                  
                      // get the normalized form of f and track the exponent
                      shift = 0;
                      while(fnorm >= 2.0) { fnorm /= 2.0; shift++; }
                      while(fnorm < 1.0) { fnorm *= 2.0; shift--; }
                      fnorm = fnorm - 1.0;
                  
                      // calculate the binary form (non-float) of the significand data
                      significand = fnorm * ((1LL<<significandbits) + 0.5f);
                  
                      // get the biased exponent
                      exp = shift + ((1<<(expbits-1)) - 1); // shift + bias
                  
                      // return the final answer
                      return (sign<<(bits-1)) | (exp<<(bits-expbits-1)) | significand;
                  }
                  

                  这篇关于C++中double/float类型的二进制序列化的可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  <legend id='EjQNl'><style id='EjQNl'><dir id='EjQNl'><q id='EjQNl'></q></dir></style></legend>
                  • <small id='EjQNl'></small><noframes id='EjQNl'>

                        <bdo id='EjQNl'></bdo><ul id='EjQNl'></ul>

                        <i id='EjQNl'><tr id='EjQNl'><dt id='EjQNl'><q id='EjQNl'><span id='EjQNl'><b id='EjQNl'><form id='EjQNl'><ins id='EjQNl'></ins><ul id='EjQNl'></ul><sub id='EjQNl'></sub></form><legend id='EjQNl'></legend><bdo id='EjQNl'><pre id='EjQNl'><center id='EjQNl'></center></pre></bdo></b><th id='EjQNl'></th></span></q></dt></tr></i><div id='EjQNl'><tfoot id='EjQNl'></tfoot><dl id='EjQNl'><fieldset id='EjQNl'></fieldset></dl></div>
                        <tfoot id='EjQNl'></tfoot>

                              <tbody id='EjQNl'></tbody>