Debian Codegraph @

src/c/numbers/number_compare.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * number_compare.d - number comparison
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15
16 #include <ecl/ecl.h>
17 #include <ecl/impl/math_dispatch2.h>
18 #include "numbers/float_fix_compare.d"
19
20 /*
21 *
22 * The value of ecl_number_compare(x, y) is
23 *
24 * -1 if x < y
25 * 0 if x = y
26 * 1 if x > y.
27 *
28 * If x or y is not real, it fails.
29 */
30 int
31 ecl_number_compare(cl_object x, cl_object y)
32 {
33 double dx, dy;
34 long double ldx, ldy;
35 BEGIN:
36 MATH_DISPATCH2_BEGIN(x,y) {
37 /* rational x rational */
38 CASE_FIXNUM_FIXNUM {
39 cl_fixnum
40 ix = ecl_fixnum(x),
41 iy = ecl_fixnum(y);
42 if (ix < iy) return -1;
43 else return (ix != iy);
44 }
45 /* INV: (= x y) can't be zero since fixnum != bignum */
46 CASE_FIXNUM_BIGNUM { return _ecl_big_sign(y) < 0 ? 1 : -1; }
47 CASE_BIGNUM_FIXNUM { return _ecl_big_sign(x) < 0 ? -1 : 1; }
48 CASE_BIGNUM_BIGNUM { return _ecl_big_compare(x, y); }
49 CASE_FIXNUM_RATIO;
50 CASE_BIGNUM_RATIO { return ecl_number_compare(ecl_times(x, y->ratio.den), y->ratio.num); }
51 CASE_RATIO_FIXNUM;
52 CASE_RATIO_BIGNUM { return ecl_number_compare(x->ratio.num, ecl_times(y, x->ratio.den)); }
53 CASE_RATIO_RATIO { return ecl_number_compare(ecl_times(x->ratio.num, y->ratio.den),
54 ecl_times(y->ratio.num, x->ratio.den)); }
55 /* float x fixnum */
56 CASE_SINGLE_FLOAT_FIXNUM { return -double_fix_compare(ecl_fixnum(y), ecl_single_float(x)); }
57 CASE_FIXNUM_SINGLE_FLOAT { return double_fix_compare(ecl_fixnum(x), ecl_single_float(y)); }
58 CASE_DOUBLE_FLOAT_FIXNUM { return -double_fix_compare(ecl_fixnum(y), ecl_double_float(x)); }
59 CASE_FIXNUM_DOUBLE_FLOAT { return double_fix_compare(ecl_fixnum(x), ecl_double_float(y)); }
60 CASE_LONG_FLOAT_FIXNUM { return -long_double_fix_compare(ecl_fixnum(y), ecl_long_float(x)); }
61 CASE_FIXNUM_LONG_FLOAT { return long_double_fix_compare(ecl_fixnum(x), ecl_long_float(y)); }
62 /* float x [bignum,ratio] */
63 CASE_SINGLE_FLOAT_BIGNUM;
64 CASE_SINGLE_FLOAT_RATIO;
65 CASE_DOUBLE_FLOAT_BIGNUM;
66 CASE_DOUBLE_FLOAT_RATIO;
67 CASE_LONG_FLOAT_BIGNUM;
68 CASE_LONG_FLOAT_RATIO;
69 {
70 #ifdef ECL_IEEE_FP
71 if (ecl_float_infinity_p(x))
72 return ecl_number_compare(x, ecl_make_fixnum(0));
73 #endif
74 x = cl_rational(x);
75 goto BEGIN;
76 }
77 CASE_BIGNUM_SINGLE_FLOAT;
78 CASE_RATIO_SINGLE_FLOAT;
79 CASE_BIGNUM_DOUBLE_FLOAT;
80 CASE_RATIO_DOUBLE_FLOAT;
81 CASE_BIGNUM_LONG_FLOAT;
82 CASE_RATIO_LONG_FLOAT;
83 {
84 #ifdef ECL_IEEE_FP
85 if (ecl_float_infinity_p(y))
86 return ecl_number_compare(ecl_make_fixnum(0), y);
87 #endif
88 y = cl_rational(y);
89 goto BEGIN;
90 }
91 /* float x float */
92 CASE_SINGLE_FLOAT_SINGLE_FLOAT {
93 dx = ecl_single_float(x);
94 dy = ecl_single_float(y);
95 goto DOUBLEFLOAT;
96 }
97 CASE_SINGLE_FLOAT_DOUBLE_FLOAT {
98 dx = ecl_single_float(x);
99 dy = ecl_double_float(y);
100 goto DOUBLEFLOAT;
101 }
102 CASE_DOUBLE_FLOAT_SINGLE_FLOAT {
103 dx = ecl_double_float(x);
104 dy = ecl_single_float(y);
105 goto DOUBLEFLOAT;
106 }
107 CASE_DOUBLE_FLOAT_DOUBLE_FLOAT {
108 dx = ecl_double_float(x);
109 dy = ecl_double_float(y);
110 DOUBLEFLOAT:
111 if (dx == dy) return 0;
112 else return (dx < dy) ? -1 : 1;
113 }
114 CASE_SINGLE_FLOAT_LONG_FLOAT {
115 ldx = ecl_single_float(x);
116 ldy = ecl_long_float(y);
117 goto LONGFLOAT;
118 }
119 CASE_LONG_FLOAT_SINGLE_FLOAT {
120 ldx = ecl_long_float(x);
121 ldy = ecl_single_float(y);
122 goto LONGFLOAT;
123 }
124 CASE_DOUBLE_FLOAT_LONG_FLOAT {
125 ldx = ecl_double_float(x);
126 ldy = ecl_long_float(y);
127 goto LONGFLOAT;
128 }
129 CASE_LONG_FLOAT_DOUBLE_FLOAT {
130 ldx = ecl_long_float(x);
131 ldy = ecl_double_float(y);
132 goto LONGFLOAT;
133 }
134 CASE_LONG_FLOAT_LONG_FLOAT {
135 ldx = ecl_long_float(x);
136 ldy = ecl_long_float(y);
137 LONGFLOAT:
138 if (ldx == ldy) return 0;
139 else return (ldx < ldy) ? -1 : 1;
140 }
141 CASE_UNKNOWN(@[<],x,y,@[real]);
142 }
143 MATH_DISPATCH2_END;
144 }
145
146 static cl_object
147 monotonic(int s, int t, int narg, ecl_va_list nums)
148 {
149 cl_object c, d;
150
151 if (narg == 0) {
152 FEwrong_num_arguments_anonym();
153 }
154 if (narg == 1) {
155 c = ecl_va_arg(nums);
156 if (ECL_REAL_TYPE_P(ecl_t_of(c))) {
157 return1(ECL_T);
158 }
159 FEwrong_type_nth_arg(@[<], 1, c, @[real]);
160 }
161 /* INV: type check occurs in ecl_number_compare() */
162 c = ecl_va_arg(nums);
163 #ifdef ECL_IEEE_FP
164 if (ecl_float_nan_p(c))
165 return1(ECL_NIL);
166 #endif
167 for (; --narg; c = d) {
168 d = ecl_va_arg(nums);
169 if (
170 #ifdef ECL_IEEE_FP
171 ecl_float_nan_p(d) ||
172 #endif
173 s*ecl_number_compare(d, c) < t)
174 return1(ECL_NIL);
175 }
176 return1(ECL_T);
177 }
178
179 #define MONOTONIC(i, j) (cl_narg narg, ...) \
180 { ecl_va_list nums; ecl_va_start(nums, narg, narg, 0); \
181 cl_object result = monotonic(i, j, narg, nums); \
182 ecl_va_end(nums); \
183 return result; }
184
185 cl_object @<= MONOTONIC( 1, 0)
186 cl_object @>= MONOTONIC(-1, 0)
187 cl_object @< MONOTONIC( 1, 1)
188 cl_object @> MONOTONIC(-1, 1)