1. GNU Multiple Precision library

This module provides a wrapper for the Gnu Multiple Precision Arithmetic library known as gmp. The C++ interface is required for this library, you may need to download it. The library and include files must be available in standard places for the scripting harness to find them.

REQUIRES: -lgmpxx -lgmp

Start python section to cpkgs/target/gmpxx.py[1 /1 ]
     1: #line 12 "./lpsrc/flx_gmp.pak"
     2: execfile("config"+os.sep+"config.py")
     3: try:
     4:   cload(globals(),"target_gmpxx")
     5: except:
     6:   HAVE_GMPXX=TARGET_CXX.check_header_exists(xqt,'gmpxx.h')
     7:   f=cwrite('target_gmpxx')
     8:   pa(f,locals(),"HAVE_GMPXX")
     9:   f.close()
    10:   cload(globals(),"target_gmpxx")
    11: 
End python section to cpkgs/target/gmpxx.py[1]
Start python section to spkgs/gmpxx.py[1 /1 ]
     1: #line 24 "./lpsrc/flx_gmp.pak"
     2: execfile('cpkgs'+os.sep+'target'+os.sep+'gmpxx.py')
     3: if HAVE_GMPXX:
     4:   unit_tests = ['test'+os.sep+'flx_gmp_test.flx']
     5: 
     6: iscr_source = ['lpsrc/flx_gmp.pak']
     7: weaver_directory = 'doc/gmp/'
     8: 
End python section to spkgs/gmpxx.py[1]
Start data section to config/gmpxx.fpc[1 /1 ]
     1: requires_slibs: -lgmp
     2: requires_dlibs: -lgmp
     3: provides_dlib: -lgmpxx
     4: provides_slib: -lgmpxx
     5: 
End data section to config/gmpxx.fpc[1]
Start data section to lib/flx_gmp.flx[1 /1 ]
     1: // THIS WRAPPER IS FFAU .. it is NOT LGPL licenced
     2: // This is because the wrapper was hand written from
     3: // scratch .. it was NOT derived from any LGPL headers
     4: // Code LINKED against libgmp, however, may be governed
     5: // by the LGPL licence, since the object files ARE
     6: // derived from gmp.h
     7: 
     8: header "#include <cstdio>";
     9: header gmpxx_h = """
    10: #include <gmpxx.h>
    11: namespace flx { namespace gmp {
    12: extern mpz_class lcm(mpz_class const&,mpz_class const&);
    13: extern mpz_class gcd(mpz_class const&,mpz_class const&);
    14: }}
    15: """;
    16: 
    17: body gmpxx_lcm = """
    18: namespace flx { namespace gmp {
    19: mpz_class lcm(mpz_class const &a, mpz_class const &b)
    20: {
    21:   mpz_t r; mpz_init(r);
    22:   mpz_lcm(r,a.get_mpz_t(),b.get_mpz_t());
    23:   return mpz_class(r);
    24: }
    25: }}
    26: """;
    27: 
    28: body gmpxx_gcd = """
    29: namespace flx { namespace gmp {
    30: mpz_class gcd(mpz_class const &a, mpz_class const &b)
    31: {
    32:   mpz_t r; mpz_init(r);
    33:   mpz_gcd(r,a.get_mpz_t(),b.get_mpz_t());
    34:   return mpz_class(r);
    35: }
    36: }}
    37: """;
    38: 
    39: module Gmp
    40: {
    41:   requires gmpxx_h;
    42:   requires package "gmpxx";
    43:   type mpz='mpz_class';
    44:   type mpq='mpq_class';
    45:   type mpf='mpf_class';
    46:   fun add:mpz*mpz->mpz="$1+$2";
    47:   fun sub:mpz*mpz->mpz="$1-$2";
    48:   fun mul:mpz*mpz->mpz="$1*$2";
    49:   fun div:mpz*mpz->mpz="$1/$2";
    50:   fun neg:mpz->mpz="-$1";
    51:   fun abs:mpz->mpz="abs($1)";
    52:   fun sgn:mpz->int="sgn($1)";
    53:   fun sqrt:mpz->mpz="sqrt($1)";
    54:   fun cmp:mpz*mpz->int="cmp($1,$2)";
    55:   proc fprint: ostream * mpz="*$1<<$2;";
    56:   fun eq:mpz*mpz->bool="$1==2";
    57:   fun ne:mpz*mpz->bool="$1!=$2";
    58:   fun lt:mpz*mpz->bool="$1<$2";
    59:   fun le:mpz*mpz->bool="$1<=$2";
    60:   fun gt:mpz*mpz->bool="$1>$2";
    61:   fun ge:mpz*mpz->bool="$1>=$2";
    62:   fun add:mpq*mpq->mpq="$1+$2";
    63:   fun sub:mpq*mpq->mpq="$1-$2";
    64:   fun mul:mpq*mpq->mpq="$1*$2";
    65:   fun div:mpq*mpq->mpq="$1/$2";
    66:   fun neg:mpq->mpq="-$1";
    67:   fun abs:mpq->mpq="abs($1)";
    68:   fun sgn:mpq->int="sgn($1)";
    69:   fun sqrt:mpq->mpq="sqrt($1)";
    70:   fun cmp:mpq*mpq->int="cmp($1,$2)";
    71:   proc fprint: ostream * mpq="*$1<<$2;";
    72:   fun eq:mpq*mpq->bool="$1==2";
    73:   fun ne:mpq*mpq->bool="$1!=$2";
    74:   fun lt:mpq*mpq->bool="$1<$2";
    75:   fun le:mpq*mpq->bool="$1<=$2";
    76:   fun gt:mpq*mpq->bool="$1>$2";
    77:   fun ge:mpq*mpq->bool="$1>=$2";
    78:   fun add:mpf*mpf->mpf="$1+$2";
    79:   fun sub:mpf*mpf->mpf="$1-$2";
    80:   fun mul:mpf*mpf->mpf="$1*$2";
    81:   fun div:mpf*mpf->mpf="$1/$2";
    82:   fun neg:mpf->mpf="-$1";
    83:   fun abs:mpf->mpf="abs($1)";
    84:   fun sgn:mpf->int="sgn($1)";
    85:   fun sqrt:mpf->mpf="sqrt($1)";
    86:   fun cmp:mpf*mpf->int="cmp($1,$2)";
    87:   proc fprint: ostream * mpf="*$1<<$2;";
    88:   fun eq:mpf*mpf->bool="$1==2";
    89:   fun ne:mpf*mpf->bool="$1!=$2";
    90:   fun lt:mpf*mpf->bool="$1<$2";
    91:   fun le:mpf*mpf->bool="$1<=$2";
    92:   fun gt:mpf*mpf->bool="$1>$2";
    93:   fun ge:mpf*mpf->bool="$1>=$2";
    94: fun lcm: mpz * mpz -> mpz = "flx::gmp::lcm($1,$2)" requires gmpxx_lcm;
    95: fun gcd: mpz * mpz -> mpz = "flx::gmp::gcd($1,$2)" requires gmpxx_gcd;
    96: fun wedge: mpz * mpz -> mpz = "flx::gmp::lcm($1,$2)" requires gmpxx_lcm;
    97: fun vee: mpz * mpz -> mpz = "flx::gmp::gcd($1,$2)" requires gmpxx_gcd;
    98: 
    99: fun mod: mpz * mpz -> mpz = "$1%$2";
   100: fun mpz_of_int: int -> mpz = "mpz_class($1)";
   101: fun mpq_of_int: int -> mpq = "mpq_class($1)";
   102: fun mpf_of_double: double -> mpf = "mpf_class($1)";
   103: }
   104: 
End data section to lib/flx_gmp.flx[1]
Start felix section to test/flx_gmp_test.flx[1 /1 ]
     1: #line 116 "./lpsrc/flx_gmp.pak"
     2: #import <flx.flxh>
     3: include "flx_gmp";
     4: open Gmp;
     5: 
     6: {
     7:   val x:mpz = mpz_of_int 99;
     8:   val y:mpz = mpz_of_int 7;
     9:   print x; endl;
    10:   print y; endl;
    11:   print$ x + y; endl;
    12:   print$ x - y; endl;
    13:   print$ x * y; endl;
    14:   print$ x / y; endl;
    15:   print$ x % y; endl;
    16: 
    17:   print$ x == y; endl;
    18:   print$ x != y; endl;
    19:   print$ x < y; endl;
    20:   print$ x <= y; endl;
    21:   print$ x > y; endl;
    22:   print$ x >= y; endl;
    23: 
    24:   print$ lcm (x,y); endl;
    25:   print$ gcd (x,y); endl;
    26:   print$ x /\ y; endl;
    27:   print$ x \/ y; endl;
    28: };
    29: {
    30:   val x:mpq = mpq_of_int 99;
    31:   val y:mpq = mpq_of_int 7;
    32:   print x; endl;
    33:   print y; endl;
    34:   print$ x + y; endl;
    35:   print$ x - y; endl;
    36:   print$ x * y; endl;
    37:   print$ x / y; endl;
    38:   print$ x == y; endl;
    39:   print$ x != y; endl;
    40:   print$ x < y; endl;
    41:   print$ x <= y; endl;
    42:   print$ x > y; endl;
    43:   print$ x >= y; endl;
    44: };
    45: {
    46:   val x:mpf = mpf_of_double 99.0;
    47:   val y:mpf = mpf_of_double 7.0;
    48:   print x; endl;
    49:   print y; endl;
    50:   print$ x + y; endl;
    51:   print$ x - y; endl;
    52:   print$ x * y; endl;
    53:   print$ x / y; endl;
    54:   print$ x == y; endl;
    55:   print$ x != y; endl;
    56:   print$ x < y; endl;
    57:   print$ x <= y; endl;
    58:   print$ x > y; endl;
    59:   print$ x >= y; endl;
    60: };
    61: 
End felix section to test/flx_gmp_test.flx[1]
Start data section to test/flx_gmp_test.expect[1 /1 ]
     1: 99
     2: 7
     3: 106
     4: 92
     5: 693
     6: 14
     7: 1
     8: false
     9: true
    10: false
    11: false
    12: true
    13: true
    14: 693
    15: 1
    16: 693
    17: 1
    18: 99
    19: 7
    20: 106
    21: 92
    22: 693
    23: 99/7
    24: false
    25: true
    26: false
    27: false
    28: true
    29: true
    30: 99
    31: 7
    32: 106
    33: 92
    34: 693
    35: 14.1429
    36: false
    37: true
    38: false
    39: false
    40: true
    41: true
End data section to test/flx_gmp_test.expect[1]