~ubuntu-branches/ubuntu/intrepid/ecl/intrepid

« back to all changes in this revision

Viewing changes to src/gmp/demos/pexpr.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Van Eynde
  • Date: 2007-04-09 11:51:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070409115151-ql8cr0kalzx1jmla
Tags: 0.9i-20070324-2
Upload to unstable. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Program for computing integer expressions using the GNU Multiple Precision
2
2
   Arithmetic Library.
3
3
 
4
 
Copyright 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
4
Copyright 1997, 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
5
5
 
6
6
This program is free software; you can redistribute it and/or modify it under
7
7
the terms of the GNU General Public License as published by the Free Software
13
13
PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
14
 
15
15
You should have received a copy of the GNU General Public License along with
16
 
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17
 
Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
16
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
17
Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
18
 
19
19
 
20
20
/* This expressions evaluator works by building an expression tree (using a
34
34
   -t        print timing information
35
35
   -html     output html
36
36
   -wml      output wml
37
 
   -nosplit  do not split long lines each 60th digit
 
37
   -split    split long lines each 80th digit
38
38
*/
39
39
 
40
40
/* Define LIMIT_RESOURCE_USAGE if you want to make sure the program doesn't
66
66
 
67
67
 
68
68
#define TIME(t,func)                                                    \
69
 
  do { int __t0, __times, __t, __tmp;                                   \
70
 
    __times = 1;                                                        \
 
69
  do { int __t0, __t, __tmp;                                            \
71
70
    __t0 = cputime ();                                                  \
72
71
    {func;}                                                             \
73
72
    __tmp = cputime () - __t0;                                          \
74
 
    while (__tmp < 100)                                                 \
75
 
      {                                                                 \
76
 
        __times <<= 1;                                                  \
77
 
        __t0 = cputime ();                                              \
78
 
        for (__t = 0; __t < __times; __t++)                             \
79
 
          {func;}                                                       \
80
 
        __tmp = cputime () - __t0;                                      \
81
 
      }                                                                 \
82
 
    (t) = (double) __tmp / __times;                                     \
 
73
    (t) = __tmp;                                                        \
83
74
  } while (0)
84
75
 
85
76
/* GMP version 1.x compatibility.  */
199
190
  sigemptyset (&(act.sa_mask));
200
191
#define SIGNAL(sig)  sigaction (sig, &act, NULL)
201
192
#else
202
 
  struct { int sa_flags } act;
 
193
  struct { int sa_flags; } act;
203
194
#define SIGNAL(sig)  signal (sig, cleanup_and_exit)
204
195
#endif
205
196
  act.sa_flags = 0;
422
413
 
423
414
      if (print_timing)
424
415
        {
425
 
          double t;
 
416
          int t;
426
417
          TIME (t, mpz_eval_expr (r, e));
427
 
          printf ("computation took %.2f ms%s\n", t, newline);
 
418
          printf ("computation took %d ms%s\n", t, newline);
428
419
        }
429
420
      else
430
421
        mpz_eval_expr (r, e);
435
426
          char *tmp, *s;
436
427
 
437
428
          out_len = mpz_sizeinbase (r, base >= 0 ? base : -base) + 2;
 
429
#ifdef LIMIT_RESOURCE_USAGE
 
430
          if (out_len > 100000)
 
431
            {
 
432
              printf ("result is about %ld digits, not printing it%s\n",
 
433
                      (long) out_len - 3, newline);
 
434
              exit (-2);
 
435
            }
 
436
#endif
438
437
          tmp = malloc (out_len);
439
438
 
440
439
          if (print_timing)
441
440
            {
442
 
              double t;
 
441
              int t;
443
442
              printf ("output conversion ");
444
443
              TIME (t, mpz_get_str (tmp, base, r));
445
 
              printf ("took %.2f ms%s\n", t, newline);
 
444
              printf ("took %d ms%s\n", t, newline);
446
445
            }
447
446
          else
448
447
            mpz_get_str (tmp, base, r);
450
449
          out_len = strlen (tmp);
451
450
          if (flag_splitup_output)
452
451
            {
453
 
              for (s = tmp; out_len > 60; s += 60)
 
452
              for (s = tmp; out_len > 80; s += 80)
454
453
                {
455
 
                  fwrite (s, 1, 60, stdout);
 
454
                  fwrite (s, 1, 80, stdout);
456
455
                  printf ("%s\n", newline);
457
 
                  out_len -= 60;
 
456
                  out_len -= 80;
458
457
                }
459
458
 
460
459
              fwrite (s, 1, out_len, stdout);
470
469
      else
471
470
        {
472
471
          printf ("result is approximately %ld digits%s\n",
473
 
                  (long) mpz_sizeinbase (r, 10), newline);
 
472
                  (long) mpz_sizeinbase (r, base >= 0 ? base : -base),
 
473
                  newline);
474
474
        }
475
475
 
476
476
      free_expr (e);
724
724
  {"nextprime", NEXTPRIME, 1},
725
725
  {"binom", BINOM, 2},
726
726
  {"binomial", BINOM, 2},
 
727
  {"fac", FAC, 1},
 
728
  {"fact", FAC, 1},
 
729
  {"factorial", FAC, 1},
727
730
  {"", NOP, 0}
728
731
};
729
732