void
check (mpfi_ptr i, mpq_srcptr a, mpq_srcptr b,
       mpfr_srcptr expected_left, mpfr_srcptr expected_right,
       int expected_inex)
{
  int inex;

  inex = mpfi_interv_q (i, a, b);
  if (inex != expected_inex) {
    printf ("Error: mpfi_interv_q (i, a, b) returns %d instead of %d\n",
            inex, expected_inex);
    printf ("precision(i) = %lu\na =", mpfi_get_prec (i));
    mpq_out_str (stdout, 10, a);
    printf ("\nb = ");
    mpq_out_str (stdout, 10, b);
    printf ("\n");
    exit (1);
  }
  if (!same_mpfr_value (&(i->left), expected_left)
      || !same_mpfr_value (&(i->right), expected_right)) {
    printf ("Error: mpfi_interv_q (i, a, b) failed.\n");
    printf ("\na = ");
    mpq_out_str (stdout, 10, a);
    printf ("\nb = ");
    mpq_out_str (stdout, 10, b);
    printf ("\ngot    i = ");
    mpfi_out_str (stdout, 10, 0, i);
    printf ("\nexpected = [");
    mpfr_out_str (stdout, 10, 0, expected_left, MPFI_RNDD);
    printf (", ");
    mpfr_out_str (stdout, 10, 0, expected_right, MPFI_RNDU);
    printf ("]\n");
    exit (1);
  }
}
Esempio n. 2
0
static void
dumpSkip(mpq_t *t, const symbol_t *scan)
{
    static int          initialized = 0;
    static mpq_t        dt;
    static int          de;
    static int          nu;
    static mpz_t        zde;
    static mpz_t        znu;
    int rnd;

    if (! initialized) {
        mpq_init(dt);
        mpz_init(zde);
        mpz_init(znu);
        initialized = 1;
    }

    if (mpq_equal(*t, scan->start)) {
        return;
    }

    while (dump_tuplet_current != NO_ID) {
        /* stop */
        fprintf(lily_out, " }");
        tuplet_pop(&dump_tuplet_current);
    }

    if (mpq_cmp(*t, scan->start) > 0) {
        fprintf(stderr, "Uh oh -- start time ");
        mpq_out_str(stderr, 10, scan->start);
        fprintf(stderr, " is too low, should be ");
        mpq_out_str(stderr, 10, *t);
        fprintf(stderr, " -- is my voice analysis correct?\n");
        return;
    }
    mpq_sub(dt, scan->start, *t);
    mpq_get_num(znu, dt);
    mpq_get_den(zde, dt);
    nu = mpz_get_ui(znu);
    de = mpz_get_ui(zde);
    rnd = nu / de;
    if (rnd != 0) {
        fprintf(lily_out, " s1*%d", rnd);
        nu -= rnd * de;
    }
    if (! is_two_pow(de)) {
        fprintf(stderr, "Uh oh -- skip now already a tuplet %d/%d??\n", nu, de);
        fprintf(lily_out, " s1*%d/%d", nu, de);
    } else {
        if (nu != 0) {
            fprintf(lily_out, " s%d*%d", de, nu);
        }
    }
    last_dumped_symbol = &sym_any_skip;
    mpq_set(*t, scan->start);
    VPRINTF(" skip to t = ");
    VPRINT_MPQ(*t);
}
Esempio n. 3
0
//Printing
//@Function:	Display
//@Purpose:		Displays a point as (nX,nY) to std::cout
void Point::Display()
{
	std::cout << "(";
	mpq_out_str(stdout, 10, this->nX);
	std::cout << ",";
	mpq_out_str(stdout, 10, this->nY);
	std::cout << ")";
}
Esempio n. 4
0
void print_rational_number(FILE *OUT, rational_complex_number z)
/***************************************************************\
* USAGE: prints z to OUT                                        *
\***************************************************************/
{
  int base = 10;

  mpq_out_str(OUT, base, z->re);
  if (mpq_sgn(z->im) >= 0)
    fprintf(OUT, "+");
  mpq_out_str(OUT, base, z->im);
  fprintf(OUT, "*I");

  return;
}
Esempio n. 5
0
File: tgmpop.c Progetto: Canar/mpfr
static void
test_specialq (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int N,
               int (*mpfr_func)(mpfr_ptr, mpfr_srcptr, mpq_srcptr, mpfr_rnd_t),
               void (*mpq_func)(mpq_ptr, mpq_srcptr, mpq_srcptr),
               const char *op)
{
  mpfr_t fra, frb, frq;
  mpq_t  q1, q2, qr;
  unsigned int n;
  mpfr_prec_t prec;

  for (prec = p0 ; prec < p1 ; prec++)
    {
      mpfr_inits2 (prec, fra, frb, frq, (mpfr_ptr) 0);
      mpq_init (q1); mpq_init(q2); mpq_init (qr);

      for( n = 0 ; n < N ; n++)
        {
          mpq_set_ui(q1, randlimb(), randlimb() );
          mpq_set_ui(q2, randlimb(), randlimb() );
          mpq_canonicalize (q1);
          mpq_canonicalize (q2);
          mpq_func (qr, q1, q2);
          mpfr_set_q (fra, q1, MPFR_RNDD);
          mpfr_func (fra, fra, q2, MPFR_RNDD);
          mpfr_set_q (frb, q1, MPFR_RNDU);
          mpfr_func (frb, frb, q2, MPFR_RNDU);
          mpfr_set_q (frq, qr, MPFR_RNDN);
          /* We should have fra <= qr <= frb */
          if ( (mpfr_cmp(fra, frq) > 0) || (mpfr_cmp (frq, frb) > 0))
            {
              printf("Range error for prec=%lu and %s",
                     (unsigned long) prec, op);
              printf ("\nq1="); mpq_out_str(stdout, 2, q1);
              printf ("\nq2="); mpq_out_str(stdout, 2, q2);
              printf ("\nfr_dn="); mpfr_print_binary (fra);
              printf ("\nfr_q ="); mpfr_print_binary (frq);
              printf ("\nfr_up="); mpfr_print_binary (frb);
              putchar('\n');
              exit (1);
            }
        }

      mpq_clear (q1); mpq_clear (q2); mpq_clear (qr);
      mpfr_clears (fra, frb, frq, (mpfr_ptr) 0);
    }
}
Esempio n. 6
0
void print_rational_vector_coordinate(FILE *OUT, rational_complex_vector z)
/***************************************************************\
* USAGE: prints z                                               *
\***************************************************************/
{
  int i, size = z->size, base = 10;

  if (size > 0)
  { // print each coordinate
    for (i = 0; i < size; i++)
    {
      mpq_out_str(OUT, base, z->coord[i]->re);
      fprintf(OUT, " ");
      mpq_out_str(OUT, base, z->coord[i]->im);
      fprintf(OUT, "\n");
    }
  } 

  return;
}
Esempio n. 7
0
void mpq_mat_print(mpq_mat_t mat){

   printf("%ld %ld  ", mat->r, mat->c);

   long i;
   for (i = 0; i < mat->r*mat->c; i++){
      mpq_out_str(NULL, 10, mat->entries[i]); printf(" ");
   }

   return;
}
ats_void_type
atslib_mpq_out_str_exn (
  ats_ptr_type file
, ats_int_type base
, const ats_mpq_ptr_type x
) {
  size_t n = mpq_out_str((FILE*)file, base, (mpq_ptr)x) ;
  if (n == 0) {
    ats_exit_errmsg (1, "exit(ATS): [mpq_out_str] failed.\n") ;
  } // end of [if]
  return ;
} // end of [atslib_mpq_out_str_exn]
Esempio n. 9
0
void gmp_print_mpq(FILE* fp, const mpq_t qval)
{
   mpf_t fval;
   
   mpf_init(fval);
   mpf_set_q(fval, qval);
   mpf_out_str(fp, 10, 32, fval);
   fprintf(fp, " = ");
   mpq_out_str(fp, 10, qval);
   fputc('\n', fp);
   mpf_clear(fval);
}
Esempio n. 10
0
static symbol_p
rest_create(niffRest *p)
{
    symbol_p    s = symbol_create(t_current);
    note_p      n = &s->symbol.note;

    mpq_init(n->duration);
    rat2mpq(n->duration, &p->duration);

    stem_p stem = stem_current;
    if (mpq_cmp(n->duration, time_sig_current->duration) >= 0 &&
            ! mpq_equal(t_current, t_measure_start)) {
        fprintf(stderr, "Meet SharpEye rest(measure) bug. Replace start time ");
        mpq_out_str(stderr, 10, t_current);
        fprintf(stderr, " with measure start time ");
        mpq_out_str(stderr, 10, t_measure_start);
        fprintf(stderr, "\n");
        mpq_set(s->start, t_measure_start);
		stem = NULL;	// don't share a stem when the time is incorrect
    }

    s->type = SYM_NOTE;
    n->value = p->staffStep;
    n->flags |= FLAG_REST;

    if (stem == NULL) {
#if VERBOSE
        VPRINTF("\n ****** Get a Rest chunk without stem chunk??");
#else
        fprintf(stderr, "Warning: ****** Get a Rest chunk without stem chunk??\n");
#endif
        stem = &stem_create()->symbol.stem;
    }
    n->tie_start = NO_ID;
    n->tie_end   = NO_ID;
    n->stem      = stem;
    n->tuplet    = stem->tuplet;

    return s;
}
Esempio n. 11
0
void mpq_mat_print_pretty(mpq_mat_t mat){

   printf("[\n[");
   long i;
   for (i = 0; i < mat->r*mat->c; i++){
      mpq_out_str(NULL, 10, mat->entries[i]);
      if (((i+1) % mat->c) != 0) printf(" ");
      else if ((i != 0) && ((i+1)!= mat->r * mat->c) ) printf("]\n[");
   }
   printf("]\n]\n");

   return;
}
/* Print "name=value\n" to stdout for an mpq_t value.  */
void
mpq_trace (const char *name, mpq_srcptr q)
{
  mp_trace_start (name);
  if (q == NULL)
    {
      printf ("NULL\n");
      return;
    }

  mpq_out_str (stdout, mp_trace_base, q);
  printf ("\n");
}
Esempio n. 13
0
static void write_val(FILE* fp, LpFormat format, Bool force_sign, const mpq_t val)
{
   switch(format)
   {
   case LP_FORM_LPF :
   case LP_FORM_RLP :
   case LP_FORM_PIP :
      fprintf(fp, force_sign ? "%+.15g" : "%.15g", mpq_get_d(val));
      break;
   case LP_FORM_HUM :
      if (force_sign && (mpq_sgn(val) > 0)) /*lint !e634 Strong type mismatch (type 'Bool') */
         fprintf(fp, "+");

      mpq_out_str(fp, 10, val);
      break;
   default:
      abort();
   }
}
Esempio n. 14
0
File: tgmpop.c Progetto: Canar/mpfr
static void
bug_div_q_20100810 (void)
{
  mpfr_t x;
  mpfr_t y;
  mpq_t q;
  int inexact;

  mpfr_init (x);
  mpfr_init (y);
  mpq_init (q);

  /* mpfr_div_q: the inexact value must be set in case of overflow */
  mpq_set_ui (q, 3, 4096);
  mpfr_set_inf (x, +1);
  mpfr_nextbelow (x);
  inexact = mpfr_div_q (y, x, q, MPFR_RNDU);

  if (inexact <= 0)
    {
      printf ("Overflow error in mpfr_div_q. ");
      printf ("Wrong inexact flag: got %d, should be positive.\n", inexact);

      exit (1);
    }
  if (!mpfr_inf_p (y))
    {
      printf ("Overflow error in mpfr_div_q (y, x, q, MPFR_RNDD). ");
      printf ("\nx = ");
      mpfr_out_str (stdout, 10, 0, x, MPFR_RNDD);
      printf ("\nq = ");
      mpq_out_str (stdout, 10, q);
      printf ("\ny = ");
      mpfr_out_str (stdout, 10, 0, y, MPFR_RNDD);
      printf (" (should be +infinity)\n");

      exit (1);
    }

  mpq_clear (q);
  mpfr_clear (y);
  mpfr_clear (x);
}
Esempio n. 15
0
/*!
 * Test most of the functions that can be applied to the \c chv_t.
 *
 * Illustrates how the concept is used.
 */
int
test_chv(int little_endian_flag)
{
    chv_t chv;

    int8_t   c   = +123;
    uint8_t  uc  = -123;

    int16_t  s   = +12345;
    uint16_t us  = -12345;

    int      i   = +1234567890;
    uint     ui  = -1234567890;

    int64_t  ll  = 0x0011223344556677LL;
    uint64_t ull = 0x7766554433221100ULL;

    size_t   sz  = UINT32_MAX;

    float    f   = 1/3.0f;
    double   d   = 1/3.0;

    const char *cstr  = "pelle";
    char       *cstr1 = NULL;

#ifdef HAVE_GMP_H
    mpz_t mpz; mpz_init_set_str(mpz, "012345657890123456578901234565789012345657890123456578901234565789", 10);
    mpq_t mpq; mpq_init(mpq); mpq_set_str(mpq, "3/5", 10);
#endif

    printf("%d %u  %d %u  %d %u  %lld %llu  %zu  %g %g ",
           c, uc, s, us, i, ui, ll, ull, sz, f, d);
    printf("%s ", cstr);
    mpz_out_str (stdout, 10, mpz); printf(" ");
    mpq_out_str (stdout, 10, mpq); printf(" ");
    endline();

    chv_init(&chv);

    if (little_endian_flag) {
        chv_enc_s8(&chv, &c); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u8(&chv, &uc); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_s16le(&chv, &s); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u16le(&chv, &us); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_s32le(&chv, &i); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u32le(&chv, &ui); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_s64le(&chv, &ll); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u64le(&chv, &ull); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_sizele(&chv, &sz); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_f32le(&chv, &f); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_f64le(&chv, &d); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_cstr_lengthU32le(&chv, cstr); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
    } else {
        chv_enc_s8(&chv, &c); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u8(&chv, &uc); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_s16be(&chv, &s); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u16be(&chv, &us); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_s32be(&chv, &i); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u32be(&chv, &ui); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_s64be(&chv, &ll); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_u64be(&chv, &ull); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_sizebe(&chv, &sz); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_f32be(&chv, &f); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_enc_f64be(&chv, &d); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_enc_cstr_lengthU32be(&chv, cstr); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
    }
#ifdef HAVE_GMP_H
    chv_enc_mpz(&chv, mpz, little_endian_flag);
    chv_enc_mpq(&chv, mpq, little_endian_flag);
#endif

    chv_save(&chv, "chv_test.bin"); // save to file

    // reset variables
    c = 0; uc = 0;
    s = 0; us = 0;
    i = 0; ui = 0;
    ll = 0; ull = 0;
    f = 0; d = 0;
    cstr = NULL;
    cstr1 = NULL;
    mpz_clear(mpz);
    mpq_clear(mpq);

    // allocate variables
    mpz_init(mpz);
    mpq_init(mpq);

    chv_load(&chv, "chv_test.bin"); // load from file

    if (little_endian_flag) {
        chv_dec_s8(&chv, &c); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u8(&chv, &uc); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_s16le(&chv, &s); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u16le(&chv, &us); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_s32le(&chv, &i); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u32le(&chv, &ui); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_s64le(&chv, &ll); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u64le(&chv, &ull); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_sizele(&chv, &sz); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_f32le(&chv, &f); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_f64le(&chv, &d); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_cstr_lengthU32le(&chv, &cstr1); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
    } else {
        chv_dec_s8(&chv, &c); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u8(&chv, &uc); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_s16be(&chv, &s); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u16be(&chv, &us); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_s32be(&chv, &i); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u32be(&chv, &ui); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_s64be(&chv, &ll); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_u64be(&chv, &ull); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_sizebe(&chv, &sz); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_f32be(&chv, &f); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
        chv_dec_f64be(&chv, &d); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");

        chv_dec_cstr_lengthU32be(&chv, &cstr1); chv_fprint_x_quoted_chars(stdout, &chv); printf("\n");
    }
#ifdef HAVE_GMP_H
    chv_dec_mpz(&chv, mpz, little_endian_flag);
    chv_dec_mpq(&chv, mpq, little_endian_flag);
#endif

    printf("%d %u  %d %u  %d %u  %lld %llu  %zu  %g %g ",
           c, uc, s, us, i, ui, ll, ull, sz, f, d);
    printf("%s ", cstr1);
    mpz_out_str(stdout, 10, mpz); printf(" ");
    mpq_out_str(stdout, 10, mpq); printf(" ");
    endline();

    free(cstr1);
    mpz_clear(mpz);
    mpq_clear(mpq);

    chv_clear(&chv);

    printf("\n");

    return 0;
}
Esempio n. 16
0
File: tgmpop.c Progetto: Canar/mpfr
static void
test_genericq (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int N,
               int (*func)(mpfr_ptr, mpfr_srcptr, mpq_srcptr, mpfr_rnd_t),
               const char *op)
{
  mpfr_prec_t prec;
  mpfr_t arg1, dst_big, dst_small, tmp;
  mpq_t  arg2;
  mpfr_rnd_t rnd;
  int inexact, compare, compare2;
  unsigned int n;

  mpfr_inits (arg1, dst_big, dst_small, tmp, (mpfr_ptr) 0);
  mpq_init (arg2);

  for (prec = p0; prec <= p1; prec++)
    {
      mpfr_set_prec (arg1, prec);
      mpfr_set_prec (tmp, prec);
      mpfr_set_prec (dst_small, prec);

      for (n=0; n<N; n++)
        {
          mpfr_urandomb (arg1, RANDS);
          mpq_set_ui (arg2, randlimb (), randlimb() );
          mpq_canonicalize (arg2);
          rnd = RND_RAND ();
          mpfr_set_prec (dst_big, prec+10);
          compare = func(dst_big, arg1, arg2, rnd);
          if (mpfr_can_round (dst_big, prec+10, rnd, rnd, prec))
            {
              mpfr_set (tmp, dst_big, rnd);
              inexact = func(dst_small, arg1, arg2, rnd);
              if (mpfr_cmp (tmp, dst_small))
                {
                  printf ("Results differ for prec=%u rnd_mode=%s and %s_q:\n"
                          "arg1=",
                          (unsigned) prec, mpfr_print_rnd_mode (rnd), op);
                  mpfr_print_binary (arg1);
                  printf("\narg2=");
                  mpq_out_str(stdout, 2, arg2);
                  printf ("\ngot      ");
                  mpfr_print_binary (dst_small);
                  printf ("\nexpected ");
                  mpfr_print_binary (tmp);
                  printf ("\napprox  ");
                  mpfr_print_binary (dst_big);
                  putchar('\n');
                  exit (1);
                }
              compare2 = mpfr_cmp (tmp, dst_big);
              /* if rounding to nearest, cannot know the sign of t - f(x)
                 because of composed rounding: y = o(f(x)) and t = o(y) */
              if (compare * compare2 >= 0)
                compare = compare + compare2;
              else
                compare = inexact; /* cannot determine sign(t-f(x)) */
              if (((inexact == 0) && (compare != 0)) ||
                  ((inexact > 0) && (compare <= 0)) ||
                  ((inexact < 0) && (compare >= 0)))
                {
                  printf ("Wrong inexact flag for rnd=%s and %s_q:\n"
                          "expected %d, got %d",
                          mpfr_print_rnd_mode (rnd), op, compare, inexact);
                  printf ("\narg1="); mpfr_print_binary (arg1);
                  printf ("\narg2="); mpq_out_str(stdout, 2, arg2);
                  printf ("\ndstl="); mpfr_print_binary (dst_big);
                  printf ("\ndsts="); mpfr_print_binary (dst_small);
                  printf ("\ntmp ="); mpfr_print_binary (tmp);
                  putchar('\n');
                  exit (1);
                }
            }
        }
    }

  mpq_clear (arg2);
  mpfr_clears (arg1, dst_big, dst_small, tmp, (mpfr_ptr) 0);
}
Esempio n. 17
0
File: io.c Progetto: AllardJ/Tomato
int
main (int argc, char **argv)
{
  mpq_t  op1, op2;
  mp_size_t size;
  int i;
  int reps = 10000;
  FILE *fp;
  int base;
  gmp_randstate_ptr rands;
  mpz_t bs;
  unsigned long bsi, size_range;
  size_t nread;

  tests_start ();
  rands = RANDS;

  mpz_init (bs);

  if (argc == 2)
    reps = atoi (argv[1]);

  mpq_init (op1);
  mpq_init (op2);

  fp = fopen (FILENAME, "w+");

  for (i = 0; i < reps; i++)
    {
      mpz_urandomb (bs, rands, 32);
      size_range = mpz_get_ui (bs) % 10 + 2;

      mpz_urandomb (bs, rands, size_range);
      size = mpz_get_ui (bs);
      mpz_errandomb (mpq_numref(op1), rands, 512L);
      mpz_errandomb_nonzero (mpq_denref(op1), rands, 512L);
      mpq_canonicalize (op1);

      mpz_urandomb (bs, rands, 1);
      bsi = mpz_get_ui (bs);
      if ((bsi & 1) != 0)
	mpq_neg (op1, op1);

      mpz_urandomb (bs, rands, 16);
      bsi = mpz_get_ui (bs);
      base = bsi % 36 + 1;
      if (base == 1)
	base = 0;

      rewind (fp);
      if (mpq_out_str (fp, base, op1) == 0
	  || putc (' ', fp) == EOF
	  || fflush (fp) != 0)
	{
	  printf ("mpq_out_str write error\n");
	  abort ();
	}

      rewind (fp);
      nread = mpq_inp_str (op2, fp, base);
      if (nread == 0)
	{
	  if (ferror (fp))
	    printf ("mpq_inp_str stream read error\n");
	  else
	    printf ("mpq_inp_str data conversion error\n");
	  abort ();
	}

      if (nread != ftell(fp))
	{
	  printf ("mpq_inp_str nread doesn't match ftell\n");
	  printf ("  nread  %lu\n", (unsigned long) nread);
	  printf ("  ftell  %ld\n", ftell(fp));
	  abort ();
	}

      if (mpq_cmp (op1, op2))
	{
	  printf ("ERROR\n");
	  printf ("op1  = "); debug_mp (op1, -16);
	  printf ("op2  = "); debug_mp (op2, -16);
	  printf ("base = %d\n", base);
	  abort ();
	}
    }

  fclose (fp);

  unlink (FILENAME);

  mpz_clear (bs);
  mpq_clear (op1);
  mpq_clear (op2);

  tests_end ();
  exit (0);
}
Esempio n. 18
0
File: io.c Progetto: AllardJ/Tomato
void
debug_mp (mpq_t x, int base)
{
  mpq_out_str (stdout, base, x); fputc ('\n', stdout);
}
Esempio n. 19
0
int
main (int argc, char **argv)
{
  static const struct {
    struct pair_t  left;
    unsigned long  n;
    struct pair_t  right;

  } data[] = {
    { {"0","1"}, 0, {"0","1"} },
    { {"0","1"}, 1, {"0","1"} },
    { {"0","1"}, 2, {"0","1"} },

    { {"1","1"}, 0, {"1","1"} },
    { {"1","1"}, 1, {"2","1"} },
    { {"1","1"}, 2, {"4","1"} },
    { {"1","1"}, 3, {"8","1"} },

    { {"1","1"}, 31, {"0x80000000","1"} },
    { {"1","1"}, 32, {"0x100000000","1"} },
    { {"1","1"}, 33, {"0x200000000","1"} },
    { {"1","1"}, 63, {"0x8000000000000000","1"} },
    { {"1","1"}, 64, {"0x10000000000000000","1"} },
    { {"1","1"}, 65, {"0x20000000000000000","1"} },
    { {"1","1"}, 95, {"0x800000000000000000000000","1"} },
    { {"1","1"}, 96, {"0x1000000000000000000000000","1"} },
    { {"1","1"}, 97, {"0x2000000000000000000000000","1"} },
    { {"1","1"}, 127, {"0x80000000000000000000000000000000","1"} },
    { {"1","1"}, 128, {"0x100000000000000000000000000000000","1"} },
    { {"1","1"}, 129, {"0x200000000000000000000000000000000","1"} },

    { {"1","2"}, 31, {"0x40000000","1"} },
    { {"1","2"}, 32, {"0x80000000","1"} },
    { {"1","2"}, 33, {"0x100000000","1"} },
    { {"1","2"}, 63, {"0x4000000000000000","1"} },
    { {"1","2"}, 64, {"0x8000000000000000","1"} },
    { {"1","2"}, 65, {"0x10000000000000000","1"} },
    { {"1","2"}, 95, {"0x400000000000000000000000","1"} },
    { {"1","2"}, 96, {"0x800000000000000000000000","1"} },
    { {"1","2"}, 97, {"0x1000000000000000000000000","1"} },
    { {"1","2"}, 127, {"0x40000000000000000000000000000000","1"} },
    { {"1","2"}, 128, {"0x80000000000000000000000000000000","1"} },
    { {"1","2"}, 129, {"0x100000000000000000000000000000000","1"} },

    { {"1","0x80000000"}, 30, {"1","2"} },
    { {"1","0x80000000"}, 31, {"1","1"} },
    { {"1","0x80000000"}, 32, {"2","1"} },
    { {"1","0x80000000"}, 33, {"4","1"} },
    { {"1","0x80000000"}, 62, {"0x80000000","1"} },
    { {"1","0x80000000"}, 63, {"0x100000000","1"} },
    { {"1","0x80000000"}, 64, {"0x200000000","1"} },
    { {"1","0x80000000"}, 94, {"0x8000000000000000","1"} },
    { {"1","0x80000000"}, 95, {"0x10000000000000000","1"} },
    { {"1","0x80000000"}, 96, {"0x20000000000000000","1"} },
    { {"1","0x80000000"}, 126, {"0x800000000000000000000000","1"} },
    { {"1","0x80000000"}, 127, {"0x1000000000000000000000000","1"} },
    { {"1","0x80000000"}, 128, {"0x2000000000000000000000000","1"} },

    { {"1","0x100000000"}, 1, {"1","0x80000000"} },
    { {"1","0x100000000"}, 2, {"1","0x40000000"} },
    { {"1","0x100000000"}, 3, {"1","0x20000000"} },

    { {"1","0x10000000000000000"}, 1, {"1","0x8000000000000000"} },
    { {"1","0x10000000000000000"}, 2, {"1","0x4000000000000000"} },
    { {"1","0x10000000000000000"}, 3, {"1","0x2000000000000000"} },
  };

  void (*fun) (mpq_ptr, mpq_srcptr, unsigned long);
  const struct pair_t  *p_start, *p_want;
  const char  *name;
  mpq_t    sep, got, want;
  mpq_ptr  q;
  int      i, muldiv, sign, overlap;

  tests_start ();

  mpq_init (sep);
  mpq_init (got);
  mpq_init (want);

  for (i = 0; i < numberof (data); i++)
    {
      for (muldiv = 0; muldiv < 2; muldiv++)
        {
          if (muldiv == 0)
            {
              fun = mpq_mul_2exp;
              name = "mpq_mul_2exp";
              p_start = &data[i].left;
              p_want = &data[i].right;
            }
          else
            {
              fun = mpq_div_2exp;
              name = "mpq_div_2exp";
              p_start = &data[i].right;
              p_want = &data[i].left;
            }

          for (sign = 0; sign <= 1; sign++)
            {
              mpz_set_str_or_abort (mpq_numref(want), p_want->num, 0);
              mpz_set_str_or_abort (mpq_denref(want), p_want->den, 0);
              if (sign)
                mpq_neg (want, want);

              for (overlap = 0; overlap <= 1; overlap++)
                {
                  q = overlap ? got : sep;

                  /* initial garbage in "got" */
                  mpq_set_ui (got, 123L, 456L);

                  mpz_set_str_or_abort (mpq_numref(q), p_start->num, 0);
                  mpz_set_str_or_abort (mpq_denref(q), p_start->den, 0);
                  if (sign)
                    mpq_neg (q, q);

                  (*fun) (got, q, data[i].n);
                  MPQ_CHECK_FORMAT (got);

                  if (! mpq_equal (got, want))
                    {
                      printf ("%s wrong at data[%d], sign %d, overlap %d\n",
                              name, i, sign, overlap);
                      printf ("   num \"%s\"\n", p_start->num);
                      printf ("   den \"%s\"\n", p_start->den);
                      printf ("   n   %lu\n", data[i].n);

                      printf ("   got  ");
                      mpq_out_str (stdout, 16, got);
                      printf (" (hex)\n");

                      printf ("   want ");
                      mpq_out_str (stdout, 16, want);
                      printf (" (hex)\n");

                      abort ();
                    }
                }
            }
        }
    }

  check_random ();

  mpq_clear (sep);
  mpq_clear (got);
  mpq_clear (want);

  tests_end ();
  exit (0);
}
Esempio n. 20
0
int print_coeff(FILE *OUT, rational_complex_number z, int somethingPrinted)
/***************************************************************\
* USAGE: prints z to OUT in user-friendly way                 *
\***************************************************************/
{
  int rV = -2; // 0 if z is zero (nothing printed), 1 if z is one (nothing printed), -1 if z is -1 (nothing printed), -2 otherwise (something printed)
  int base = 10;

  if (mpq_cmp_ui(z->re, 0, 1) == 0)
  { // real part is zero
    if (mpq_cmp_ui(z->im, 0, 1) == 0)
    { // imag part is zero
      rV = 0;
    }
    else
    { // imag part is nonzero
      if (somethingPrinted && mpq_sgn(z->im) >= 0)
        fprintf(OUT, "+");

      if (mpq_cmp_ui(z->im, 1, 1) == 0)
        fprintf(OUT, "I");
      else if (mpq_cmp_si(z->im, -1, 1) == 0)
        fprintf(OUT, "-I");
      else
      {
        mpq_out_str(OUT, base, z->im);
        fprintf(OUT, "*I");
      }
    }
  }
  else
  { // real part is nonzero
    if (mpq_cmp_ui(z->im, 0, 1) == 0)
    { // imag part is zero
      if (mpq_cmp_ui(z->re, 1, 1) == 0)
      { // value is 1
        rV = 1;
      }
      else if (mpq_cmp_si(z->re, -1, 1) == 0)
      { // value is -1
        rV = -1;
      }
      else
      {
        if (somethingPrinted && mpq_sgn(z->re) >= 0)
          fprintf(OUT, "+");
        mpq_out_str(OUT, base, z->re);
      }
    }
    else
    { // imag part is nonzero
      if (somethingPrinted)
        fprintf(OUT, "+");
      fprintf(OUT, "(");
      mpq_out_str(OUT, base, z->re);
      if (mpq_sgn(z->im) >= 0)
        fprintf(OUT, "+");
      mpq_out_str(OUT, base, z->im);
      fprintf(OUT, "*I)");
    }
  }

  return rV;
}
Esempio n. 21
0
int
main (int argc, char *argv[])
{
  int        type = 'z';
  int        base = 0;
  mp_size_t  prec = 64;
  int        obase, opt, i, ret;

  while ((opt = getopt (argc, argv, "b:fp:qrz")) != EOF)
    {
      switch (opt) {
      case 'f':
      case 'q':
      case 'r':
      case 'z':
        type = opt;
        break;
      case 'b':
        base = atoi (optarg);
        break;
      case 'p':
        prec = atoi (optarg);
        break;
      case '?':
      default:
        abort ();
      }
    }

  obase = (base == 0 ? 10 : base);

  if (optind >= argc)
    {
      printf ("Usage: %s [-z] [-q] [-f] [-r] [-p prec] [-b base] expression...\n", argv[0]);
      exit (1);
    }

  switch (type) {
  case 'z':
  default:
    {
      mpz_t  res, foo, bar;

      mpz_init (res);
      mpz_init_set_ui (foo, 55L);
      mpz_init_set_ui (bar, 99L);

      for (i = optind; i < argc; i++)
        TRY (mpz_expr, mpz_out_str (stdout, obase, res), argv[i]);

      mpz_clear (res);
      mpz_clear (foo);
      mpz_clear (bar);
    }
    break;

  case 'q':
    {
      mpq_t  res, foo, bar;

      mpq_init (res);
      mpq_init (foo);
      mpq_init (bar);

      mpq_set_ui (foo, 55L, 1);
      mpq_set_ui (bar, 99L, 1);

      for (i = optind; i < argc; i++)
        TRY (mpq_expr, mpq_out_str (stdout, obase, res), argv[i]);

      mpq_clear (res);
      mpq_clear (foo);
      mpq_clear (bar);
    }
    break;

  case 'f':
    {
      mpf_t  res, foo, bar;

      mpf_init2 (res, prec);
      mpf_init_set_ui (foo, 55L);
      mpf_init_set_ui (bar, 99L);

      for (i = optind; i < argc; i++)
        TRY (mpf_expr, mpf_out_str (stdout, obase, 0, res), argv[i]);

      mpf_clear (res);
      mpf_clear (foo);
      mpf_clear (bar);
    }
    break;

  case 'r':
#if HAVE_MPFR
    {
      mpfr_t  res, foo, bar;

      mpfr_init2 (res, prec);
      mpfr_init_set_ui (foo, 55L, GMP_RNDZ);
      mpfr_init_set_ui (bar, 99L, GMP_RNDZ);

      for (i = optind; i < argc; i++)
        TRY (mpfr_expr,
             mpfr_out_str (stdout, obase, 0, res, GMP_RNDZ),
             argv[i]);

      mpfr_clear (res);
      mpfr_clear (foo);
      mpfr_clear (bar);
    }
#else
    printf ("mpfr not compiled in\n");
    exit (1);
#endif
    break;
  }

  return 0;
}