当使用具有不同优化选项的 g++-11.1.1 编译时,下面的代码片段的行为是不同的。该代码段启用了浮点异常处理。我不认为算法细节很重要(有点 3D 几何)。它所做的事情自然容易受到执行无效浮点运算的影响,但编写的代码使这些无效运算实际上永远不会发生(例如,if 语句用于确保非零分母)。
#include <array>
#include <cfenv>
#include <csignal>
#include <fstream>
#include <iostream>
#include <limits>
#include <cmath>
#include <vector>
// Vector structure -----------------------------------------------------------
struct Vector
{
double xs[3];
Vector(double x)
{
xs[0] = x;
xs[1] = x;
xs[2] = x;
}
Vector(double x, double y, double z)
{
xs[0] = x;
xs[1] = y;
xs[2] = z;
}
Vector(std::istream& is)
{
is >> xs[0] >> xs[1] >> xs[2];
}
};
// Vector output stream operator ----------------------------------------------
inline …Run Code Online (Sandbox Code Playgroud)