github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpf/reldiff.c (about)

     1  /* mpf_reldiff -- Generate the relative difference of two floats.
     2  
     3  Copyright 1996, 2001, 2004, 2005 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  #include "gmp.h"
    32  #include "gmp-impl.h"
    33  
    34  
    35  /* The precision we use for d = x-y is based on what mpf_div will want from
    36     the dividend.  It calls mpn_div_q to produce a quotient of rprec+1 limbs.
    37     So rprec+1 == dsize - xsize + 1, hence dprec = rprec+xsize.  */
    38  
    39  void
    40  mpf_reldiff (mpf_t rdiff, mpf_srcptr x, mpf_srcptr y)
    41  {
    42    if (UNLIKELY (SIZ(x) == 0))
    43      {
    44        mpf_set_ui (rdiff, (unsigned long int) (mpf_sgn (y) != 0));
    45      }
    46    else
    47      {
    48        mp_size_t dprec;
    49        mpf_t d;
    50        TMP_DECL;
    51  
    52        TMP_MARK;
    53        dprec = PREC(rdiff) + ABSIZ(x);
    54        ASSERT (PREC(rdiff)+1 == dprec - ABSIZ(x) + 1);
    55  
    56        PREC(d) = dprec;
    57        PTR(d) = TMP_ALLOC_LIMBS (dprec + 1);
    58  
    59        mpf_sub (d, x, y);
    60        SIZ(d) = ABSIZ(d);
    61        mpf_div (rdiff, d, x);
    62  
    63        TMP_FREE;
    64      }
    65  }