1 /* is_inside.c -- Test whether the first operand is in the second one.
2 
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2010, 2011,
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 int
mpfi_is_strictly_inside(mpfi_srcptr a,mpfi_srcptr b)29 mpfi_is_strictly_inside (mpfi_srcptr a, mpfi_srcptr b)
30 {
31   return mpfr_cmp (&(b->left), &(a->left)) < 0
32     && mpfr_cmp (&(a->right), &(b->right)) < 0;
33 }
34 
35 int
mpfi_is_inside(mpfi_srcptr a,mpfi_srcptr b)36 mpfi_is_inside (mpfi_srcptr a, mpfi_srcptr b)
37 {
38   /* Returns 0 if one of the operands is a NaN */
39   if (MPFI_NAN_P (a) || MPFI_NAN_P (b))
40     return 0;
41 
42   return mpfr_cmp (&(b->left), &(a->left)) <= 0
43     && mpfr_cmp (&(a->right), &(b->right)) <= 0;
44 }
45 
46 int
mpfi_is_inside_d(const double a,mpfi_srcptr b)47 mpfi_is_inside_d (const double a, mpfi_srcptr b)
48 {
49   int res;
50   mpfi_t tmp;
51 
52   mpfi_init2 (tmp, mpfi_get_prec (b));
53   mpfi_set_d (tmp, a);
54   res = mpfi_is_inside (tmp, b);
55   MPFI_CLEAR (tmp);
56 
57   return res;
58 }
59 
60 int
mpfi_is_inside_ui(const unsigned long a,mpfi_srcptr b)61 mpfi_is_inside_ui (const unsigned long a, mpfi_srcptr b)
62 {
63   /* Returns 0 if one of the operands is a NaN */
64   if (MPFI_NAN_P (b))
65     return 0;
66 
67   return mpfr_cmp_ui (&(b->left), a) <= 0
68     &&  mpfr_cmp_ui (&(b->right), a) >= 0;
69 }
70 
71 int
mpfi_is_inside_si(const long a,mpfi_srcptr b)72 mpfi_is_inside_si (const long a, mpfi_srcptr b)
73 {
74   /* Returns 0 if one of the operands is a NaN */
75   if (MPFI_NAN_P (b))
76     return 0;
77 
78   return mpfr_cmp_si (&(b->left), a) <= 0
79     &&  mpfr_cmp_si (&(b->right), a) >= 0;
80 }
81 
82 int
mpfi_is_inside_z(mpz_srcptr a,mpfi_srcptr b)83 mpfi_is_inside_z (mpz_srcptr a, mpfi_srcptr b)
84 {
85   int res;
86   mpfi_t tmp;
87 
88   mpfi_init2 (tmp, mpfi_get_prec (b));
89   mpfi_set_z (tmp, a);
90   res = mpfi_is_inside (tmp, b);
91   MPFI_CLEAR (tmp);
92 
93   return res;
94 }
95 
96 int
mpfi_is_inside_q(mpq_srcptr a,mpfi_srcptr b)97 mpfi_is_inside_q (mpq_srcptr a, mpfi_srcptr b)
98 {
99   int res;
100   mpfi_t tmp;
101   /* Returns 0 if one of the operands is a NaN */
102   if (MPFI_NAN_P (b))
103     return 0;
104 
105   mpfi_init2 (tmp, mpfi_get_prec (b));
106   mpfi_set_q (tmp, a);
107   res = mpfi_is_inside (tmp, b);
108   MPFI_CLEAR (tmp);
109 
110   return res;
111 }
112 
113 int
mpfi_is_inside_fr(mpfr_srcptr a,mpfi_srcptr b)114 mpfi_is_inside_fr (mpfr_srcptr a, mpfi_srcptr b)
115 {
116   /* Returns 0 if one of the operands is a NaN */
117   if (mpfr_nan_p (a) || MPFI_NAN_P (b))
118     return 0;
119 
120   return mpfr_cmp (a, &(b->left)) >= 0
121     &&  mpfr_cmp (a, &(b->right)) <= 0;
122 }
123