bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mpf accuracy


From: Torbjorn Granlund
Subject: Re: mpf accuracy
Date: 01 Aug 2002 12:45:13 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

[Your message was posted to the GMP developer's list, I follow up to
the bugs list instead.]

Christian Hoenig <address@hidden> writes:

  Hi ML,

  I quickly parsed the ML-archive, but could not find anything concerning my 
  problem. And additionaly I am not that deep in gmp.

  Im using GMP 4.1

  My Problem:
  I do simple mpf arithmetics. Look at the following code:

      mpf_set_default_prec(256);
      mpf_t sum, one, two;
      char* s,* o,* t;
      mp_exp_t es, eo, et;
      mpf_init_set_si(sum, 0);
      mpf_init_set_si(one, 0);
      mpf_init_set_si(two, 0);

      mpf_set_str(one, "1.123456", 10);
      mpf_set_str(two, "1.123456", 10);

      mpf_add(sum, one, two);

      s = mpf_get_str(NULL, &es, 10, 0, sum); 
      o = mpf_get_str(NULL, &eo, 10, 0, one); 
      t = mpf_get_str(NULL, &et, 10, 0, two); 

      qDebug("sum: \'%s\' exp: \'%d\'",s,(int)es);
      qDebug("one: \'%s\' exp: \'%d\'",o,(int)eo);
      qDebug("two: \'%s\' exp: \'%d\'",t,(int)et);

  The output is:

  sum: 
  
'2246911999999999999999999999999999999999999999999999999999999999999999999999984'
 
  exp: '1'
  one: '1123456' exp: '1'
  two: '1123456' exp: '1'

  I wanted to use GMP to avoid such problems :-(. What am I doing wrong?

"Such" problems?

Are you surprised that numbers aren't represented exactly
even with GMP?  In floating-point notation (using a base
where 10 is not a factor) a base-10 number like 1.123456
cannot be represented exactly.  GMP uses a better
approximation than is possible for the `double' type,
but still not the exact value.

-- 
Torbjörn



reply via email to

[Prev in Thread] Current Thread [Next in Thread]