1 /* diam.c -- Various diameter functions.
2 
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2010, 2018,
4                      Spaces project, Inria Lorraine
5                      and Salsa project, INRIA Rocquencourt,
6                      and Arenaire project, Inria Rhone-Alpes, France
7                      and Lab. ANO, USTL (Univ. of Lille),  France
8 
9 This file is part of the MPFI Library.
10 
11 The MPFI Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or (at your
14 option) any later version.
15 
16 The MPFI Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20 
21 You should have received a copy of the GNU Lesser General Public License
22 along with the MPFI Library; see the file COPYING.LIB.  If not, write to
23 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24 MA 02110-1301, USA. */
25 
26 #include "mpfi-impl.h"
27 
28 /* Absolute diameter                            */
29 int
mpfi_diam_abs(mpfr_ptr diam,mpfi_srcptr interv)30 mpfi_diam_abs (mpfr_ptr diam, mpfi_srcptr interv)
31 {
32   return (mpfr_sub (diam, &(interv->right), &(interv->left), MPFI_RNDU));
33 }
34 
35 /* Relative diameter                                     */
36 /* Always computes an overestimation                     */
37 /* return value: 0 if the result is exact, > 0 otherwise */
38 int
mpfi_diam_rel(mpfr_ptr diam,mpfi_srcptr interv)39 mpfi_diam_rel (mpfr_ptr diam, mpfi_srcptr interv)
40 {
41   mpfr_t center;
42   int inexact_mid, inexact_sub = 0, inexact = 0;
43 
44   mpfr_init2 (center, mpfr_get_prec (diam));
45 
46   /* if interv is bounded, compute d/|c| where d is the absolute diameter of
47      interv and c is its midpoint
48      if interv is not bounded, d and c are infinite, their quotient does not
49      exist */
50   inexact_sub = mpfr_sub (diam, &(interv->right), &(interv->left), MPFI_RNDU);
51 
52   inexact_mid = mpfi_mid (center, interv);
53   if (mpfr_sgn (center) * inexact_mid > 0 && !mpfr_inf_p (center)) {
54     /* the absolute value of center have to be underestimated for an
55        overestimated quotient, except for the infinite cases where the
56        division below should return NaN.
57        Note that in case of underflow, mpfr_sgn(center)==0 and the
58        if-condition fails, preventing the next operation to actually increase
59        the absolute value of center */
60     if (mpfr_sgn (center) > 0)
61       mpfr_nextbelow (center);
62     else
63       mpfr_nextabove (center);
64   }
65   mpfr_abs (center, center, MPFI_RNDD); /* always exact */
66 
67   inexact = mpfr_div (diam, diam, center, MPFI_RNDU);
68   mpfr_clear (center);
69 
70   if (mpfr_nan_p (diam))
71     MPFR_RET_NAN;
72 
73   if (inexact || inexact_sub || inexact_mid)
74     return 1;
75   else
76     return 0;
77 }
78 
79 /* Diameter: relative if the interval does not contain 0 */
80 /* absolute otherwise                                    */
81 int
mpfi_diam(mpfr_ptr diam,mpfi_srcptr interv)82 mpfi_diam (mpfr_ptr diam, mpfi_srcptr interv)
83 {
84 
85   if (!mpfi_has_zero (interv)) {
86     return (mpfi_diam_rel (diam, interv));
87   }
88   else {
89     return (mpfi_diam_abs (diam, interv));
90   }
91 }
92