Debian Codegraph @

src/c/numbers/ceiling.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 * ceiling.d - implementation of CL:CEILING
6 *
7 * Copyright (c) 2010 Juan Jose Garcia Ripoll
8 *
9 * See file 'LICENSE' for the copyright details.
10 *
11 */
12
13 #define ECL_INCLUDE_MATH_H
14 #include <ecl/ecl.h>
15 #include <float.h>
16 #ifndef HAVE_ISOC99
17 # define ceilf ceil
18 #endif
19 #include <ecl/internal.h>
20
21 #pragma STDC FENV_ACCESS ON
22
23 @(defun ceiling (x &optional (y OBJNULL))
24 @
25 if (narg == 1)
26 return ecl_ceiling1(x);
27 else
28 return ecl_ceiling2(x, y);
29 @)
30
31 cl_object
32 ecl_ceiling1(cl_object x)
33 {
34 cl_object v0, v1;
35 ECL_MATHERR_CLEAR;
36
37 switch (ecl_t_of(x)) {
38 case t_fixnum:
39 case t_bignum:
40 v0 = x;
41 v1 = ecl_make_fixnum(0);
42 break;
43 case t_ratio: {
44 const cl_env_ptr the_env = ecl_process_env();
45 v0 = ecl_ceiling2(x->ratio.num, x->ratio.den);
46 v1 = ecl_make_ratio(ecl_nth_value(the_env, 1), x->ratio.den);
47 break;
48 }
49 case t_singlefloat: {
50 float d = ecl_single_float(x);
51 float y = ceilf(d);
52 v0 = _ecl_float_to_integer(y);
53 v1 = ecl_make_single_float(d - y);
54 break;
55 }
56 case t_doublefloat: {
57 double d = ecl_double_float(x);
58 double y = ceil(d);
59 v0 = _ecl_double_to_integer(y);
60 v1 = ecl_make_double_float(d - y);
61 break;
62 }
63 case t_longfloat: {
64 long double d = ecl_long_float(x);
65 long double y = ceill(d);
66 v0 = _ecl_long_double_to_integer(y);
67 v1 = ecl_make_long_float(d - y);
68 break;
69 }
70 default:
71 FEwrong_type_nth_arg(@[ceiling],1,x,@[real]);
72 }
73
74 ECL_MATHERR_TEST;
75 @(return v0 v1);
76 }
77
78 cl_object
79 ecl_ceiling2(cl_object x, cl_object y)
80 {
81 const cl_env_ptr the_env = ecl_process_env();
82 cl_object v0, v1;
83 cl_type ty;
84 ECL_MATHERR_CLEAR;
85
86 v0 = v1 = ECL_NIL;
87 ty = ecl_t_of(y);
88 if (ecl_unlikely(!ECL_REAL_TYPE_P(ty))) {
89 FEwrong_type_nth_arg(@[ceiling],2, y, @[real]);
90 }
91 switch(ecl_t_of(x)) {
92 case t_fixnum:
93 switch(ty) {
94 case t_fixnum: { /* FIX / FIX */
95 cl_fixnum a = ecl_fixnum(x); cl_fixnum b = ecl_fixnum(y);
96 cl_fixnum q = a / b; cl_fixnum r = a % b;
97 if ((r^b) > 0 && r) { /* same signs and some remainder */
98 v0 = ecl_make_fixnum(q+1);
99 v1 = ecl_make_fixnum(r-b);
100 } else {
101 v0 = ecl_make_fixnum(q);
102 v1 = ecl_make_fixnum(r);
103 }
104 break;
105 }
106 case t_bignum: { /* FIX / BIG */
107 /* We must perform the division because there is the
108 * pathological case
109 * x = MOST_NEGATIVE_FIXNUM
110 * y = - MOST_NEGATIVE_FIXNUM
111 */
112 ECL_WITH_TEMP_BIGNUM(bx,4);
113 _ecl_big_set_fixnum(bx, ecl_fixnum(x));
114 v0 = _ecl_big_ceiling(bx, y, &v1);
115 break;
116 }
117 case t_ratio: /* FIX / RAT */
118 v0 = ecl_ceiling2(ecl_times(x, y->ratio.den), y->ratio.num);
119 v1 = ecl_make_ratio(ecl_nth_value(the_env, 1), y->ratio.den);
120 break;
121 case t_singlefloat: { /* FIX / SF */
122 float n = ecl_single_float(y);
123 float p = ecl_fixnum(x)/n;
124 float q = ceilf(p);
125 v0 = _ecl_float_to_integer(q);
126 v1 = ecl_make_single_float(p*n - q*n);
127 break;
128 }
129 case t_doublefloat: { /* FIX / DF */
130 double n = ecl_double_float(y);
131 double p = ecl_fixnum(x)/n;
132 double q = ceil(p);
133 v0 = _ecl_double_to_integer(q);
134 v1 = ecl_make_double_float(p*n - q*n);
135 break;
136 }
137 case t_longfloat: { /* FIX / LF */
138 long double n = ecl_long_float(y);
139 long double p = ecl_fixnum(x)/n;
140 long double q = ceill(p);
141 v0 = _ecl_long_double_to_integer(q);
142 v1 = ecl_make_long_float(p*n - q*n);
143 break;
144 }
145 default:
146 (void)0; /*Never reached */
147 }
148 break;
149 case t_bignum:
150 switch(ecl_t_of(y)) {
151 case t_fixnum: { /* BIG / FIX */
152 ECL_WITH_TEMP_BIGNUM(by,4);
153 _ecl_big_set_fixnum(by, ecl_fixnum(y));
154 v0 = _ecl_big_ceiling(x, by, &v1);
155 break;
156 }
157 case t_bignum: { /* BIG / BIG */
158 v0 = _ecl_big_ceiling(x, y, &v1);
159 break;
160 }
161 case t_ratio: /* BIG / RAT */
162 v0 = ecl_ceiling2(ecl_times(x, y->ratio.den), y->ratio.num);
163 v1 = ecl_make_ratio(ecl_nth_value(the_env, 1), y->ratio.den);
164 break;
165 case t_singlefloat: { /* BIG / SF */
166 float n = ecl_single_float(y);
167 float p = _ecl_big_to_double(x)/n;
168 float q = ceilf(p);
169 v0 = _ecl_float_to_integer(q);
170 v1 = ecl_make_single_float(p*n - q*n);
171 break;
172 }
173 case t_doublefloat: { /* BIG / DF */
174 double n = ecl_double_float(y);
175 double p = _ecl_big_to_double(x)/n;
176 double q = ceil(p);
177 v0 = _ecl_double_to_integer(q);
178 v1 = ecl_make_double_float(p*n - q*n);
179 break;
180 }
181 case t_longfloat: { /* BIG / LF */
182 long double n = ecl_long_float(y);
183 long double p = _ecl_big_to_double(x)/n;
184 long double q = ceill(p);
185 v0 = _ecl_long_double_to_integer(q);
186 v1 = ecl_make_long_float(p*n - q*n);
187 break;
188 }
189 default:
190 (void)0; /*Never reached */
191 }
192 break;
193 case t_ratio:
194 switch(ecl_t_of(y)) {
195 case t_ratio: /* RAT / RAT */
196 v0 = ecl_ceiling2(ecl_times(x->ratio.num, y->ratio.den),
197 ecl_times(x->ratio.den, y->ratio.num));
198 v1 = ecl_make_ratio(ecl_nth_value(the_env, 1), ecl_times(x->ratio.den, y->ratio.den));
199 break;
200 default: /* RAT / ANY */
201 v0 = ecl_ceiling2(x->ratio.num, ecl_times(x->ratio.den, y));
202 v1 = ecl_divide(ecl_nth_value(the_env, 1), x->ratio.den);
203 }
204 break;
205 case t_singlefloat: { /* SF / ANY */
206 float n = ecl_to_double(y);
207 float p = ecl_single_float(x)/n;
208 float q = ceilf(p);
209 v0 = _ecl_float_to_integer(q);
210 v1 = ecl_make_single_float(p*n - q*n);
211 break;
212 }
213 case t_doublefloat: { /* DF / ANY */
214 double n = ecl_to_double(y);
215 double p = ecl_double_float(x)/n;
216 double q = ceil(p);
217 v0 = _ecl_double_to_integer(q);
218 v1 = ecl_make_double_float(p*n - q*n);
219 break;
220 }
221 case t_longfloat: { /* LF / ANY */
222 long double n = ecl_to_long_double(y);
223 long double p = ecl_long_float(x)/n;
224 long double q = ceill(p);
225 v0 = _ecl_long_double_to_integer(q);
226 v1 = ecl_make_long_float(p*n - q*n);
227 break;
228 }
229 default:
230 FEwrong_type_nth_arg(@[ceiling], 1, x, @[real]);
231 }
232
233 ECL_MATHERR_TEST;
234 ecl_return2(the_env, v0, v1);
235 }