我正在尝试使用 scipy 的 Voronoi 镶嵌
http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.spatial.Voronoi.html
但python不会导入它
from scipy.spatial import Voronoi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name Voronoi
Run Code Online (Sandbox Code Playgroud)
但是,我从 scipy.spatial 导入其他类没有问题
from scipy.spatial import Delaunay
Run Code Online (Sandbox Code Playgroud) 我重复相同的计算两次,但在一次我得到一个浮点异常,而在另一个我没有.
#include <iostream>
#include <cmath>
#include <fenv.h>
using namespace std;
int main(void)
{
feenableexcept(-1);
double x,y,z;
x = 1.0;
y = (1.0/(24.3*24.0*3600.0))*x;
cout << "y = " << y << endl;
z = x/(24.3*24.0*3600.0);
cout << "z = " << z << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在g ++和clang ++上测试了它,并在两者中得到了以下输出
y = 4.76299e-07
Floating point exception
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
我有这段代码可以用clang(甚至是-Weverything)编译好,但gcc会发出错误.
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class PhonebookWriter
{
public:
PhonebookWriter(const string& fname):
fname_(fname), names_(), numbers_() {}
PhonebookWriter& operator()(const string& name,
const string& number)
{
names_.push_back(name);
numbers_.push_back(number);
return *this;
}
~PhonebookWriter(void)
{
ofstream f(fname_.c_str());
for(size_t i=0;i<names_.size();++i)
f << names_[i] << " " << numbers_[i] << "\n";
f.close();
}
private:
const string fname_;
vector<string> names_;
vector<string> numbers_;
};
namespace {
void write_guests_data(const string& fname)
{
PhonebookWriter(fname)("Mr Foo Bar","12345")("Mrs Bar Foo","54321");
}
}
int main(void)
{
write_guests_data("phone_book.txt");
return …Run Code Online (Sandbox Code Playgroud) 假设你用c ++编写了一个函数,但是心不在焉地忘记输入这个单词return.在那种情况下会发生什么?我希望编译器会抱怨,或者一旦程序达到这一点,至少会出现一个分段错误.然而,实际发生的事情要糟糕得多:程序会喷出垃圾.不仅如此,实际输出还取决于优化水平!以下是一些演示此问题的代码:
#include <iostream>
#include <vector>
using namespace std;
double max_1(double n1,
double n2)
{
if(n1>n2)
n1;
else
n2;
}
int max_2(const int n1,
const int n2)
{
if(n1>n2)
n1;
else
n2;
}
size_t max_length(const vector<int>& v1,
const vector<int>& v2)
{
if(v1.size()>v2.size())
v1.size();
else
v2.size();
}
int main(void)
{
cout << max_1(3,4) << endl;
cout << max_1(4,3) << endl;
cout << max_2(3,4) << endl;
cout << max_2(4,3) << endl;
cout << max_length(vector<int>(3,1),vector<int>(4,1)) << endl;
cout << max_length(vector<int>(4,1),vector<int>(3,1)) << …Run Code Online (Sandbox Code Playgroud)