double의 확장이 필요할경우 해결방법좀 부탁드립니다.

pjs0919의 이미지

#include<stdio.h>

#define MAX 100

int fac(int);

/*
 * main() :
 *
 */
int main(void)
{
	double exp=1.0;		/* 1/0! = 1 이므로 exp를 1로 초기화한다. */
	int i;
	//printf("자연상수 e를 구합니다.\n");
	for(i=1; i<=MAX; i++)
	{
		exp += (1.0/ (float)fac(i) );
		printf("자연상수 e = %.30f \n", exp);
	}
	return 0;
}
/*
 * fac() :
 *
 */
int fac(int i)
{
	if(i==1)
		return 1;
	else
		return fac(i-1)*i;
}

여기에서 i가 34이후로는 double이 오버플로 되더군요..
더블의 확장이 필요한데...이러한 자료형의 확장이 필요한경우
어떻게 해결해야 하나요?
구지 이런일때문이 아니더라도 ... 다른 컴퓨터아키텍쳐상에서
이식이 필요할경우 분명히 이런작업이 필요할텐데...해결방법좀 알려주시면 감사하겠습니다.

rasungboy의 이미지

pjs0919 wrote:

여기에서 i가 34이후로는 double이 오버플로 되더군요..
더블의 확장이 필요한데...이러한 자료형의 확장이 필요한경우
어떻게 해결해야 하나요?
구지 이런일때문이 아니더라도 ... 다른 컴퓨터아키텍쳐상에서
이식이 필요할경우 분명히 이런작업이 필요할텐데...해결방법좀 알려주시면 감사하겠습니다.

근데 제가 보니 i 가 34 가 되면 fac 호출시 리턴값이 0 입니다.

이경우 exp += (1.0/ (float)fac(i) );

코드에서 문제가 될수 있죠. 0으로 나눌려고 했으니

말이죠. 정확히 뭘 할려는 코드인지 모르겟지만 이부분을 잘

살펴보시길 바랍니다.

근데 0으로 나누면 프로그램 크래쉬 나지 않나요?

singlet의 이미지

   INT_MAX = 2,147,483,647
       13! = 6,227,020,800

ULLONG_MAX = 18,446,744,073,709,551,615
       21! = 51,090,942,171,709,440,000

       33! = 0688 589C C0E9 505E 2F2F EE55 8000 0000 (hex)
       34! = DE1B C4D1 9EFC AC82 445D A75B 0000 0000 (hex)

      100! =  93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,264,
             381,621,468,592,963,895,217,599,993,229,915,608,941,463,976,156,
             518,286,253,697,920,827,223,758,251,185,210,916,864,000,000,000,
             000,000,000,000,000
pjs0919의 이미지

다시 자세히 보니..문제는 double형 exp가 아니더군요..8)

int형 fac()에서 int가 오버플로되면서 음수값이 되자..값이 틀려지면서

이렇게 되는거였네요.....

답변해주셔서 감사합니다.

\(´∇`)ノ.大韓兒 朴鐘緖人

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.