C ++ Primer ex 7.20 - 寻找阶乘 [英] C++ Primer ex 7.20 - finding factorial

查看:68
本文介绍了C ++ Primer ex 7.20 - 寻找阶乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行正常。任何有关改进的建议:


/ * C ++入门 - 4 / e

*

*练习7.20

*声明:

*编写一个程序来查找int的阶乘。

*使用一个iteratice函数。

*

* /


#include< iostream>


long double find_factorial(int num){

/ * 32位int的最大值是32768.所以我们需要使用

long double来存储它的阶乘值,但是仍然可以用于任何值

在1754以上我得到inf作为输出。我没有任何问题,因为我已经成功创建了迭代版本

的阶乘函数。

* /


long double facto = 1;


for(int i = num; i!= 0; --i)

{

facto * = i;

}


return facto;

}

int main()

{

std :: cout<< 输入数字:" ;;

int i;

std :: cin> i;


std: :cout<< Factorial of

<<我

<< " ="

<< find_factorial(i)

<< std :: endl;

返回0;

}

-
http://arnuld.blogspot.com

it runs fine. any advice for the improvement:

/* C++ Primer - 4/e
*
* exercise 7.20
* STATEMENT:
* write a programme to find the factorial of an int.
* use an iteratice function.
*
*/

#include <iostream>

long double find_factorial( int num ) {
/* maximum value of 32 bit int is 32768. so we need to use
long double to store its factorial value, but still for any values
above 1754 i get "inf" as output. i dont''t have any problem with that
kind of output as i have successfully created the iterative version
of the factorial function.
*/

long double facto = 1;

for(int i = num; i != 0; --i)
{
facto *= i;
}

return facto;
}
int main()
{
std::cout << "Enter the number: ";
int i;
std::cin >i;

std::cout << "Factorial of "
<< i
<< " = "
<< find_factorial( i )
<< std::endl;
return 0;
}
--
http://arnuld.blogspot.com

推荐答案

On Fri,2007年8月17日12:17:30 +0500,arnuld写道:
On Fri, 17 Aug 2007 12:17:30 +0500, arnuld wrote:

它运行良好。任何有关改进的建议:


/ * C ++入门 - 4 / e

*

*练习7.20

*声明:

*编写一个程序来查找int的阶乘。

*使用一个iteratice函数。

*

* /


#include< iostream>


long double find_factorial(int num){

/ * 32位int的最大值是32768.所以我们需要使用
it runs fine. any advice for the improvement:

/* C++ Primer - 4/e
*
* exercise 7.20
* STATEMENT:
* write a programme to find the factorial of an int.
* use an iteratice function.
*
*/

#include <iostream>

long double find_factorial( int num ) {
/* maximum value of 32 bit int is 32768. so we need to use



2 ^ 32!= 32768


-

令人讨厌的用户

2^32 != 32768

--
Obnoxious User


2007年8月17日星期五07:25:02 +0000,Obnoxious User写道:
On Fri, 17 Aug 2007 07:25:02 +0000, Obnoxious User wrote:

2 ^ 32!= 32768
2^32 != 32768



ok,无论值是多少,重点是: 长时间的是不会

处理任何大于1754的阶乘值。


-
http://arnuld.blogspot.com

ok, whatever the value is, the point is: "long doouble" is not going to
handle any value greater than the factorial of 1754.

--
http://arnuld.blogspot.com


arnuld写道:
arnuld wrote:

> On Fri,2007年8月17日07:25:02 +0000,Obnoxious User写道:
>On Fri, 17 Aug 2007 07:25:02 +0000, Obnoxious User wrote:


> 2 ^ 32!= 32768
>2^32 != 32768



ok,无论价值如何,


ok, whatever the value is,



难道你不熟悉数字和数字限制

在电脑上工作吗?

相信32768是32位

整数表示的最大值表示对整数和金额的理解非常缺乏

位。

Shouldn''t you familiarize yourself on how numbers and numeric limits
work in computers?
Believing that 32768 is the largest value representable with a 32-bit
integer shows a great lack of understanding about integers and amounts
of bits.


重点是:long doouble是不会

处理任何大于1754的阶乘值。
the point is: "long doouble" is not going to
handle any value greater than the factorial of 1754.



一个long double不会给你*准确*一些

值的结果。当结果足够大时,最后的数字将是

错误,因此结果将是错误的。它不是

给定数字的阶乘。

A long double is not going to give you an *accurate* result from some
value forward. When the result is large enough the last digits will be
wrong, and thus the result will be wrong. It''s not the factorial of the
given number.


这篇关于C ++ Primer ex 7.20 - 寻找阶乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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