小编Den*_*nko的帖子

GNU C++中程序的奇怪行为,使用浮点数

看看这个程序:

#include <iostream>
#include <cmath>

using namespace std;

typedef pair<int, int> coords;

double dist(coords a, coords b)
{
    return sqrt((a.first - b.first) * (a.first - b.first) +
              (a.second - b.second) * (a.second - b.second));
}

int main()
{
    coords A = make_pair(1, 0);
    coords B = make_pair(0, 1);
    coords C = make_pair(-1, 0);
    coords D = make_pair(0, -1);

    cerr.precision(20);
    cerr << dist(A, B) + dist(C, D) << endl;
    cerr << dist(A, D) + dist(B, C) << endl;

    if(dist(A, B) + …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point double gcc compiler-optimization

9
推荐指数
1
解决办法
428
查看次数

标签 统计

c++ ×1

compiler-optimization ×1

double ×1

floating-point ×1

gcc ×1