1 /*
2     Copyright (C) 2016 Arb authors
3 
4     This file is part of Arb.
5 
6     Arb is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include "arb.h"
13 
14 int
arb_intersection(arb_t z,const arb_t x,const arb_t y,slong prec)15 arb_intersection(arb_t z, const arb_t x, const arb_t y, slong prec)
16 {
17     arf_t left, right, t, xr, yr;
18     int result;
19 
20     if (arf_is_nan(arb_midref(x)) || arf_is_nan(arb_midref(y)))
21     {
22         arb_indeterminate(z);
23         return 1;
24     }
25 
26     if (mag_is_inf(arb_radref(x)) && mag_is_inf(arb_radref(y)))
27     {
28         arb_zero_pm_inf(z);
29         return 1;
30     }
31 
32     result = arb_overlaps(x, y);
33 
34     if (result)
35     {
36         arf_init(left);
37         arf_init(right);
38         arf_init(t);
39 
40         arf_init_set_mag_shallow(xr, arb_radref(x));
41         arf_init_set_mag_shallow(yr, arb_radref(y));
42 
43         arf_sub(left, arb_midref(x), xr, prec, ARF_RND_FLOOR);
44         arf_sub(t, arb_midref(y), yr, prec, ARF_RND_FLOOR);
45         arf_max(left, left, t);
46 
47         arf_add(right, arb_midref(x), xr, prec, ARF_RND_CEIL);
48         arf_add(t, arb_midref(y), yr, prec, ARF_RND_CEIL);
49         arf_min(right, right, t);
50 
51         arb_set_interval_arf(z, left, right, prec);
52 
53         arf_clear(left);
54         arf_clear(right);
55         arf_clear(t);
56     }
57 
58     return result;
59 }
60