bug-gmp
[Top][All Lists]
Advanced

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

Calculation error


From: Wu Zhenhua
Subject: Calculation error
Date: Fri, 12 Dec 2003 11:33:42 -0500

Hi, Dear sir/madam, I'm very happy to use GMP in my research. I found a
bug. The program I used to test is small as below:

#include <iostream>
#include <gmpxx.h>
#include <stdio.h>
#include <gmp.h>
#include <math.h>

using namespace std;

int main(){

  mpf_class cfa, cfb, cfc;


  cfa.set_prec(1000);
  cfb.set_prec(1000);
  cfc.set_prec(1000);

  cfa = 1;
  cfb = 0.1;

  gmp_printf("%Ff, %Ff\n", cfa.get_mpf_t(), cfb.get_mpf_t());

  char num[200];
  mp_exp_t exp_num;

  for(int i = 0; i < 12; i++)
    cfa = cfa * 10;

  gmp_printf("%Ff, %Ff\n", cfa.get_mpf_t(), cfb.get_mpf_t());

  cfc = cfa + cfb;

  gmp_printf("%Ff\n", cfc.get_mpf_t());

  if( cfc > cfa )
    printf("Good\n");
  else
    printf("Bad\n");

  mpf_get_str(num, &exp_num ,10, 150, cfc.get_mpf_t());

  printf("%s, %d\n",num, exp_num);

}

compile like shis:
g++ -g -I{$HOME}/local/include -L{$HOME}/local/lib -lgmpxx -lgmp
-Wl,-rpath,{$HOME}/local/lib test_gmp.cc

The result I got is:

1.000000, 0.100000
1000000000000.000000, 0.100000
1000000000000.100000
Good
10000000000001000000000000000055511151231257827021181583404541015625, 13


In the last line: the number "55511151231257827021181583404541015625"
always appears 16 magnitudes after the last non-zero digit. It seems like
some systematic error inside calculation.

Appreciate your help, Best
zhenhua
-- 
Wu Zhenhua
Center for Polymer Study
Boston University Physics Department
617-353-3891(O)




reply via email to

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