<tfoot id='rIwG3'></tfoot>

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

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

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

    1. <legend id='rIwG3'><style id='rIwG3'><dir id='rIwG3'><q id='rIwG3'></q></dir></style></legend>

      C++:“float"的 printf() 格式规范是什么?

      时间:2023-10-06

          <tbody id='omZHV'></tbody>
          <bdo id='omZHV'></bdo><ul id='omZHV'></ul>

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

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

              1. <tfoot id='omZHV'></tfoot>
              2. 本文介绍了C++:“float"的 printf() 格式规范是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                C++:floatprintf() 格式规范是什么?(Visual C++)

                C++: What is the printf() format spec for float? (Visual C++)

                过去我用 %g 表示 float%lg 表示 double.

                It used to be that I used %g for float and %lg for double.

                看起来规范已更改,float 未定义,double%g.

                It looks like the spec changed and float is undefined and double is %g.

                我的内存中有一些要打印出来,因此无法选择强制转换.

                I have bits in memory that I am printing out so casting is not an option.

                有没有办法使用 printf() 打印出 float 值?

                Is there a way that I can print out float values using printf() ?

                更新:

                此代码是为对嵌入式系统上使用的通用 C++ 库进行单元测试而编写的.这是我必须做的才能让 float 工作.代码在模板函数中:

                This code was written for unit testing generic C++ libs used on an embedded system. Here's what I had to do to get the float to work. The code is in a template function:

                template <typename T,typename TTYP,typename Ttyp,int bits,bool IsSigned> 
                Error testMatrixT() 
                { ...
                

                这是一个代码片段:

                if (typeid(Ttyp) == typeid(float)) {    
                    float64 c = *(float32*)&Tp(row,col);
                    float64 a1 = *(float32*)&Arg1(row,col);
                    float64 a2 = *(float32*)&Arg2(row,col);
                    float64 e = *(float32*)&Exp(row,col);
                    m_b = (c == e);
                    _snprintf(m_acDiag, sizeof(m_acDiag)-1
                        , "add(Arg1,Arg2): arg1=%g, arg2=%g, Expected=%g, Actual=%g, Result: %s"
                        , a1, a2, e, c, BOOL_PF(m_b));
                } else {
                    ...
                

                很丑吧?使用浮点数作为参数会产生错误的输出.也许是因为使用 _snprintf() ?几年前,我会使用 %lg 并且没问题.没有了.

                Pretty ugly isn't it? Using floats as args give bad output. Maybe due to using _snprintf() ? Years ago I would use %lg and it would be OK. Not anymore.

                推荐答案

                double 和 float 使用与 printf (%a, %e, %f,和 %g).这是因为 printf 是一个可变参数函数.在调用之前,任何浮点参数都被隐式提升为双精度;您实际上无法将浮点数传递给 printf.

                double and float use the same format specifiers with printf (%a, %e, %f, and %g). This is because printf is a variadic function. Any float arguments are implicitly promoted to double before the call; you can't actually pass a float to printf.

                这篇关于C++:“float"的 printf() 格式规范是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:CUDA 链接错误 - Visual Express 2008 - 由于(空)配置文件 下一篇:__attribute__((format(printf, 1, 2))) 用于 MSVC?

                相关文章

                最新文章

              3. <i id='thTfx'><tr id='thTfx'><dt id='thTfx'><q id='thTfx'><span id='thTfx'><b id='thTfx'><form id='thTfx'><ins id='thTfx'></ins><ul id='thTfx'></ul><sub id='thTfx'></sub></form><legend id='thTfx'></legend><bdo id='thTfx'><pre id='thTfx'><center id='thTfx'></center></pre></bdo></b><th id='thTfx'></th></span></q></dt></tr></i><div id='thTfx'><tfoot id='thTfx'></tfoot><dl id='thTfx'><fieldset id='thTfx'></fieldset></dl></div>
                  <bdo id='thTfx'></bdo><ul id='thTfx'></ul>
                <tfoot id='thTfx'></tfoot>
              4. <small id='thTfx'></small><noframes id='thTfx'>

                  <legend id='thTfx'><style id='thTfx'><dir id='thTfx'><q id='thTfx'></q></dir></style></legend>