diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile | 71 | ||||
-rw-r--r-- | tests/mon_fichier | 9 | ||||
-rwxr-xr-x | tests/tadd | bin | 0 -> 87676 bytes | |||
-rw-r--r-- | tests/tadd.c | 238 | ||||
-rwxr-xr-x | tests/tagm | bin | 0 -> 261980 bytes | |||
-rw-r--r-- | tests/tagm.c | 162 | ||||
-rwxr-xr-x | tests/tcmp | bin | 0 -> 56308 bytes | |||
-rw-r--r-- | tests/tcmp.c | 57 | ||||
-rwxr-xr-x | tests/tcmp2 | bin | 0 -> 50994 bytes | |||
-rw-r--r-- | tests/tcmp2.c | 52 | ||||
-rwxr-xr-x | tests/tcmp_ui | bin | 0 -> 91932 bytes | |||
-rw-r--r-- | tests/tcmp_ui.c | 35 | ||||
-rwxr-xr-x | tests/tdiv | bin | 0 -> 99622 bytes | |||
-rw-r--r-- | tests/tdiv.c | 78 | ||||
-rwxr-xr-x | tests/tget_str | bin | 0 -> 233208 bytes | |||
-rw-r--r-- | tests/tget_str.c | 77 | ||||
-rwxr-xr-x | tests/tmul | bin | 0 -> 77673 bytes | |||
-rw-r--r-- | tests/tmul.c | 107 | ||||
-rwxr-xr-x | tests/tmul_2exp | bin | 0 -> 47742 bytes | |||
-rw-r--r-- | tests/tmul_2exp.c | 25 | ||||
-rwxr-xr-x | tests/tmul_ui | bin | 0 -> 49667 bytes | |||
-rw-r--r-- | tests/tmul_ui.c | 23 | ||||
-rwxr-xr-x | tests/tround | bin | 0 -> 45819 bytes | |||
-rw-r--r-- | tests/tround.c | 22 | ||||
-rwxr-xr-x | tests/tset_d | bin | 0 -> 50462 bytes | |||
-rw-r--r-- | tests/tset_d.c | 60 | ||||
-rwxr-xr-x | tests/tset_f | bin | 0 -> 89031 bytes | |||
-rw-r--r-- | tests/tset_f.c | 33 | ||||
-rwxr-xr-x | tests/tset_i | bin | 0 -> 48695 bytes | |||
-rw-r--r-- | tests/tset_si.c | 37 | ||||
-rwxr-xr-x | tests/tset_str | bin | 0 -> 37180 bytes | |||
-rw-r--r-- | tests/tset_str.c | 45 | ||||
-rwxr-xr-x | tests/tsqrt | bin | 0 -> 102471 bytes | |||
-rw-r--r-- | tests/tsqrt.c | 63 |
34 files changed, 1194 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 000000000..4e4c837bd --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,71 @@ +CC=gcc +INCFLAGS=-I$(GMPDIR)/include -I.. +LDFLAGS=libmpfr.a $(GMPDIR)/lib/libgmp.a +CFLAGS=-g -DMsb + +all_files=tmul tround tset_f tcmp tcmp2 tdiv tmul_2exp tmul_ui tset_d tset_i tset_str tadd tsqrt tagm + +all: $(all_files) + for f in $(all_files) ; do \ + echo Testing $$f; \ + ./$$f; \ + done + +../$(ODIR)/libmpfr.a: ../mpfr.h + cd .. ; ./mmpfr + +tadd: tadd.c ../add.c + cd .. ; ./mmpfr tadd + -chmod g+w tadd + +tcmp: tcmp.c ../cmp.c + cd .. ; ./mmpfr tcmp + -chmod g+w tcmp + +tcmp2: tcmp2.c ../cmp.c + cd .. ; ./mmpfr tcmp2 + -chmod g+w tcmp2 + +tmul: tmul.c ../mul.c + cd .. ; ./mmpfr tmul + -chmod g+w tmul + +tdiv: tdiv.c ../div.c + cd .. ; ./mmpfr tdiv + -chmod g+w tdiv + +tround: tround.c ../round.c + cd .. ; ./mmpfr tround + -chmod g+w tround + +tset_f: tset_f.c ../set_f.c + cd .. ; ./mmpfr tset_f + -chmod g+w tset_f + +tset_d: tset_d.c ../set_d.c + cd .. ; ./mmpfr tset_d + -chmod g+w tset_d + +tset_si: tset_si.c ../set_si.c + cd .. ; ./mmpfr tset_si + -chmod g+w tset_si + +tset_str: tset_str.c ../set_str_raw.c + cd .. ; ./mmpfr tset_str + -chmod g+w tset_str + +tmul_ui: tmul_ui.c ../mul_ui.c + cd .. ; ./mmpfr tmul_ui + -chmod g+w tmul_ui + +tmul_2exp: tmul_2exp.c ../mul_2exp.c + cd .. ; ./mmpfr tmul_2exp + -chmod g+w tmul_2exp + +tagm: tagm.c ../agm.c + cd .. ; ./mmpfr tagm + -chmod g+w tagm + +clean: + -rm *~ $(all_files) + diff --git a/tests/mon_fichier b/tests/mon_fichier new file mode 100644 index 000000000..bbc5844f7 --- /dev/null +++ b/tests/mon_fichier @@ -0,0 +1,9 @@ +check for a=7.14111721922249727293e+141, b=1.19068418426567829913e+142 +mpfr_agm failed for a=7.14111721922249727292e+141, b=1.19068418426567829912e+142, rnd_mode=3 +expected result is 9.37191344337123761296e+141, got 9.37191344337124167449e+141 (3 ulp) + +check for a=1.10542733675580811212e+116, b=2.98141268622731060995e+116 +mpfr_agm failed for a=1.10542733675580811213e+116, b=2.98141268622731060995e+116, rnd_mode=0 +expected result is 1.92773215447859987843e+116, got 1.92773215447859952847e+116 (1 ulp) + +fin diff --git a/tests/tadd b/tests/tadd Binary files differnew file mode 100755 index 000000000..5c07eb3e1 --- /dev/null +++ b/tests/tadd diff --git a/tests/tadd.c b/tests/tadd.c new file mode 100644 index 000000000..6957181c2 --- /dev/null +++ b/tests/tadd.c @@ -0,0 +1,238 @@ +/* 10^6 additions on a PII-400: +precision * mpf_add mpfr_add(RNDZ/RNDN/RNDU) maple mupad +53 bits 0.003 0.45 0.64/0.59/0.63 +100 bits 0.52 0.76/0.75/0.77 +225 0.54 1.14 +500 bits 0.72 1.58/1.59/1.65 +1000 bits 1.10 2.34 +2017 1.87 3.31 +5025 4.17 4.87 +10017 7.69 7.52 +20017 15.0 13.4 +50017 57.8 37.8 +100017 124. 105. +*/ + +/* #define DEBUG */ + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" + +#define ABS(x) (((x)>0) ? (x) : (-x)) + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + if (lrand48()%2) d=-d; /* generates negative numbers */ + return d; +} + +/* checks that x+y gives the same results in double + and with mpfr with 53 bits of precision */ +void check(double x, double y, unsigned int rnd_mode, unsigned int px, +unsigned int py, unsigned int pz, double res) +{ + double z1,z2; mpfr_t xx,yy,zz; + + /* printf("x=%1.20e, y=%1.20e, rnd_mode=%u px=%u py=%u pz=%u\n",x,y,rnd_mode, + px, py, pz); */ + mpfr_init2(xx, px); + mpfr_init2(yy, py); + mpfr_init2(zz, pz); + mpfr_set_d(xx, x, rnd_mode); + mpfr_set_d(yy, y, rnd_mode); + mpfr_add(zz, xx, yy, rnd_mode); + mpfr_set_machine_rnd_mode(rnd_mode); + z1 = (res==0.0) ? x+y : res; + z2 = mpfr_get_d(zz); + /* printf("x+y=%1.20e\n",z2); */ + if (px==53 && py==53 && pz==53) res=1.0; + if (res!=0.0 && z1!=z2) { + printf("expected sum is %1.20e, got %1.20e\n",z1,z2); + printf("mpfr_add failed for x=%1.20e y=%1.20e with rnd_mode=%u\n",x,y,rnd_mode); + exit(1); + } + mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); +} + +/* returns the number of ulp's between a and b */ +int ulp(a,b) double a,b; +{ + double eps=1.1102230246251565404e-16; /* 2^(-53) */ + b = (a-b)/a; if (b<0) b = -b; + return (int) floor(b/eps); +} + +void check2(x,px,y,py,pz,rnd_mode) double x,y; int px,py,pz,rnd_mode; +{ + mpfr_t xx, yy, zz; double z,z2; int u; + +#ifdef DEBUG +printf("x=%1.20e,%d y=%1.20e,%d pz=%d,rnd=%d\n",x,px,y,py,pz,rnd_mode); +#endif + mpfr_init2(xx,px); mpfr_init2(yy,py); mpfr_init2(zz,pz); + mpfr_set_d(xx, x, rnd_mode); + mpfr_set_d(yy, y, rnd_mode); + mpfr_add(zz, xx, yy, rnd_mode); + mpfr_set_machine_rnd_mode(rnd_mode); + z = x+y; z2=mpfr_get_d(zz); u=ulp(z,z2); +#ifdef DEBUG + printf("x+y=%1.20e,%d (%d ulp) rnd_mode=%d\n",z2,pz,u,rnd_mode); + mpfr_set_d(zz, z2, rnd_mode); + printf("i.e."); mpfr_print_raw(zz); putchar('\n'); +#endif + /* one ulp difference is possible due to composed rounding */ + if (px>=53 && py>=53 && pz>=53 && ABS(u)>1) { + printf("x=%1.20e,%d y=%1.20e,%d pz=%d,rnd=%d\n",x,px,y,py,pz,rnd_mode); + printf("got %1.20e\n",z2); + printf("result should be %1.20e (diff=%d ulp)\n",z,u); + mpfr_set_d(zz, z, rnd_mode); + printf("i.e."); mpfr_print_raw(zz); putchar('\n'); + exit(1); } + mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); +} + +check64() +{ + mpfr_t x, t, u; + mpfr_init2(x, 65); mpfr_init2(t, 65); mpfr_init2(u, 65); + mpfr_set_str_raw(x, "0.10011010101000110101010000000011001001001110001011101011111011101E623"); + mpfr_set_str_raw(t, "0.10011010101000110101010000000011001001001110001011101011111011100E623"); + mpfr_sub(u, x, t, GMP_RNDU); + if (mpfr_get_d(u) != 9.4349060620538533806e167) { /* 2^558 */ + printf("Error (1) in mpfr_sub\n"); exit(1); + } + mpfr_init2(x, 64); mpfr_init2(t, 64); mpfr_init2(u, 64); + mpfr_set_str_raw(x, "0.1000011110101111011110111111000011101011101111101101101100000100E-220"); + mpfr_set_str_raw(t, "0.1000011110101111011110111111000011101011101111101101010011111101E-220"); + mpfr_add(u, x, t, GMP_RNDU); + if ((MANT(u)[0] & 1) != 1) { + printf("error in mpfr_add with rnd_mode=GMP_RNDU\n"); + printf("b= "); mpfr_print_raw(x); putchar('\n'); + printf("c= "); mpfr_print_raw(t); putchar('\n'); + printf("b+c="); mpfr_print_raw(u); putchar('\n'); + exit(1); + } + mpfr_clear(x); mpfr_clear(t); mpfr_clear(u); +} + +main(argc,argv) int argc; char *argv[]; +{ + double x,y; int i,prec,rnd_mode,px,py,pz; + + check64(); + check(1.16809465359248765399e+196, 7.92883212101990665259e+196, + GMP_RNDU, 53, 53, 53, 0.0); + check(3.14553393112021279444e-67, 3.14553401015952024126e-67, + GMP_RNDU, 53, 53, 53, 0.0); + printf("Checking random precisions\n"); + srand(getpid()); + check2(2.26531902208967707071e+168,99,-2.67795218510613988524e+168,67,94,2); + check2(-1.21510626304662318398e+145,70,1.21367733647758957118e+145,65,61,3); + check2(2.73028857032080744543e+155,83,-1.16446121423113355603e+163,59,125,1); + check2(-4.38589520019641698848e+78,155,-1.09923643769309483415e+72,15,159,3); + check2(-1.49963910666191123860e+265,76,-2.30915090591874527520e-191,8,75,1); + check2(5.17945380930936917508e+112,119,1.11369077158813567738e+108,15,150,1); + check2(-2.66910493504493276454e-52,117,1.61188644159592323415e-52,61,68,1); + check2(-1.87427265794105342764e-57,175,1.76570844587489516446e+190,2,115,1); + check2(6.85523243386777784171e+107,187,-2.78148588123699111146e+48,87,178,3); + check2(-1.15706375390780417299e-135,94,-1.07455137477117851576e-129,66,111,2); + check2(-1.15706375390780417299e-135,94,-1.07455137477117851576e-129,66,111,3); + check2(-3.31624349995221499866e-22,107,-8.20150212714204839621e+156,79,99,3); + check2(-1.15706375390780417299e-135,94,-1.07455137477117851576e-129,66,111,3); + check2(-1.08007920352320089721e+150,63,1.77607317509426332389e+73,64,64,0); + check2(4.49465557237618783128e+53,108,-2.45103927353799477871e+48,60,105,0); + check2(3.25471707846623300604e-160,81,-7.93846654265839958715e-274,58,54,0); + check2(-8.88471912490080158206e+253,79,-7.84488427404526918825e+124,95,53,3); + check2(-2.18548638152863831959e-125,61,-1.22788940592412363564e+226,71,54,0); + check2(-7.94156823309993162569e+77,74,-5.26820160805275124882e+80,99,101,3); + check2(-3.85170653452493859064e+189,62,2.18827389706660314090e+158,94,106,3); + check2(1.07966151149311101950e+46,88,1.13198076934147457400e+46,67,53,0); + check2(3.36768223223409657622e+209,55,-9.61624007357265441884e+219,113,53,0); + check2(-6.47376909368979326475e+159,111,5.11127211034490340501e+159,99,62,3); + check2(-4.95229483271607845549e+220,110,-6.06992115033276044773e+213,109,55,0); + check2(-6.47376909368979326475e+159,74,5.11127211034490340501e+159,111,75,2); + check2(2.26531902208967707070e+168,99,-2.67795218510613988525e+168,67,94,2); + check2(-2.28886326552077479586e-188,67,3.41419438647157839320e-177,60,110,2); + check2(-2.66910493504493276454e-52,117,1.61188644159592323415e-52,61,68,1); + check2(2.90983392714730768886e+50,101,2.31299792168440591870e+50,74,105,1); + check2(2.72046257722708717791e+243,97,-1.62158447436486437113e+243,83,96,0); + /* checks random precisions */ + for (i=0;i<1000000;i++) { +#ifdef DEBUG +printf("\nTest i=%d\n",i); +#endif + px = 53 + (rand() % 64); + py = 53 + (rand() % 64); + pz = 53 + (rand() % 64); + rnd_mode = rand() % 4; + do { x = drand(); } while (isnan(x)); + do { y = drand(); } while (isnan(y)); + check2(x,px,y,py,pz,rnd_mode); + } + printf("Checking double precision (53 bits)\n"); + prec = (argc<2) ? 53 : atoi(argv[1]); + rnd_mode = (argc<3) ? -1 : atoi(argv[2]); + check(-8.22183238641455905806e-19, 7.42227178769761587878e-19, + GMP_RNDD, 53, 53, 53, 0.0); + check(5.82106394662028628236e+234, -5.21514064202368477230e+89, + GMP_RNDD, 53, 53, 53, 0.0); + check(5.72931679569871602371e+122, -5.72886070363264321230e+122, + GMP_RNDN, 53, 53, 53, 0.0); + check(-5.09937369394650450820e+238, 2.70203299854862982387e+250, + GMP_RNDD, 53, 53, 53, 0.0); + check(-2.96695924472363684394e+27, 1.22842938251111500000e+16, + GMP_RNDD, 53, 53, 53, 0.0); + check(1.74693641655743793422e-227, -7.71776956366861843469e-229, + GMP_RNDN, 53, 53, 53, 0.0); + check(-1.03432206392780011159e-125, 1.30127034799251347548e-133, + GMP_RNDN, 53, 53, 53, 0.0); + check(1.05824655795525779205e+71, -1.06022698059744327881e+71, + GMP_RNDZ, 53, 53, 53, 0.0); + check(-5.84204911040921732219e+240, 7.26658169050749590763e+240, + GMP_RNDD, 53, 53, 53, 0.0); + /* the following check double overflow */ + /* check(6.27557402141211962228e+307, 1.32141396570101687757e+308, + GMP_RNDZ, 53, 53, 53, 0.0); */ + check(1.00944884131046636376e+221, 2.33809162651471520268e+215, + GMP_RNDN, 53, 53, 53, 0.0); + check(4.29232078932667367325e-278, 1.07735250473897938332e-281, + GMP_RNDU, 53, 53, 53, 0.0); + check(5.27584773801377058681e-80, 8.91207657803547196421e-91, + GMP_RNDN, 53, 53, 53, 0.0); + check(2.99280481918991653800e+272, 5.34637717585790933424e+271, + GMP_RNDN, 53, 53, 53, 0.0); + check(4.67302514390488041733e-184, 2.18321376145645689945e-190, + GMP_RNDN, 53, 53, 53, 0.0); + check(5.57294120336300389254e+71, 2.60596167942024924040e+65, + GMP_RNDZ, 53, 53, 53, 0.0); + x=6151626677899716.0; for (i=0;i<30;i++) x = 2.0*x; + check(x, 4938448004894539.0, GMP_RNDU, 53, 53, 53, 0.0); + check(1.23056185051606761523e-190, 1.64589756643433857138e-181, + GMP_RNDU, 53, 53, 53, 0.0); + check(2.93231171510175981584e-280, 3.26266919161341483877e-273, + GMP_RNDU, 53, 53, 53, 0.0); + check(5.76707395945001907217e-58, 4.74752971449827687074e-51, + GMP_RNDD, 53, 53, 53, 0.0); + check(277363943109.0, 11.0, GMP_RNDN, 53, 53, 53, 0.0); + /* test denormalized numbers too */ + check(8.06294740693074521573e-310, 6.95250701071929654575e-310, + GMP_RNDU, 53, 53, 53, 0.0); + /* compares to results with double precision using machine arithmetic */ + for (i=0;i<1000000;i++) { + x = drand(); + y = drand(); + if (ABS(x)>2.2e-307 && ABS(y)>2.2e-307 && x+y<1.7e+308 && x+y>-1.7e308) + /* avoid denormalized numbers and overflows */ + check(x, y, (rnd_mode==-1) ? lrand48()%4 : rnd_mode, + prec, prec, prec, 0.0); + } +} + diff --git a/tests/tagm b/tests/tagm Binary files differnew file mode 100755 index 000000000..e54c62ada --- /dev/null +++ b/tests/tagm diff --git a/tests/tagm.c b/tests/tagm.c new file mode 100644 index 000000000..6a89b534e --- /dev/null +++ b/tests/tagm.c @@ -0,0 +1,162 @@ +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <unistd.h> +#include "gmp.h" +#include "mpfr.h" + + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + do { + i[0] = lrand48(); + i[1] = lrand48(); + /*if (lrand48()%2) d=-d; */ /* generates negative numbers */ + /* useless here */ + } while ((d<1e-153)||(d>1e153)); /* to avoid underflow or overflow + in double calculus in sqrt(u*v) */ + + return d; +} + + +/* returns the number of ulp's between a and b */ +int ulp(a,b) double a,b; +{ + double eps=1.1102230246251565404e-16; /* 2^(-53) */ + b = (a-b)/a; if (b<0) b = -b; + return (int) floor(b/eps); +} + + +double max(double a,double b) { + if (a>=b) + return a; + return b; +} + +double min(double a,double b) { + if (b>=a) + return a; + return b; +} + + + +double dagm(double a, double b) { + double u,v,tmpu,tmpv; + + if ((isnan(a))||(isnan(b))) + return a+b; + + tmpv=max(a,b); + tmpu=min(a,b); + + while (!(((tmpu==u)&&(tmpv==v))||(ulp(u,v)==0))) { + u=tmpu; + v=tmpv; + tmpu=sqrt(u*v); + tmpv=(u+v)/2.0; + } + /* printf("difference : %i ulp\n",ulp(u,v)); */ + return u; +} + + + +void check(double a, double b, unsigned char rnd_mode) +{ + mpfr_t ta, tb, tres; + double res1, res2; + + mpfr_init2(ta, 53); + mpfr_init2(tb, 53); + mpfr_init2(tres, 53); + + mpfr_set_d(ta, a, rnd_mode); + mpfr_set_d(tb, b, rnd_mode); + + mpfr_agm(tres, ta, tb, rnd_mode); + mpfr_set_machine_rnd_mode(rnd_mode); + + res1=dagm(a,b); + res2 = mpfr_get_d(tres); + + if (res1!=res2 && (!isnan(res1) || !isnan(res2))) { + printf("mpfr_agm failed for a=%1.20e, b=%1.20e, rnd_mode=%d\n",a,b,rnd_mode); + printf("expected result is %1.20e, got %1.20e (%d ulp)\n",res1,res2, + ulp(res2,res1)); + } + else + printf("GOAL !!!\n"); + mpfr_clear(ta); mpfr_clear(tb); mpfr_clear(tres); + +} +/* + void main() +{ + int i; + double a,b,gd,pt; + + check(2,1,GMP_RNDN); + check(6,4,GMP_RNDN); + check(62,61,GMP_RNDN); + check(0.5,1,GMP_RNDN); + check(1,2,GMP_RNDN); + check(234375765,234375000,GMP_RNDN); + check(8,1,GMP_RNDU); + check(1,44,GMP_RNDU); + + srand(getpid()); + + for (i=0;i<100;i++) { + a = drand(); + b = drand(); + + gd=max(a,b); + pt=min(a,b); + + if (((gd-pt)/pt)<2) { + printf("check for a=%1.20e, b=%1.20e\n",a,b); + check(a, b, rand() % 4); + printf("\n"); + } + else { + printf("suit pas hypotheses !\n"); + check(a, b, rand() % 4); + } + } + printf("fin\n"); + } +*/ + +void main() { + mpfr_t a,b,res; + int p; + double op1,op2; + char * strptr,* expptr; + printf("debut\n"); + p=53; + op1=1; + op2=2; + + mpfr_init2(a,p); + mpfr_init2(b,p); + mpfr_init2(res,p); + + mpfr_set_d(a,op1,GMP_RNDN); + mpfr_set_d(b,op2,GMP_RNDN); + mpfr_agm(res,a,b,GMP_RNDN); + + printf("avant\n"); + strptr= mpfr_get_str(NULL,expptr,10,0,res,GMP_RNDN); + + printf("mag entre %f et %f a la precision %i :\n",op1,op2,p); + printf("%sE%s\n",strptr,expptr); +} + + diff --git a/tests/tcmp b/tests/tcmp Binary files differnew file mode 100755 index 000000000..faba0dc31 --- /dev/null +++ b/tests/tcmp diff --git a/tests/tcmp.c b/tests/tcmp.c new file mode 100644 index 000000000..baf36ca6e --- /dev/null +++ b/tests/tcmp.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include "gmp.h" +#include "longlong.h" +#include "mpfr.h" + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + if (lrand48()%2) d=-d; /* generates negative numbers */ + return d; +} + +void main() +{ + double x,y; mpfr_t xx,yy; int i,c; + + mpfr_init2(xx, 65); mpfr_init2(yy, 65); + mpfr_set_str_raw(xx, "0.10011010101000110101010000000011001001001110001011101011111011101E623"); + mpfr_set_str_raw(yy, "0.10011010101000110101010000000011001001001110001011101011111011100E623"); + if (mpfr_cmp2(xx,yy)!=64) { printf("Error (1) in mpfr_cmp\n"); exit(1); } + mpfr_set_str_raw(xx, "0.10100010001110110111000010001000010011111101000100011101000011100"); + mpfr_set_str_raw(yy, "0.10100010001110110111000010001000010011111101000100011101000011011"); + if (mpfr_cmp2(xx,yy)!=64) { printf("Error (1) in mpfr_cmp\n"); exit(1); } + mpfr_init2(xx,53); + mpfr_init2(yy,200); + mpfr_set_d(xx, 1.0, 0); + mpfr_set_d(yy, 1.0, 0); + if (mpfr_cmp(xx,yy)!=0) { + printf("Error in mpfr_cmp: 1.0 != 1.0\n"); exit(1); + } + mpfr_set_prec(yy, 31, GMP_RNDZ); + mpfr_set_d(xx, 1.0000000002, 0); + mpfr_set_d(yy, 1.0, 0); + if (!(mpfr_cmp(xx,yy)>0)) { + printf("Error in mpfr_cmp: not 1.0000000002 > 1.0\n"); exit(1); + } + mpfr_set_prec(yy, 53, GMP_RNDZ); + for (i=0;i<1000000;) { x=drand(); y=drand(); + if (!isnan(x) && !isnan(y)) { + i++; + mpfr_set_d(xx, x, 0); + mpfr_set_d(yy, y, 0); + c = mpfr_cmp(xx,yy); + if ((c>0 && x<=y) || (c==0 && x!=y) || (c<0 && x>=y)) { + printf("Error in mpfr_cmp with x=%1.20e, y=%1.20e mpfr_cmp(x,y)=%d\n", + x,y,c); exit(1); + } + } + } + mpfr_clear(xx); mpfr_clear(yy); +} diff --git a/tests/tcmp2 b/tests/tcmp2 Binary files differnew file mode 100755 index 000000000..1cb3e6073 --- /dev/null +++ b/tests/tcmp2 diff --git a/tests/tcmp2.c b/tests/tcmp2.c new file mode 100644 index 000000000..c4c080b97 --- /dev/null +++ b/tests/tcmp2.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include "gmp.h" +#include "longlong.h" +#include "mpfr.h" + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + return d; +} + +void tcmp2(x, y, i) double x, y; int i; +{ + mpfr_t xx,yy; int j; + + if (i==-1) i = (int) floor(log(x)/log(2.0)) - (int) floor(log(x-y)/log(2.0)); + mpfr_init2(xx, 53); mpfr_init2(yy, 53); + mpfr_set_d(xx, x, 0); + mpfr_set_d(yy, y, 0); + if ((j=mpfr_cmp2(xx, yy)) != i) { + printf("Error in mpfr_cmp2: x=%1.16e y=%1.16e mpfr_cmp(x,y)=%d instead of %d\n",x,y,j,i); + exit(1); + } + mpfr_clear(xx); mpfr_clear(yy); +} + +void main() +{ + int i,j; double x=1.0, y, z; + + tcmp2(1.06022698059744327881e+71, 1.05824655795525779205e+71, -1); + tcmp2(1.0, 1.0, 53); + for (i=0;i<54;i++) { + tcmp2(1.0, 1.0-x, i); + x /= 2.0; + } + for (j=0;j<1000000;j++) { + x = drand(); if (x<0) x = -x; + y = drand(); if (y<0) y = -y; + if (!isnan(x) && !isnan(y)) { + if (x<y) { z=x; x=y; y=z; } + tcmp2(x, y, -1); + } + } +} + diff --git a/tests/tcmp_ui b/tests/tcmp_ui Binary files differnew file mode 100755 index 000000000..9ad3d0cb8 --- /dev/null +++ b/tests/tcmp_ui diff --git a/tests/tcmp_ui.c b/tests/tcmp_ui.c new file mode 100644 index 000000000..98642f7ca --- /dev/null +++ b/tests/tcmp_ui.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include "gmp.h" +#include "longlong.h" +#include "mpfr.h" + +main() +{ + mpfr_t x; unsigned long i; long s; + + mpfr_init(x); + + mpfr_set_ui(x, 3, GMP_RNDZ); + if (mpfr_cmp_ui(x, i=3)!=0) { + printf("Error in mpfr_cmp_ui(%1.20f,%d)\n",mpfr_get_d(x), i); exit(1); + } + if (mpfr_cmp_ui(x, i=2)<=0) { + printf("Error in mpfr_cmp_ui(%1.20f,%d)\n",mpfr_get_d(x), i); exit(1); + } + if (mpfr_cmp_ui(x, i=4)>=0) { + printf("Error in mpfr_cmp_ui(%1.20f,%d)\n",mpfr_get_d(x), i); exit(1); + } + + mpfr_set_si(x, -3, GMP_RNDZ); + if (mpfr_cmp_si(x, s=-3)!=0) { + printf("Error in mpfr_cmp_si(%1.20f,%d)\n",mpfr_get_d(x), s); exit(1); + } + if (mpfr_cmp_si(x, i=-4)<=0) { + printf("Error in mpfr_cmp_si(%1.20f,%d)\n",mpfr_get_d(x), s); exit(1); + } + if (mpfr_cmp_si(x, i=1)>=0) { + printf("Error in mpfr_cmp_si(%1.20f,%d)\n",mpfr_get_d(x), s); exit(1); + } +} diff --git a/tests/tdiv b/tests/tdiv Binary files differnew file mode 100755 index 000000000..55fe33689 --- /dev/null +++ b/tests/tdiv diff --git a/tests/tdiv.c b/tests/tdiv.c new file mode 100644 index 000000000..971970089 --- /dev/null +++ b/tests/tdiv.c @@ -0,0 +1,78 @@ +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "mpfr.h" + +/* #define DEBUG */ + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + if (lrand48()%2) d=-d; /* generates negative numbers */ + return d; +} + +/* returns the number of ulp's between a and b */ +int ulp(a,b) double a,b; +{ + double eps=1.1102230246251565404e-16; /* 2^(-53) */ + b = (a-b)/a; if (b<0) b = -b; + return (int) floor(b/eps); +} + +#define check(n,d,r) check4(n,d,r,53) + +void check4(N, D, rnd_mode, p) double N, D; unsigned char rnd_mode; int p; +{ + mpfr_t q, n, d; double Q,Q2; + +#ifdef DEBUG + printf("N=%1.20e D=%1.20e rnd_mode=%d\n",N,D,rnd_mode); +#endif + mpfr_init2(q, p); mpfr_init2(n, p); mpfr_init2(d, p); + mpfr_set_d(n, N, rnd_mode); + mpfr_set_d(d, D, rnd_mode); + mpfr_div(q, n, d, rnd_mode); + mpfr_set_machine_rnd_mode(rnd_mode); + Q = N/D; + Q2 = mpfr_get_d(q); +#ifdef DEBUG + printf("expected quotient is %1.20e, got %1.20e (%d ulp)\n",Q,Q2, + ulp(Q2,Q)); + mpfr_print_raw(q); putchar('\n'); +#endif + if (Q!=Q2 && (!isnan(Q) || !isnan(Q2))) { + printf("mpfr_div failed for n=%1.20e, d=%1.20e, rnd_mode=%d\n",N,D,rnd_mode); + printf("expected quotient is %1.20e, got %1.20e (%d ulp)\n",Q,Q2, + ulp(Q2,Q)); + exit(1); + } + mpfr_clear(q); mpfr_clear(n); mpfr_clear(d); +} + +void main() +{ + int i; double n, d, e; + + check4(2.44394909079968374564e-150, 2.10263340267725788209e+187, 2, 65); +exit(1); + /* the following tests when d is an exact power of two */ + check(9.89438396044940256501e-134, 5.93472984109987421717e-67, 2); + check(9.89438396044940256501e-134, -5.93472984109987421717e-67, 2); + check(-4.53063926135729747564e-308, 7.02293374921793516813e-84, 3); + check(6.25089225176473806123e-01, -2.35527154824420243364e-230, 3); + check(6.52308934689126000000e+15, -1.62063546601505417497e+273, 0); + check(1.04636807108079349236e-189, 3.72295730823253012954e-292, 1); + for (i=0;i<100000;i++) { + do { n = drand(); d = drand(); e = fabs(n)/fabs(d); } + /* smallest normalized is 2^(-1022), largest is 2^(1023)*(2-2^(-52)) */ + while (e>=1.7976931348623157081e308 || e<2.225073858507201383e-308); + check(n, d, rand() % 4); + } +} diff --git a/tests/tget_str b/tests/tget_str Binary files differnew file mode 100755 index 000000000..4ac65d743 --- /dev/null +++ b/tests/tget_str diff --git a/tests/tget_str.c b/tests/tget_str.c new file mode 100644 index 000000000..6093bce67 --- /dev/null +++ b/tests/tget_str.c @@ -0,0 +1,77 @@ +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "gmp.h" +#include "mpfr.h" +#include <time.h> + +void print_double(d) double d; +{ + int e, i; + + e = (int) ceil(log(fabs(d))/log(2.0)); + /* d <= 2^e */ + e -= 53; + if (e>0) for (i=0;i<e;i++) d /= 2.0; + else for (i=0;i<-e;i++) d *= 2.0; + printf("%1.0f*2^(%d)",d,e); +} + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + if (lrand48()%2) d=-d; /* generates negative numbers */ + return d; +} + +check(d, rnd) double d; unsigned char rnd; +{ + mpfr_t x; char *str, str2[30]; int l, l2; + + mpfr_init2(x, 53); + mpfr_set_d(x, d, 53, 0, rnd); + str = mpfr_get_str(NULL, NULL, 10, 5, x, rnd); + mpfr_set_machine_rnd_mode(rnd); + sprintf(str2, "%1.4e", d); + l2 = strlen(str2); + l = strlen(str); + if (l!=l2) printf("l=%d l2=%d\n",l,l2); + if (str2[l2-3]=='-' && str2[l2-2]=='0' && str2[l2-1]=='0') + str2[l2-3]='+'; /* rule for sign of exponent 0 ? */ + if (strcmp(str, str2)) { + printf("Error in mpfr_get_str for d=%s=",str2); + print_double(d); + printf("\ngot %s\n", str); + exit(1); + } + mpfr_clear(x); + free(str); +} + +int +main(int argc, char **argv) +{ + int i; double d; + + srand(getpid()); + /* printf seems to round towards nearest in all cases, at least with gcc */ + check(4.059650008e-83, 0); + check(-6.606499965302424244461355e233, 0); + check(-7.4, 0); + check(0.997, 0); + check(-4.53063926135729747564e-308, 0); + check(2.14478198760196000000e+16, 0); + check(7.02293374921793516813e-84, 0); + for (i=0;i<100000;i++) { + do { d = drand(); } while (isnan(d)); + check(d, 0); + } +} + + + diff --git a/tests/tmul b/tests/tmul Binary files differnew file mode 100755 index 000000000..4e43f999a --- /dev/null +++ b/tests/tmul diff --git a/tests/tmul.c b/tests/tmul.c new file mode 100644 index 000000000..2f561ca58 --- /dev/null +++ b/tests/tmul.c @@ -0,0 +1,107 @@ +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" + +#define MINNORM 2.2250738585072013831e-308 /* 2^(-1022), smallest normalized */ + +/* 10^6 multiplications on a PII-400: +precision * mpf_mul mpfr_mul(RNDZ/RNDN/RNDU) maple mupad +53 bits 0.018 0.89 1.44/1.58/1.50 15.2[16] 17.1 +100 bits 1.66 2.01/2.38/2.27 20.0[30] 18.7 +200 bits 3.45 Seg. fault 27.6[60] 20.2 +225 4.12 4.33 +500 bits 12.53 12.39 81.5[151] 29.3 +1000 bits 40.26 38.35 190.5[301] 58.6 +2000 bits 169.3 +2017 123. 119. +5025 bits 552 544 1860 918 +*/ + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + if (lrand48()%2) d=-d; /* generates negative numbers */ + return d; +} + +/* checks that x*y gives the same results in double + and with mpfr with 53 bits of precision */ +int check(double x, double y, unsigned int rnd_mode, unsigned int px, +unsigned int py, unsigned int pz, double res) +{ + double z1,z2,z3; mpfr_t xx,yy,zz; int i; + mpf_t xxx,yyy,zzz; + + /* printf("x=%1.20e, y=%1.20e, rnd_mode=%u px=%u py=%u pz=%u\n",x,y,rnd_mode, + px, py, pz); */ + mpfr_init2(xx, px); + mpfr_init2(yy, py); + mpfr_init2(zz, pz); + mpf_init2(xxx,px); mpf_init2(yyy,py); mpf_init2(zzz,pz); + mpf_set_d(xxx, x); mpf_set_d(yyy, y); + mpfr_set_d(xx, x, rnd_mode); + mpfr_set_d(yy, y, rnd_mode); +for (i=0;i<1;i++) mpfr_mul(zz, xx, yy, rnd_mode); + mpf_mul(zzz, xxx, yyy); + mpfr_set_machine_rnd_mode(rnd_mode); + z1 = (res==0.0) ? x*y : res; + z2 = mpfr_get_d(zz); + z3 = mpf_get_d(zzz); + if (px==53 && py==53 && pz==53) res=1.0; + if (res!=0.0 && z1!=z2 && (z1>=MINNORM || z1<=-MINNORM)) { + printf("expected product is %1.20e, got %1.20e\n",z1,z2); + printf("mpfr_mul failed for x=%1.20e y=%1.20e with rnd_mode=%u\n",x,y,rnd_mode); +mpfr_print_raw(zz); putchar('\n'); + exit(1); + } + mpfr_clear(xx); mpfr_clear(yy); mpfr_clear(zz); + mpf_clear(xxx); mpf_clear(yyy); mpf_clear(zzz); +} + +main(argc,argv) int argc; char *argv[]; +{ + double x,y,z; int i,prec,rnd_mode; + + prec = (argc<2) ? 53 : atoi(argv[1]); + rnd_mode = (argc<3) ? -1 : atoi(argv[2]); + check(2.71331408349172961467e-08, -6.72658901114033715233e-165, + GMP_RNDZ, 53, 53, 53, 0.0); + x=0.31869277231188065; y=0.88642843322303122; + check(x, y, GMP_RNDZ, 53, 53, 53, 0.0); + x=8.47622108205396074254e-01; y=3.24039313247872939883e-01; + check(x, y, GMP_RNDU, 28, 45, 1, 0.5); + x=2.63978122803639081440e-01; + y=5736014.0/8388608.0; /* 6.83786096444222835089e-01; */ + check(x, y, GMP_RNDN, 34, 23, 31, 0.180504585267044603); + x=9.84891017624509146344e-01; /* rounded to 1.0 with prec=6 */ + x=1.0; + y=1.18351709358762491320e-01; + check(x, y, GMP_RNDU, 6, 41, 36, 0.1183517093595583); + /* the following checks that rounding to nearest sets the last + bit to zero in case of equal distance */ + check(67108865.0, 134217729.0, GMP_RNDN, 53, 53, 53, 0.0); + x=1.37399642157394197284e-01; y=2.28877275604219221350e-01; + check(x, y, GMP_RNDN, 49, 15, 32, 0.0314472340833162888); + x=4.03160720978664954828e-01; y=5.85483042917246621073e-01; + check(x, y, GMP_RNDZ, 51, 22, 32, 0.2360436821472831); + x=3.90798504668055102229e-14; y=9.85394674650308388664e-04; + check(x, y, GMP_RNDN, 46, 22, 12, 0.385027296503914762e-16); + x=4.58687081072827851358e-01; y=2.20543551472118792844e-01; + check(x, y, GMP_RNDN, 49, 3, 1, 0.125); + for (i=0;i<1000000;) { + x = drand(); + y = drand(); + z = x*y; if (z<0) z=-z; + if (z<1e+308 && z>1e-308) /* don't test overflow/underflow for now */ + { i++; + check(x, y, (rnd_mode==-1) ? lrand48()%4 : rnd_mode, + prec, prec, prec, 0.0); + } + } +} + diff --git a/tests/tmul_2exp b/tests/tmul_2exp Binary files differnew file mode 100755 index 000000000..849710aa3 --- /dev/null +++ b/tests/tmul_2exp diff --git a/tests/tmul_2exp.c b/tests/tmul_2exp.c new file mode 100644 index 000000000..3f6055727 --- /dev/null +++ b/tests/tmul_2exp.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include "gmp.h" +#include "mpfr.h" + +/* checks that x*y gives the same results in double + and with mpfr with 53 bits of precision */ + +void main(argc,argv) int argc; char *argv[]; +{ + double x, z; mpfr_t w; + + mpfr_init2(w, 53); + + srand48(time(NULL)); + x = drand48(); + mpfr_set_d(w, x, 53, 1, 0); + mpfr_mul_2exp(w, w, 10, GMP_RNDZ); + if (x != (z = mpfr_get_d(w)/1024)) + { + fprintf(stderr, "%lf != %lf\n", x, z); + }; +} + diff --git a/tests/tmul_ui b/tests/tmul_ui Binary files differnew file mode 100755 index 000000000..473d7e0b1 --- /dev/null +++ b/tests/tmul_ui diff --git a/tests/tmul_ui.c b/tests/tmul_ui.c new file mode 100644 index 000000000..e173f4e87 --- /dev/null +++ b/tests/tmul_ui.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" +#include "time.h" + +int +main(int argc, char **argv) +{ + mpfr_t x; + + mpfr_init2(x, 53); + mpfr_set_d(x, 1.0/3.0, GMP_RNDZ); + mpfr_print_raw(x); putchar('\n'); + + mpfr_mul_ui(x, x, 3, GMP_RNDU); + mpfr_print_raw(x); putchar('\n'); + + printf("%f\n", mpfr_get_d(x)); + + mpfr_clear(x); + return(0); +} diff --git a/tests/tround b/tests/tround Binary files differnew file mode 100755 index 000000000..10fd312dd --- /dev/null +++ b/tests/tround diff --git a/tests/tround.c b/tests/tround.c new file mode 100644 index 000000000..1a743781e --- /dev/null +++ b/tests/tround.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" + +main() +{ + mpfr_t x; + + /* checks that rounds to nearest sets the last + bit to zero in case of equal distance */ + mpfr_init2(x, 2); + mpfr_set_d(x, 5.0, 0); + if (mpfr_get_d(x) != 4.0) { printf("Error in tround: got %1.1f instead of 4.0\n",mpfr_get_d(x)); } + + mpfr_set_d(x, 0.00098539467465030839, 0); + + mpfr_set_d(x, 9.84891017624509146344e-01, GMP_RNDU); + if (mpfr_get_d(x) != 1.0) { printf("Error in tround: got %f instead of 1.0\n",mpfr_get_d(x)); exit(1); } + + mpfr_clear(x); +} diff --git a/tests/tset_d b/tests/tset_d Binary files differnew file mode 100755 index 000000000..f639320b0 --- /dev/null +++ b/tests/tset_d diff --git a/tests/tset_d.c b/tests/tset_d.c new file mode 100644 index 000000000..54478c3f4 --- /dev/null +++ b/tests/tset_d.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" +#include <time.h> +#include <math.h> + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + return d; +} + +int +main(int argc, char **argv) +{ + mpfr_t x,y,z; unsigned long k,n; double d; + + mpfr_init2(z, 32); + mpfr_set_d(z, 1.0, 0); + if (mpfr_get_d(z) != 1.0) { + mpfr_print_raw(z); putchar('\n'); + printf("Error: 1.0 != 1.0\n"); exit(1); + } + mpfr_init2(x, 53); mpfr_init2(y, 53); + mpfr_set_d(x, d=-1.08007920352320089721e+150, 0); + if (mpfr_get_d(x) != d) { + mpfr_print_raw(x); putchar('\n'); + printf("Error: get_d o set_d <> identity for d = %1.20e %1.20e\n",d, + mpfr_get_d(x)); exit(1); + } + srand48(time(NULL)); + mpfr_set_d(x, 8.06294740693074521573e-310, 0); + d = -6.72658901114033715233e-165; + mpfr_set_d(x, d, 0); + if (d != mpfr_get_d(x)) { + mpfr_print_raw(x); putchar('\n'); + printf("Error: get_d o set_d <> identity for d = %1.20e %1.20e\n",d, + mpfr_get_d(x)); exit(1); + } + n = (argc==1) ? 1000000 : atoi(argv[1]); + for (k = 1; k <= n; k++) + { + do { d = drand(); } while (isnan(d)); /* does not yet work for NaN */ + mpfr_set_d(x, d, 0); + if (d != mpfr_get_d(x)) + { + fprintf(stderr, + "Mismatch on : %1.18g != %1.18g\n", d, mpfr_get_d(x)); + mpfr_print_raw(x); putchar('\n'); + } + } + + mpfr_clear(x); + return 0; +} diff --git a/tests/tset_f b/tests/tset_f Binary files differnew file mode 100755 index 000000000..1c0d8e334 --- /dev/null +++ b/tests/tset_f diff --git a/tests/tset_f.c b/tests/tset_f.c new file mode 100644 index 000000000..8a8dc0a50 --- /dev/null +++ b/tests/tset_f.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" +#include "time.h" + +int +main() +{ + mpfr_t x; mpf_t y; mpf_t z; unsigned long k, pr; + + mpfr_init2(x, 100); + mpf_init(y); + + srandom(time(NULL)); + mpf_random2(y, 10, 0); + mpfr_set_f(x, y, 53, rand() & 3); + + mpf_out_str(stdout, 10, 0, y); + printf("\n%1.21g\n", mpfr_get_d(x)); + + mpf_clear(y); mpfr_clear(x); + + for (k = 1; k <= 100000; k++) + { + pr = 1 + (rand()&255); + mpf_init2(z, pr); + mpf_random2(z, z->_mp_prec, 0); + mpfr_init2(x, pr); + mpfr_set_f(x, z, pr, 0); + } + return(0); +} diff --git a/tests/tset_i b/tests/tset_i Binary files differnew file mode 100755 index 000000000..4863b90fa --- /dev/null +++ b/tests/tset_i diff --git a/tests/tset_si.c b/tests/tset_si.c new file mode 100644 index 000000000..f3d3bcef9 --- /dev/null +++ b/tests/tset_si.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" +#include "time.h" + +int +main(int argc, char **argv) +{ + mpfr_t x; long k, z, d; unsigned long zl, dl; + + mpfr_init2(x, 100); + + srandom(time(NULL)); + + for (k = 1; k <= atoi(argv[1]); k++) + { + z = random() - (1 << 30); + mpfr_set_si(x, z, GMP_RNDZ); + d = (int)mpfr_get_d(x); + if (d != z) + printf("Expected %ld got %ld\n", z, d); + + } + + for (k = 1; k <= atoi(argv[1]); k++) + { + zl = random(); + mpfr_set_ui(x, zl, GMP_RNDZ); + dl = (unsigned int) mpfr_get_d(x); + if (dl != zl) + printf("Expected %lu got %lu\n", zl, dl); + } + + mpfr_clear(x); + return(0); +} diff --git a/tests/tset_str b/tests/tset_str Binary files differnew file mode 100755 index 000000000..7d81d195d --- /dev/null +++ b/tests/tset_str diff --git a/tests/tset_str.c b/tests/tset_str.c new file mode 100644 index 000000000..e37544fe0 --- /dev/null +++ b/tests/tset_str.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "mpfr.h" +#include <time.h> + +int +main(int argc, char **argv) +{ + mpfr_t x; unsigned long k, bd, nc; char *str, *str2; + + srandom(time(NULL)); + + if (argc > 1) { nc = atoi(argv[1]); } else { nc = 53; } + if (nc < 24) { nc = 24; } + + bd = random()&8; + + str2 = str = (char *) malloc (nc*sizeof(char)); + + if (bd) + { + for(k = 1; k <= bd; k++) + { *(str2++) = (random() & 1) + '0'; } + } + else { *(str2++) = '0'; } + + *(str2++) = '.'; + + for(k = 1; k < nc - 17 - bd; k++) + { + *(str2++) = '0' + (random() & 1); + } + + *(str2++) = 'e'; + sprintf(str2, "%d", random() - (1 << 30)); + + printf("%s\n", str); + mpfr_init2(x, nc + 10); + mpfr_set_str_raw(x, str); + mpfr_print_raw(x); printf("\n"); + + mpfr_clear(x); free(str); + return 0; +} diff --git a/tests/tsqrt b/tests/tsqrt Binary files differnew file mode 100755 index 000000000..8c802dce5 --- /dev/null +++ b/tests/tsqrt diff --git a/tests/tsqrt.c b/tests/tsqrt.c new file mode 100644 index 000000000..c3af019c3 --- /dev/null +++ b/tests/tsqrt.c @@ -0,0 +1,63 @@ +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "mpfr.h" + +double drand() +{ + double d; long int *i; + + i = (long int*) &d; + i[0] = lrand48(); + i[1] = lrand48(); + if (lrand48()%2) d=-d; /* generates negative numbers */ + return d; +} + +/* returns the number of ulp's between a and b */ +int ulp(a,b) double a,b; +{ + double eps=1.1102230246251565404e-16; /* 2^(-53) */ + b = (a-b)/a; if (b<0) b = -b; + return (int) floor(b/eps); +} + +void check(a, rnd_mode) double a; unsigned char rnd_mode; +{ + mpfr_t q, n; double Q,Q2; + +#ifdef DEBUG + printf("a=%1.20e rnd_mode=%d\n",a,rnd_mode); +#endif + mpfr_init2(q, 53); mpfr_init2(n, 53); + mpfr_set_d(n, a, 53, 0, rnd_mode); + mpfr_set_machine_rnd_mode(rnd_mode); + mpfr_sqrt(q, n, rnd_mode); + Q = sqrt(a); + Q2 = mpfr_get_d(q); +#ifdef DEBUG + printf("expected sqrt is %1.20e, got %1.20e (%d ulp)\n",Q,Q2, + ulp(Q2,Q)); + mpfr_print_raw(q); putchar('\n'); +#endif + if (Q!=Q2 && (!isnan(Q) || !isnan(Q2))) { + printf("mpfr_sqrt failed for a=%1.20e, rnd_mode=%d\n",a,rnd_mode); + printf("expected sqrt is %1.20e, got %1.20e (%d ulp)\n",Q,Q2, + ulp(Q2,Q)); + exit(1); + } + mpfr_clear(q); mpfr_clear(n); +} + +void main() +{ + int i; double a; + + check(9.89438396044940256501e-134, 2); + for (i=0;i<1000000;i++) { + do { a = drand(); } while (isnan(a)); + check(a, rand() % 4); + } +} |