· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
MPFR

A C library for Multiple-Precision Floating-point computations with correct Rounding.



GMP¸¦ »ç¿ëÇÑ ½Ç¼öÀü¿ë ¶óÀ̺귯¸®

¿¹Á¦


´ÙÀ½Àº 200-bit Á¤¹Ðµµ(precision)·Î 1+1/1!+1/2!+...+1/100!¸¦ ±¸ÇÏ´Â ÇÁ·Î±×·¥ÀÔ´Ï´Ù.

mpfr_t s, t, u;
¼¼°³ÀÇ s, t, u ½Ç¼ö¸¦ Á¤ÀÇ
mpfr_init2 (t, 200);
200-bit Á¤¹Ðµµ·Î ÃʱâÈ­
mpfr_set_d (t, 1.0, GMP_RNDD);
º¯¼ö t¿¡ doubleÇü º¯¼ö 1.0À» Áý¾î³ÖµÇ, -InfinityÂÊ(downward rounding) ¹Ý¿Ã¸²À» Çϵµ·Ï ÇÑ´Ù. (ÀÌ °æ¿ì ¹Ý¿Ã¸²ÀÌ ÀϾÁö ¾Ê´Âµ¥, 1Àº 200ºñÆ®¿¡¼­ Á¤È®ÇÏ°Ô 1·Î Ç¥ÇöµÇ±â ¶§¹®.
mpfr_mul_ui (t, t, i, GMP_RNDU);
t¿¡ unsigned intÀÎ i¸¦ °öÇؼ­ t¿¡ ³Ö´Â´Ù. °á°ú°ªÀº +InfinityÂÊ (upward rounding)
mpfr_div (u, u, t, GMP_RNDD);
u¸¦ t·Î ³ª´«´Ù. °á°ú´Â -InfinityÂÊÀ¸·Î ¹Ý¿Ã¸²ÇÏ°í u¿¡ ÀúÀåÇÑ´Ù.
mpfr_out_str (stdout, 10, 0, s, GMP_RNDD);
10À» base·Î ÇÏ´Â Ãâ·ÂÀ» s·Î -InfinityÂÊÀ¸·Î ¹Ý¿Ã¸². ¼¼¹ø° ÀÎÀÚ 0Àº ÇÁ¸°Æ®µÉ ¼ýÀÚ¸¦ ÀÚµ¿À¸·Î ¼³Á¤Çϵµ·Ï ÇÑ´Ù.

mpfr_clear
MPFR¿¡¼­ »ç¿ëµÈ °ø°£À» Áö¿î´Ù.

#include <stdio.h>

#include <gmp.h>
#include <mpfr.h>

int main (void)
{
  unsigned int i;
  mpfr_t s, t, u;

  mpfr_init2 (t, 200);
  mpfr_set_d (t, 1.0, GMP_RNDD);
  mpfr_init2 (s, 200);
  mpfr_set_d (s, 1.0, GMP_RNDD);
  mpfr_init2 (u, 200);
  for (i = 1; i <= 100; i++)
    {
      mpfr_mul_ui (t, t, i, GMP_RNDU);
      mpfr_set_d (u, 1.0, GMP_RNDD);
      mpfr_div (u, u, t, GMP_RNDD);
      mpfr_add (s, s, u, GMP_RNDD);
    }
  printf ("Sum is ");
  mpfr_out_str (stdout, 10, 0, s, GMP_RNDD);
  putchar ('\n');
  mpfr_clear (s);
  mpfr_clear (t);
  mpfr_clear (u);
  return 0;
}


°á°ú
$ ./sample
Sum is 2.718281828459045235360287471352662497757247093699959574966913


ID
Password
Join
Men seldom show dimples to girls who have pimples.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2008-11-08 20:45:55
Processing time 0.0035 sec