c++ - visual studio 2012 和 visual studio 2015 的双重结果不同

标签 c++ visual-studio visual-studio-2012 visual-studio-2015

我正在 64 位 Windows 7 上开发数学应用程序。我们最近通过 visual studio 2015 迁移到了 c++11 我有一个问题,我已将其简化为以下小程序

#include "stdafx.h"
#include "iostream"

using namespace std;


int main()
{
    const double OM = 1.10250000000000000E-36;
    std::cout.precision(20);
    std::cout <<   OM;

    int y;
    std::cin >> y;
    return 0;
}

当我编译并运行程序时 1) 在 vi​​sual studio 2012 上,我得到的结果是 1.1025000000000001e-036 2) 在带有 c++11 的 visual studio 2015 上,我得到的结果是 1.1025000000000000611e-36

注意 visual studio 2012 中的额外 0。我们需要有相同的结果。 (注意结果不同,不仅仅是一个额外的 0,最后显示的数字也不同)

我怎样才能使这些相同(即我需要带有额外 0 的旧结果)?这给我带来了很多问题,我希望得到同样的结果。

enter image description here

需要相同结果的原因。上面的程序是对差异的一个小解释。这种差异导致了我的回归失败。有时,这种差异加起来会产生不同的结果。

我希望 visual studio 有一些编译器开关等可能会给我旧的结果。

最佳答案

Visual C++ 的旧方法,具有三位数的指数,似乎是 _set_output_format 的已弃用的 Visual C++ 扩展。 .文档说:

Important

This function is obsolete. Beginning in Visual Studio 2015, it is not available in the CRT.

所以基本上,你运气不好,但并非没有希望。

你可以为double定义你自己的打印函数,并通过std::ios_base::imbue将它链接到std::basic_ostream .这意味着您必须定义一个新的 locale只为满足您的需求。

这是一个解决方案的草图。您必须填写详细信息,这样代码才能很好地处理所有 iostream 格式选项,并且不会忽略 setprecision() 之类的东西。下面的示例代码只是一个示例,它并没有做所有这些事情。要获得完整的解决方案,您需要做一些工作(但不要太多):

template <class Char>
class my_double : public std::num_put<Char>
{
public:
    static my_double * get()
    {

        static my_double * singleton = new my_double;
        return singleton;
    }
private:
    using base = std::num_put<Char>;
    //
    // This method will format your double according to your needs.
    // Refine the code so that it reads and uses all the flags from `str`
    // and uses the `fill` character.
    //
    iter_type do_put(iter_type out, std::ios_base& str, Char fill, double v) const override
    {
        if (v < 0)
        {
            v *= -1;
            *out = '-';
            ++out;
        }
        if (v == 0.0 || std::isnan(v))
        {
            return base::do_put(out, str, fill, v);
        }
        auto exponent = static_cast<int>(std::floor(std::log10(v)));
        auto significand = v / std::pow(10.0, exponent);
        // TODO: Format according to the flags in `str`
        out = base::do_put(out, str, fill, significand);
        *(out++) = 'e';
        if (exponent < 0)
        {
            *(out++) = '-';
            exponent *= -1;
        }
        *(out++) = '0' + ( (exponent / 100) % 10);
        *(out++) = '0' + ((exponent / 10) % 10);
        *(out++) = '0' + (exponent % 10);
        return out;
    }
};

int main()
{

    // !!!
    // This is how you register your formatting function for `double`
    // !!!
    std::cout.imbue(std::locale(cout.getloc(), my_double<char>::get()));
    /// your code goes here:
}

关于c++ - visual studio 2012 和 visual studio 2015 的双重结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52423350/

相关文章:

visual-studio-2012 - 为什么在这个例子中预取加速不是更大?

visual-studio-2012 - 如何将 Visual Studio 安装项目迁移到 InstallShield Limited Edition?

c++ - 是否可以将 "Prototype pattern"调整为右值引用?

c++ - 在C++中是否可以有虚拟类型?

c++ - 为什么 std::string {"const char ptr"} 有效?

asp.net - 出版与复制

c++ - 我如何在 boost::spirit::qi 中解析带有换行符的列表?

c++ - 在 VS 2015 中重用另一个解决方案项目的功能

c++ - 在 VS 2013 编辑器中显示 C++ 编译错误

python - 为 Windows 构建 Vulkan 工具