github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/demos/expr/exprqa.c (about)

     1  /* mpq expression evaluation
     2  
     3  Copyright 2000, 2001, 2004 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library.
     6  
     7  The GNU MP Library is free software; you can redistribute it and/or modify
     8  it under the terms of either:
     9  
    10    * the GNU Lesser General Public License as published by the Free
    11      Software Foundation; either version 3 of the License, or (at your
    12      option) any later version.
    13  
    14  or
    15  
    16    * the GNU General Public License as published by the Free Software
    17      Foundation; either version 2 of the License, or (at your option) any
    18      later version.
    19  
    20  or both in parallel, as here.
    21  
    22  The GNU MP Library is distributed in the hope that it will be useful, but
    23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    24  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    25  for more details.
    26  
    27  You should have received copies of the GNU General Public License and the
    28  GNU Lesser General Public License along with the GNU MP Library.  If not,
    29  see https://www.gnu.org/licenses/.  */
    30  
    31  
    32  #include <stdio.h>
    33  #include "gmp.h"
    34  #include "expr-impl.h"
    35  
    36  
    37  static int
    38  e_mpq_ulong_p (mpq_srcptr q)
    39  {
    40    return mpz_fits_ulong_p (mpq_numref (q))
    41      && mpz_cmp_ui (mpq_denref (q), 1L) == 0;
    42  }
    43  
    44  /* get value as a ui, on the assumption it fits */
    45  static int
    46  e_mpq_get_ui_fits (mpq_srcptr q)
    47  {
    48    return mpz_get_ui (mpq_numref (q));
    49  }
    50  
    51  static void
    52  e_mpq_set_si1 (mpq_ptr q, long num)
    53  {
    54    mpq_set_si (q, num, 1L);
    55  }
    56  
    57  /* The same as mpz, but putting the result in the numerator.  Negatives and
    58     fractions aren't parsed here because '-' and '/' are operators. */
    59  static size_t
    60  e_mpq_number (mpq_ptr res, const char *e, size_t elen, int base)
    61  {
    62    mpz_set_ui (mpq_denref (res), 1L);
    63    return mpexpr_mpz_number (mpq_numref (res), e, elen, base);
    64  }
    65  
    66  
    67  /* ignoring prec */
    68  static void
    69  e_mpq_init (mpq_ptr q, unsigned long prec)
    70  {
    71    mpq_init (q);
    72  }
    73  
    74  int
    75  mpq_expr_a (const struct mpexpr_operator_t *table,
    76              mpq_ptr res, int base,
    77              const char *e, size_t elen,
    78              mpq_srcptr var[26])
    79  {
    80    struct mpexpr_parse_t  p;
    81  
    82    p.table = table;
    83    p.res = (mpX_ptr) res;
    84    p.base = base;
    85    p.e = e;
    86    p.elen = elen;
    87    p.var = (mpX_srcptr *) var;
    88  
    89    p.mpX_clear       = (mpexpr_fun_one_t)      mpq_clear;
    90    p.mpX_ulong_p     = (mpexpr_fun_i_unary_t)  e_mpq_ulong_p;
    91    p.mpX_get_ui      = (mpexpr_fun_get_ui_t)   e_mpq_get_ui_fits;
    92    p.mpX_init        = (mpexpr_fun_unary_ui_t) e_mpq_init;
    93    p.mpX_number      = (mpexpr_fun_number_t)   e_mpq_number;
    94    p.mpX_set         = (mpexpr_fun_unary_t)    mpq_set;
    95    p.mpX_set_or_swap = (mpexpr_fun_unary_t)    mpq_swap;
    96    p.mpX_set_si      = (mpexpr_fun_set_si_t)   e_mpq_set_si1;
    97    p.mpX_swap        = (mpexpr_fun_swap_t)     mpq_swap;
    98  
    99    return mpexpr_evaluate (&p);
   100  }