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