计算e [英] Calculating e

查看:75
本文介绍了计算e的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写此程序来使用以下公式计算(e)的值(1 + 1/1(1 + 1/2(1 + 1/3(1 + 1/4)))
到目前为止,我已经知道了,但是我得到的答案是1.71828,插入100时应该是2.71828.感谢您的帮助.

i am writing this program to calculate the value of (e) using this formula (1+1/1(1+1/2(1+1/3(1+1/4)))
i have this so far but im getting the answer as 1.71828 and its supposed to be 2.71828 when 100 is plugged in . i appreciate any help.

# include <iostream>
using namespace std;

void main ()
{
double n;
cout<<"please input number:";
cin>>n;
double m=1.0+1.0/n;
int i=n-1;

while (i>0){
	m=1.0+1.0/(m*i);
	--i;
		}
	
cout<<"number is "<<m<<endl;
}

推荐答案

嗨! :)替换行:
Hi!! :) Replace the line:
<br />
m=1.0+1.0/(m*i)<br />



与此:



with this one:

<br />
m = m / i + 1;<br />


问候!

P.S.
但是,我建议您注意Avi的回答. :)


regards!

P.S.
But, I suggest you to pay attention to Avi''s answer. :)


您正在从内而外地处理公式,这似乎是解决该问题的最佳方法.虽然您的公式编码不正确.

您的n级公式为1 +(1/n)(n + 1级的值).

现在,看看您实际用粗体显示的代码.

You are working from the inside out in your formula, which looks to be like the best way to approach it. You aren''t getting your formula coded right though.

Your formula for level n is 1 + (1/n)( value for level n + 1 ).

Now look at what you actually coded on the line in bold.

nfavenom写道:
nfavenom wrote:


void main ()
{
    double n;
    cout << "please input number:";
    cin >> n;
    double m = 1.0 + 1.0 / n;
    int i=n-1;

    while (i>0){
        m = 1.0 + 1.0/(m*i);
        --i;
    }

    cout<<"number is "<<m<<endl;
}



此代码还存在其他一些问题,因此一旦以粗体显示该行,就不会完成.

特别是:

1)您的main()签名错误-请检查C ++文档中main()的有效签名是什么,

2)n的类型错误,应与其他变量之一合并,



3)并非用户可能输入的所有值都是合适的.哪些值不合适?如果用户输入这样的值,当前会发生什么?怎么做才能解决这个问题.



There are also a few other problems with this code as well, so once you fix the line in bold, you aren''t done.

In particular:

1) You have the signature for main() wrong - check C++ documentation for what valid signatures for main() are,

2) n is of the wrong type and should be merged with one of your other variables,

and

3) not all values that the user might enter are appropriate. What values are not appropriate? What currently happens if the user enters such a value? How about doing something to deal with this.


这篇关于计算e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆