FrontPage|FindPage|TitleIndex|RecentChanges|RSS SQRT
 
?Joinc¿¡¼­ º¸´Ù°¡, °³Á¶Çغ¸±â·Î ¸¾¸Ô¾úÀ½

¿ø¹® #

#include <stdio.h> 
#include <unistd.h> 
#include <math.h> 

float mysqrt(int); 
float guess(float *, int); 
void sqrt_iter(float, int); 

int main(int argc, char **argv) 
{ 
    mysqrt(atoi(argv[1])); 
    printf("%f\n",sqrt(atoi(argv[1]))); 
} 

float mysqrt(int inputnum) 
{ 
    sqrt_iter (1, inputnum); 
} 

void sqrt_iter(float fguess, int inputnum) 
{ 
    float value; 
    value = guess(&fguess, inputnum); 
//    printf("return %.8f\n", value); 
} 

float guess(float *fguess, int inputnum) 
{ 
    float re_fguess; 
    re_fguess = ((inputnum/(*fguess))+*fguess)/2; 
    if (re_fguess != *fguess) 
    { 
//        printf("%.8f %.8f\n", re_fguess, fguess); 
        return guess(&re_fguess, inputnum); 
    } 
    return re_fguess; 
} 

»ý°¢µé #

Àç±ÍÈ£ÃâÀ» ¾²Áö ¾Ê°í, ·çÇÁ·Î ÇØ°áÇÑ´Ù.

ÄÚµù #

float biscuit_sqrt(double a)
{
    float guess = 1, re_guess = (a/2 + 2)/2;

    while(re_guess != guess){
        guess   = re_guess;
        re_guess = ((a/guess) + guess ) / 2;
    }

    return re_guess;
}

Taylor Àü°³ #

f(x_i+1) = f(x_i) + f'(x_i)(x_i+1 - x_i) + f''(x_i)(x_i+1 - x_i)^2 / 2! + ....
Á¤¸®Çϸé,
x_i+1 = x_i - f(x_i) / f'(x_i)

Áï,
x2 = x1 - f(x1) / f'(x1)
x3 = x2 - f(x2) / f'(x2)
...

Àû¿ëÇϱâ À§ÇØ, ¹®Á¦¸¦ ´Ù½Ã »ìÆ캸¸é,
x = ·çÆ®2 ; 
¾çº¯¿¡ Á¦°öÀ» Çϸé
x^2 = 2; 

Á¤¸®Çϸé,
x^2 - 2 = 0;

¹ÌºÐÇϸé,
f'(x) = 2x;

Áï, f(x) = x^2 - 2; f'(x) = 2x ¸¦ ¾ò´Â´Ù. ÃʱⰪ x1 À» 1.5¶ó Çϸé,

x2 = x1 - f(x1)/f'(x1)
   = 1.5 -  (1.5^2 - 2) / 3
   = 1.416667

x3 = x2 - f(x2)/f'(x2)
   = 1.414216

...

°è»êÀ» °è¼Ó ÇÒ¼ö·Ï Á¡Á¡ °ªÀÌ Á¤È®ÇØÁ® °£´Ù. ¸¸¾à x = ·çÆ®3 ÀÇ °æ¿ì¶ó¸é,
x = ·çÆ®3 ; 
¾çº¯¿¡ Á¦°öÀ» Çϸé
x^2 = 3; 

Á¤¸®Çϸé,
x^2 - 3 = 0;

¹ÌºÐÇϸé,
f'(x) = 2x;

Áï, 
f(x) = x^2 - 3; (2¸¸ 3À¸·Î ¹Ù²î¾ú´Ù!)
f'(x) = 2x; (º¯ÇÔ¾ø´Ù!)

ÀϹÝÈ­Çϸé,
f(x) = x^2 - N;
f'(x) = 2x;


Taylor Àü°³ ÄÚµù #

float sqrt_taylor(float a)
{
    float guess = 0, x = a/2; // ÃʱⰪ

    while(guess != x){ 
        guess = x;
        x = x - (x*x - a) / (x*2);
    }
    return x;
}

°íÂû #

¿øº»Ã³·³ Àç±ÍÈ£Ãâ·Î ±¸ÇÏ´Â °æ¿ì, »õ ÇÔ¼ö·Î ±¸ÇÏ´Â °æ¿ì, Ç¥ÁØÇÔ¼ö math.hÀÇ sqrt¸¦ ÀÌ¿ëÇÏ´Â °æ¿ì¸¦ ºñ±³ÇØ º¸¾Ò´Ù. °¢°¢ 1¸¸¹ø ¼öÇà, ½Ã°£´ÜÀ§´Â us

±¸Çϴ°ªÇ¥ÁØÇÔ¼ö sqrtÀç±ÍÈ£Ãâ mysqrtºñ½ºÅ¶¹æ¹ýsqrtÅ×ÀÏ·¯Àü°³ sqrt
21978583153296013
71981693853556535
20021980118541024610829
Ç¥ÁØÇÔ¼ö°¡ °¡Àå, 3¹è ÀÌ»ó ºü¸£¸ç, ³ª¸ÓÁö´Â ´Ù ºñ½ÁÇÏ´Ù -_-; ƯÀÌÇÑ°ÍÀº Á÷Á¢¸¸µç ³ÑµéÀº a °ªÀÌ Ä¿Áú¼ö·Ï ½Ã°£ÀÌ ¿À·¡°É¸°´Ù´Â --;

ÂüÁ¶¸µÅ© #

  1. Newton's Method
  2. http://www.joinc.co.kr/modules.php?op=modload&name=Forum&file=viewtopic&topic=28190&forum=3&1
  3. http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.4

last modified 2004-07-14 16:04:05
EditText|FindPage|DeletePage|LikePages|