小编Adh*_*tha的帖子

python中的无限总和

我听说python可以做无限的总和.例如,如果我想评估无限和:

1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...
Run Code Online (Sandbox Code Playgroud)

我该怎么办?我是python的新手.所以如果有人可以写出整个代码并且我需要包含/导入某些内容,我将不胜感激.

例如,在wolfram alpha中,如果我输入Summation (-1)^(n-1)/(2*n-1) from n=1 to infinity它给出答案为0.785395.我想要计算出所需精度的答案,例如wolfram alpha最多6位数.

此外,我在这里看了这篇文章并试图模仿,但它给了我以下错误:

`NameError: name 'Infinity' is not defined`
`NameError: name 'Inf' is not defined`
Run Code Online (Sandbox Code Playgroud)

谢谢,Adhvaitha

python

6
推荐指数
3
解决办法
1万
查看次数

有关使用此指针的问题

我尝试使用类中的方法和this指针将指针复制到另一个指针,如下所示.我给出了完整的测试代码,以便明确发生了什么.

class test  {
private:
    int x;
public:
    void setx(int x);
    int getx(void);
    void copy(test *temp);
};

void test::setx(int x)  {
    this->x = x;
}

int test::getx(void)    {
    return this->x;
}

void test::copy(test *temp) {
    this = temp;
}
Run Code Online (Sandbox Code Playgroud)

我从主要访问此方法如下:

int main()  {
    test a;
    a.setx(4);
    cout << a.getx()<<endl;
    test *b = new test;
    b->setx(4);
    cout << b->getx()<<endl;
    test *c;
    c=b;
    cout << c->getx()<<endl;
    test *d;
    d->copy(b);
    cout << d->getx()<<endl;
}
Run Code Online (Sandbox Code Playgroud)

但是它会出现以下错误

In member function ‘void test::copy(test*)’:
error: …
Run Code Online (Sandbox Code Playgroud)

c++ pointers this

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

标签 统计

c++ ×1

pointers ×1

python ×1

this ×1