-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathcmp.c
66 lines (49 loc) · 2.03 KB
/
cmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*=============================================================================
This file is part of ARB.
ARB is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
ARB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ARB; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2012 Fredrik Johansson
******************************************************************************/
#include "fmpr.h"
int
fmpr_cmp(const fmpr_t x, const fmpr_t y)
{
int res, xsign, ysign;
fmpr_t t;
if (fmpr_equal(x, y))
return 0;
if (fmpr_is_special(x) || fmpr_is_special(y))
{
if (fmpr_is_nan(x) || fmpr_is_nan(y))
return 0;
if (fmpr_is_zero(y)) return fmpr_sgn(x);
if (fmpr_is_zero(x)) return -fmpr_sgn(y);
if (fmpr_is_pos_inf(x)) return 1;
if (fmpr_is_neg_inf(y)) return 1;
return -1;
}
xsign = fmpr_sgn(x);
ysign = fmpr_sgn(y);
if (xsign != ysign)
return (xsign < 0) ? -1 : 1;
/* Reduces to integer comparison of bottom exponents are the same */
if (fmpz_equal(fmpr_expref(x), fmpr_expref(y)))
return fmpz_cmp(fmpr_manref(x), fmpr_manref(y)) < 0 ? -1 : 1;
/* TODO: compare position of top exponents to avoid subtraction */
fmpr_init(t);
fmpr_sub(t, x, y, 2, FMPR_RND_DOWN);
res = fmpr_sgn(t);
fmpr_clear(t);
return res;
}