102
|
1 /* Primitive operations on floating point for GNU Emacs Lisp interpreter.
|
64770
|
2 Copyright (C) 1988, 1993, 1994, 1999, 2002, 2003, 2004,
|
68651
|
3 2005, 2006 Free Software Foundation, Inc.
|
102
|
4
|
|
5 This file is part of GNU Emacs.
|
|
6
|
|
7 GNU Emacs is free software; you can redistribute it and/or modify
|
|
8 it under the terms of the GNU General Public License as published by
|
621
|
9 the Free Software Foundation; either version 2, or (at your option)
|
102
|
10 any later version.
|
|
11
|
|
12 GNU Emacs is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with GNU Emacs; see the file COPYING. If not, write to
|
64084
|
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
20 Boston, MA 02110-1301, USA. */
|
102
|
21
|
|
22
|
2094
|
23 /* ANSI C requires only these float functions:
|
|
24 acos, asin, atan, atan2, ceil, cos, cosh, exp, fabs, floor, fmod,
|
|
25 frexp, ldexp, log, log10, modf, pow, sin, sinh, sqrt, tan, tanh.
|
|
26
|
|
27 Define HAVE_INVERSE_HYPERBOLIC if you have acosh, asinh, and atanh.
|
|
28 Define HAVE_CBRT if you have cbrt.
|
19737
|
29 Define HAVE_RINT if you have a working rint.
|
2094
|
30 If you don't define these, then the appropriate routines will be simulated.
|
|
31
|
|
32 Define HAVE_MATHERR if on a system supporting the SysV matherr callback.
|
|
33 (This should happen automatically.)
|
|
34
|
|
35 Define FLOAT_CHECK_ERRNO if the float library routines set errno.
|
|
36 This has no effect if HAVE_MATHERR is defined.
|
|
37
|
|
38 Define FLOAT_CATCH_SIGILL if the float library routines signal SIGILL.
|
|
39 (What systems actually do this? Please let us know.)
|
|
40
|
|
41 Define FLOAT_CHECK_DOMAIN if the float library doesn't handle errors by
|
14036
|
42 either setting errno, or signaling SIGFPE/SIGILL. Otherwise, domain and
|
2094
|
43 range checking will happen before calling the float routines. This has
|
|
44 no effect if HAVE_MATHERR is defined (since matherr will be called when
|
|
45 a domain error occurs.)
|
|
46 */
|
|
47
|
26088
b7aa6ac26872
Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
48 #include <config.h>
|
102
|
49 #include <signal.h>
|
|
50 #include "lisp.h"
|
638
|
51 #include "syssignal.h"
|
102
|
52
|
20122
|
53 #if STDC_HEADERS
|
|
54 #include <float.h>
|
|
55 #endif
|
|
56
|
16786
|
57 /* If IEEE_FLOATING_POINT isn't defined, default it from FLT_*. */
|
|
58 #ifndef IEEE_FLOATING_POINT
|
|
59 #if (FLT_RADIX == 2 && FLT_MANT_DIG == 24 \
|
|
60 && FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128)
|
|
61 #define IEEE_FLOATING_POINT 1
|
|
62 #else
|
|
63 #define IEEE_FLOATING_POINT 0
|
|
64 #endif
|
|
65 #endif
|
|
66
|
4843
|
67 /* Work around a problem that happens because math.h on hpux 7
|
|
68 defines two static variables--which, in Emacs, are not really static,
|
|
69 because `static' is defined as nothing. The problem is that they are
|
|
70 defined both here and in lread.c.
|
|
71 These macros prevent the name conflict. */
|
|
72 #if defined (HPUX) && !defined (HPUX8)
|
|
73 #define _MAXLDBL floatfns_maxldbl
|
|
74 #define _NMAXLDBL floatfns_nmaxldbl
|
|
75 #endif
|
|
76
|
102
|
77 #include <math.h>
|
2094
|
78
|
4881
|
79 /* This declaration is omitted on some systems, like Ultrix. */
|
7448
|
80 #if !defined (HPUX) && defined (HAVE_LOGB) && !defined (logb)
|
2205
|
81 extern double logb ();
|
7448
|
82 #endif /* not HPUX and HAVE_LOGB and no logb macro */
|
2205
|
83
|
2094
|
84 #if defined(DOMAIN) && defined(SING) && defined(OVERFLOW)
|
|
85 /* If those are defined, then this is probably a `matherr' machine. */
|
|
86 # ifndef HAVE_MATHERR
|
|
87 # define HAVE_MATHERR
|
|
88 # endif
|
|
89 #endif
|
|
90
|
3027
|
91 #ifdef NO_MATHERR
|
3025
|
92 #undef HAVE_MATHERR
|
|
93 #endif
|
|
94
|
2094
|
95 #ifdef HAVE_MATHERR
|
|
96 # ifdef FLOAT_CHECK_ERRNO
|
|
97 # undef FLOAT_CHECK_ERRNO
|
|
98 # endif
|
|
99 # ifdef FLOAT_CHECK_DOMAIN
|
|
100 # undef FLOAT_CHECK_DOMAIN
|
|
101 # endif
|
|
102 #endif
|
|
103
|
|
104 #ifndef NO_FLOAT_CHECK_ERRNO
|
|
105 #define FLOAT_CHECK_ERRNO
|
|
106 #endif
|
|
107
|
|
108 #ifdef FLOAT_CHECK_ERRNO
|
|
109 # include <errno.h>
|
485
|
110
|
31095
|
111 #ifndef USE_CRT_DLL
|
485
|
112 extern int errno;
|
2094
|
113 #endif
|
31095
|
114 #endif
|
485
|
115
|
|
116 /* Avoid traps on VMS from sinh and cosh.
|
|
117 All the other functions set errno instead. */
|
|
118
|
|
119 #ifdef VMS
|
|
120 #undef cosh
|
|
121 #undef sinh
|
|
122 #define cosh(x) ((exp(x)+exp(-x))*0.5)
|
|
123 #define sinh(x) ((exp(x)-exp(-x))*0.5)
|
|
124 #endif /* VMS */
|
|
125
|
39814
|
126 #ifdef FLOAT_CATCH_SIGILL
|
621
|
127 static SIGTYPE float_error ();
|
39814
|
128 #endif
|
102
|
129
|
|
130 /* Nonzero while executing in floating point.
|
|
131 This tells float_error what to do. */
|
|
132
|
|
133 static int in_float;
|
|
134
|
|
135 /* If an argument is out of range for a mathematical function,
|
16207
|
136 here is the actual argument value to use in the error message.
|
|
137 These variables are used only across the floating point library call
|
|
138 so there is no need to staticpro them. */
|
102
|
139
|
2094
|
140 static Lisp_Object float_error_arg, float_error_arg2;
|
|
141
|
|
142 static char *float_error_fn_name;
|
102
|
143
|
485
|
144 /* Evaluate the floating point expression D, recording NUM
|
|
145 as the original argument for error messages.
|
|
146 D is normally an assignment expression.
|
1918
|
147 Handle errors which may result in signals or may set errno.
|
|
148
|
|
149 Note that float_error may be declared to return void, so you can't
|
|
150 just cast the zero after the colon to (SIGTYPE) to make the types
|
|
151 check properly. */
|
485
|
152
|
2094
|
153 #ifdef FLOAT_CHECK_ERRNO
|
|
154 #define IN_FLOAT(d, name, num) \
|
|
155 do { \
|
|
156 float_error_arg = num; \
|
|
157 float_error_fn_name = name; \
|
|
158 in_float = 1; errno = 0; (d); in_float = 0; \
|
|
159 switch (errno) { \
|
|
160 case 0: break; \
|
|
161 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
|
|
162 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
|
|
163 default: arith_error (float_error_fn_name, float_error_arg); \
|
|
164 } \
|
|
165 } while (0)
|
|
166 #define IN_FLOAT2(d, name, num, num2) \
|
|
167 do { \
|
|
168 float_error_arg = num; \
|
|
169 float_error_arg2 = num2; \
|
|
170 float_error_fn_name = name; \
|
|
171 in_float = 1; errno = 0; (d); in_float = 0; \
|
|
172 switch (errno) { \
|
|
173 case 0: break; \
|
|
174 case EDOM: domain_error (float_error_fn_name, float_error_arg); \
|
|
175 case ERANGE: range_error (float_error_fn_name, float_error_arg); \
|
|
176 default: arith_error (float_error_fn_name, float_error_arg); \
|
|
177 } \
|
|
178 } while (0)
|
|
179 #else
|
3407
|
180 #define IN_FLOAT(d, name, num) (in_float = 1, (d), in_float = 0)
|
2094
|
181 #define IN_FLOAT2(d, name, num, num2) (in_float = 1, (d), in_float = 0)
|
|
182 #endif
|
|
183
|
6375
|
184 /* Convert float to Lisp_Int if it fits, else signal a range error
|
|
185 using the given arguments. */
|
|
186 #define FLOAT_TO_INT(x, i, name, num) \
|
|
187 do \
|
|
188 { \
|
39638
|
189 if (FIXNUM_OVERFLOW_P (x)) \
|
6375
|
190 range_error (name, num); \
|
9267
1b685d477c10
(FLOAT_TO_INT, FLOAT_TO_INT2, Fexpt, Flogb, Ffloor): Use new accessor macros
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
191 XSETINT (i, (EMACS_INT)(x)); \
|
6375
|
192 } \
|
|
193 while (0)
|
|
194 #define FLOAT_TO_INT2(x, i, name, num1, num2) \
|
|
195 do \
|
|
196 { \
|
39638
|
197 if (FIXNUM_OVERFLOW_P (x)) \
|
6375
|
198 range_error2 (name, num1, num2); \
|
9267
1b685d477c10
(FLOAT_TO_INT, FLOAT_TO_INT2, Fexpt, Flogb, Ffloor): Use new accessor macros
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
199 XSETINT (i, (EMACS_INT)(x)); \
|
6375
|
200 } \
|
|
201 while (0)
|
|
202
|
2094
|
203 #define arith_error(op,arg) \
|
|
204 Fsignal (Qarith_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
|
|
205 #define range_error(op,arg) \
|
|
206 Fsignal (Qrange_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
|
6375
|
207 #define range_error2(op,a1,a2) \
|
|
208 Fsignal (Qrange_error, Fcons (build_string ((op)), \
|
|
209 Fcons ((a1), Fcons ((a2), Qnil))))
|
2094
|
210 #define domain_error(op,arg) \
|
|
211 Fsignal (Qdomain_error, Fcons (build_string ((op)), Fcons ((arg), Qnil)))
|
|
212 #define domain_error2(op,a1,a2) \
|
6375
|
213 Fsignal (Qdomain_error, Fcons (build_string ((op)), \
|
|
214 Fcons ((a1), Fcons ((a2), Qnil))))
|
102
|
215
|
|
216 /* Extract a Lisp number as a `double', or signal an error. */
|
|
217
|
|
218 double
|
|
219 extract_float (num)
|
|
220 Lisp_Object num;
|
|
221 {
|
40656
|
222 CHECK_NUMBER_OR_FLOAT (num);
|
102
|
223
|
9129
b2d1d925d5cc
(extract_float, Fexpt, Fabs, Ffloat, Fceiling, Ffloor, Fround, Ftruncate): Use
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
224 if (FLOATP (num))
|
25645
|
225 return XFLOAT_DATA (num);
|
102
|
226 return (double) XINT (num);
|
|
227 }
|
683
|
228
|
|
229 /* Trig functions. */
|
102
|
230
|
|
231 DEFUN ("acos", Facos, Sacos, 1, 1, 0,
|
41001
|
232 doc: /* Return the inverse cosine of ARG. */)
|
|
233 (arg)
|
2094
|
234 register Lisp_Object arg;
|
102
|
235 {
|
2094
|
236 double d = extract_float (arg);
|
|
237 #ifdef FLOAT_CHECK_DOMAIN
|
|
238 if (d > 1.0 || d < -1.0)
|
|
239 domain_error ("acos", arg);
|
|
240 #endif
|
|
241 IN_FLOAT (d = acos (d), "acos", arg);
|
102
|
242 return make_float (d);
|
|
243 }
|
|
244
|
|
245 DEFUN ("asin", Fasin, Sasin, 1, 1, 0,
|
41001
|
246 doc: /* Return the inverse sine of ARG. */)
|
|
247 (arg)
|
2094
|
248 register Lisp_Object arg;
|
102
|
249 {
|
2094
|
250 double d = extract_float (arg);
|
|
251 #ifdef FLOAT_CHECK_DOMAIN
|
|
252 if (d > 1.0 || d < -1.0)
|
|
253 domain_error ("asin", arg);
|
|
254 #endif
|
|
255 IN_FLOAT (d = asin (d), "asin", arg);
|
102
|
256 return make_float (d);
|
|
257 }
|
|
258
|
43413
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
259 DEFUN ("atan", Fatan, Satan, 1, 2, 0,
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
260 doc: /* Return the inverse tangent of the arguments.
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
261 If only one argument Y is given, return the inverse tangent of Y.
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
262 If two arguments Y and X are given, return the inverse tangent of Y
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
263 divided by X, i.e. the angle in radians between the vector (X, Y)
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
264 and the x-axis. */)
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
265 (y, x)
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
266 register Lisp_Object y, x;
|
102
|
267 {
|
43413
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
268 double d = extract_float (y);
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
269
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
270 if (NILP (x))
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
271 IN_FLOAT (d = atan (d), "atan", y);
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
272 else
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
273 {
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
274 double d2 = extract_float (x);
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
275
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
276 IN_FLOAT2 (d = atan2 (d, d2), "atan", y, x);
|
0f448bd1bf9a
(Fatan): Accept an optional second arg and call atan2 if passed 2 args.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
277 }
|
102
|
278 return make_float (d);
|
|
279 }
|
|
280
|
683
|
281 DEFUN ("cos", Fcos, Scos, 1, 1, 0,
|
41001
|
282 doc: /* Return the cosine of ARG. */)
|
|
283 (arg)
|
2094
|
284 register Lisp_Object arg;
|
683
|
285 {
|
2094
|
286 double d = extract_float (arg);
|
|
287 IN_FLOAT (d = cos (d), "cos", arg);
|
683
|
288 return make_float (d);
|
|
289 }
|
|
290
|
|
291 DEFUN ("sin", Fsin, Ssin, 1, 1, 0,
|
41001
|
292 doc: /* Return the sine of ARG. */)
|
|
293 (arg)
|
2094
|
294 register Lisp_Object arg;
|
102
|
295 {
|
2094
|
296 double d = extract_float (arg);
|
|
297 IN_FLOAT (d = sin (d), "sin", arg);
|
683
|
298 return make_float (d);
|
|
299 }
|
|
300
|
|
301 DEFUN ("tan", Ftan, Stan, 1, 1, 0,
|
41001
|
302 doc: /* Return the tangent of ARG. */)
|
|
303 (arg)
|
2094
|
304 register Lisp_Object arg;
|
683
|
305 {
|
2094
|
306 double d = extract_float (arg);
|
|
307 double c = cos (d);
|
|
308 #ifdef FLOAT_CHECK_DOMAIN
|
|
309 if (c == 0.0)
|
|
310 domain_error ("tan", arg);
|
|
311 #endif
|
|
312 IN_FLOAT (d = sin (d) / c, "tan", arg);
|
102
|
313 return make_float (d);
|
|
314 }
|
|
315
|
683
|
316 #if 0 /* Leave these out unless we find there's a reason for them. */
|
|
317
|
102
|
318 DEFUN ("bessel-j0", Fbessel_j0, Sbessel_j0, 1, 1, 0,
|
41001
|
319 doc: /* Return the bessel function j0 of ARG. */)
|
|
320 (arg)
|
2094
|
321 register Lisp_Object arg;
|
102
|
322 {
|
2094
|
323 double d = extract_float (arg);
|
|
324 IN_FLOAT (d = j0 (d), "bessel-j0", arg);
|
102
|
325 return make_float (d);
|
|
326 }
|
|
327
|
|
328 DEFUN ("bessel-j1", Fbessel_j1, Sbessel_j1, 1, 1, 0,
|
41001
|
329 doc: /* Return the bessel function j1 of ARG. */)
|
|
330 (arg)
|
2094
|
331 register Lisp_Object arg;
|
102
|
332 {
|
2094
|
333 double d = extract_float (arg);
|
|
334 IN_FLOAT (d = j1 (d), "bessel-j1", arg);
|
102
|
335 return make_float (d);
|
|
336 }
|
|
337
|
|
338 DEFUN ("bessel-jn", Fbessel_jn, Sbessel_jn, 2, 2, 0,
|
41001
|
339 doc: /* Return the order N bessel function output jn of ARG.
|
|
340 The first arg (the order) is truncated to an integer. */)
|
|
341 (n, arg)
|
14076
|
342 register Lisp_Object n, arg;
|
102
|
343 {
|
14076
|
344 int i1 = extract_float (n);
|
|
345 double f2 = extract_float (arg);
|
102
|
346
|
14076
|
347 IN_FLOAT (f2 = jn (i1, f2), "bessel-jn", n);
|
102
|
348 return make_float (f2);
|
|
349 }
|
|
350
|
|
351 DEFUN ("bessel-y0", Fbessel_y0, Sbessel_y0, 1, 1, 0,
|
41001
|
352 doc: /* Return the bessel function y0 of ARG. */)
|
|
353 (arg)
|
2094
|
354 register Lisp_Object arg;
|
102
|
355 {
|
2094
|
356 double d = extract_float (arg);
|
|
357 IN_FLOAT (d = y0 (d), "bessel-y0", arg);
|
102
|
358 return make_float (d);
|
|
359 }
|
|
360
|
|
361 DEFUN ("bessel-y1", Fbessel_y1, Sbessel_y1, 1, 1, 0,
|
41001
|
362 doc: /* Return the bessel function y1 of ARG. */)
|
|
363 (arg)
|
2094
|
364 register Lisp_Object arg;
|
102
|
365 {
|
2094
|
366 double d = extract_float (arg);
|
|
367 IN_FLOAT (d = y1 (d), "bessel-y0", arg);
|
102
|
368 return make_float (d);
|
|
369 }
|
|
370
|
|
371 DEFUN ("bessel-yn", Fbessel_yn, Sbessel_yn, 2, 2, 0,
|
41001
|
372 doc: /* Return the order N bessel function output yn of ARG.
|
|
373 The first arg (the order) is truncated to an integer. */)
|
|
374 (n, arg)
|
14076
|
375 register Lisp_Object n, arg;
|
102
|
376 {
|
14076
|
377 int i1 = extract_float (n);
|
|
378 double f2 = extract_float (arg);
|
102
|
379
|
14076
|
380 IN_FLOAT (f2 = yn (i1, f2), "bessel-yn", n);
|
102
|
381 return make_float (f2);
|
|
382 }
|
683
|
383
|
|
384 #endif
|
102
|
385
|
683
|
386 #if 0 /* Leave these out unless we see they are worth having. */
|
102
|
387
|
|
388 DEFUN ("erf", Ferf, Serf, 1, 1, 0,
|
41001
|
389 doc: /* Return the mathematical error function of ARG. */)
|
|
390 (arg)
|
2094
|
391 register Lisp_Object arg;
|
102
|
392 {
|
2094
|
393 double d = extract_float (arg);
|
|
394 IN_FLOAT (d = erf (d), "erf", arg);
|
102
|
395 return make_float (d);
|
|
396 }
|
|
397
|
|
398 DEFUN ("erfc", Ferfc, Serfc, 1, 1, 0,
|
41001
|
399 doc: /* Return the complementary error function of ARG. */)
|
|
400 (arg)
|
2094
|
401 register Lisp_Object arg;
|
102
|
402 {
|
2094
|
403 double d = extract_float (arg);
|
|
404 IN_FLOAT (d = erfc (d), "erfc", arg);
|
102
|
405 return make_float (d);
|
|
406 }
|
|
407
|
683
|
408 DEFUN ("log-gamma", Flog_gamma, Slog_gamma, 1, 1, 0,
|
41001
|
409 doc: /* Return the log gamma of ARG. */)
|
|
410 (arg)
|
2094
|
411 register Lisp_Object arg;
|
683
|
412 {
|
2094
|
413 double d = extract_float (arg);
|
|
414 IN_FLOAT (d = lgamma (d), "log-gamma", arg);
|
683
|
415 return make_float (d);
|
|
416 }
|
|
417
|
2094
|
418 DEFUN ("cube-root", Fcube_root, Scube_root, 1, 1, 0,
|
41001
|
419 doc: /* Return the cube root of ARG. */)
|
|
420 (arg)
|
2094
|
421 register Lisp_Object arg;
|
683
|
422 {
|
2094
|
423 double d = extract_float (arg);
|
|
424 #ifdef HAVE_CBRT
|
|
425 IN_FLOAT (d = cbrt (d), "cube-root", arg);
|
|
426 #else
|
|
427 if (d >= 0.0)
|
|
428 IN_FLOAT (d = pow (d, 1.0/3.0), "cube-root", arg);
|
|
429 else
|
|
430 IN_FLOAT (d = -pow (-d, 1.0/3.0), "cube-root", arg);
|
|
431 #endif
|
683
|
432 return make_float (d);
|
|
433 }
|
|
434
|
694
|
435 #endif
|
|
436
|
102
|
437 DEFUN ("exp", Fexp, Sexp, 1, 1, 0,
|
41001
|
438 doc: /* Return the exponential base e of ARG. */)
|
|
439 (arg)
|
2094
|
440 register Lisp_Object arg;
|
102
|
441 {
|
2094
|
442 double d = extract_float (arg);
|
|
443 #ifdef FLOAT_CHECK_DOMAIN
|
|
444 if (d > 709.7827) /* Assume IEEE doubles here */
|
|
445 range_error ("exp", arg);
|
|
446 else if (d < -709.0)
|
|
447 return make_float (0.0);
|
|
448 else
|
|
449 #endif
|
|
450 IN_FLOAT (d = exp (d), "exp", arg);
|
102
|
451 return make_float (d);
|
|
452 }
|
|
453
|
|
454 DEFUN ("expt", Fexpt, Sexpt, 2, 2, 0,
|
41001
|
455 doc: /* Return the exponential ARG1 ** ARG2. */)
|
|
456 (arg1, arg2)
|
2094
|
457 register Lisp_Object arg1, arg2;
|
102
|
458 {
|
|
459 double f1, f2;
|
|
460
|
40656
|
461 CHECK_NUMBER_OR_FLOAT (arg1);
|
|
462 CHECK_NUMBER_OR_FLOAT (arg2);
|
9129
b2d1d925d5cc
(extract_float, Fexpt, Fabs, Ffloat, Fceiling, Ffloor, Fround, Ftruncate): Use
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
463 if (INTEGERP (arg1) /* common lisp spec */
|
64810
|
464 && INTEGERP (arg2) /* don't promote, if both are ints, and */
|
|
465 && 0 <= XINT (arg2)) /* we are sure the result is not fractional */
|
102
|
466 { /* this can be improved by pre-calculating */
|
12537
|
467 EMACS_INT acc, x, y; /* some binary powers of x then accumulating */
|
3673
|
468 Lisp_Object val;
|
|
469
|
2094
|
470 x = XINT (arg1);
|
|
471 y = XINT (arg2);
|
102
|
472 acc = 1;
|
49600
|
473
|
102
|
474 if (y < 0)
|
|
475 {
|
2094
|
476 if (x == 1)
|
|
477 acc = 1;
|
|
478 else if (x == -1)
|
|
479 acc = (y & 1) ? -1 : 1;
|
|
480 else
|
|
481 acc = 0;
|
102
|
482 }
|
|
483 else
|
|
484 {
|
2094
|
485 while (y > 0)
|
|
486 {
|
|
487 if (y & 1)
|
|
488 acc *= x;
|
|
489 x *= x;
|
|
490 y = (unsigned)y >> 1;
|
|
491 }
|
102
|
492 }
|
9267
1b685d477c10
(FLOAT_TO_INT, FLOAT_TO_INT2, Fexpt, Flogb, Ffloor): Use new accessor macros
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
493 XSETINT (val, acc);
|
3673
|
494 return val;
|
102
|
495 }
|
25645
|
496 f1 = FLOATP (arg1) ? XFLOAT_DATA (arg1) : XINT (arg1);
|
|
497 f2 = FLOATP (arg2) ? XFLOAT_DATA (arg2) : XINT (arg2);
|
2094
|
498 /* Really should check for overflow, too */
|
|
499 if (f1 == 0.0 && f2 == 0.0)
|
|
500 f1 = 1.0;
|
|
501 #ifdef FLOAT_CHECK_DOMAIN
|
|
502 else if ((f1 == 0.0 && f2 < 0.0) || (f1 < 0 && f2 != floor(f2)))
|
|
503 domain_error2 ("expt", arg1, arg2);
|
|
504 #endif
|
4529
|
505 IN_FLOAT2 (f1 = pow (f1, f2), "expt", arg1, arg2);
|
102
|
506 return make_float (f1);
|
|
507 }
|
683
|
508
|
1005
|
509 DEFUN ("log", Flog, Slog, 1, 2, 0,
|
41001
|
510 doc: /* Return the natural logarithm of ARG.
|
67496
|
511 If the optional argument BASE is given, return log ARG using that base. */)
|
41001
|
512 (arg, base)
|
2094
|
513 register Lisp_Object arg, base;
|
683
|
514 {
|
2094
|
515 double d = extract_float (arg);
|
1005
|
516
|
2094
|
517 #ifdef FLOAT_CHECK_DOMAIN
|
|
518 if (d <= 0.0)
|
|
519 domain_error2 ("log", arg, base);
|
|
520 #endif
|
1005
|
521 if (NILP (base))
|
2094
|
522 IN_FLOAT (d = log (d), "log", arg);
|
1005
|
523 else
|
|
524 {
|
|
525 double b = extract_float (base);
|
|
526
|
2094
|
527 #ifdef FLOAT_CHECK_DOMAIN
|
|
528 if (b <= 0.0 || b == 1.0)
|
|
529 domain_error2 ("log", arg, base);
|
|
530 #endif
|
|
531 if (b == 10.0)
|
|
532 IN_FLOAT2 (d = log10 (d), "log", arg, base);
|
|
533 else
|
3407
|
534 IN_FLOAT2 (d = log (d) / log (b), "log", arg, base);
|
1005
|
535 }
|
683
|
536 return make_float (d);
|
|
537 }
|
|
538
|
|
539 DEFUN ("log10", Flog10, Slog10, 1, 1, 0,
|
41001
|
540 doc: /* Return the logarithm base 10 of ARG. */)
|
|
541 (arg)
|
2094
|
542 register Lisp_Object arg;
|
683
|
543 {
|
2094
|
544 double d = extract_float (arg);
|
|
545 #ifdef FLOAT_CHECK_DOMAIN
|
|
546 if (d <= 0.0)
|
|
547 domain_error ("log10", arg);
|
|
548 #endif
|
|
549 IN_FLOAT (d = log10 (d), "log10", arg);
|
683
|
550 return make_float (d);
|
|
551 }
|
|
552
|
|
553 DEFUN ("sqrt", Fsqrt, Ssqrt, 1, 1, 0,
|
41001
|
554 doc: /* Return the square root of ARG. */)
|
|
555 (arg)
|
2094
|
556 register Lisp_Object arg;
|
102
|
557 {
|
2094
|
558 double d = extract_float (arg);
|
|
559 #ifdef FLOAT_CHECK_DOMAIN
|
|
560 if (d < 0.0)
|
|
561 domain_error ("sqrt", arg);
|
|
562 #endif
|
|
563 IN_FLOAT (d = sqrt (d), "sqrt", arg);
|
683
|
564 return make_float (d);
|
|
565 }
|
|
566
|
694
|
567 #if 0 /* Not clearly worth adding. */
|
683
|
568
|
|
569 DEFUN ("acosh", Facosh, Sacosh, 1, 1, 0,
|
41001
|
570 doc: /* Return the inverse hyperbolic cosine of ARG. */)
|
|
571 (arg)
|
2094
|
572 register Lisp_Object arg;
|
683
|
573 {
|
2094
|
574 double d = extract_float (arg);
|
|
575 #ifdef FLOAT_CHECK_DOMAIN
|
|
576 if (d < 1.0)
|
|
577 domain_error ("acosh", arg);
|
|
578 #endif
|
|
579 #ifdef HAVE_INVERSE_HYPERBOLIC
|
|
580 IN_FLOAT (d = acosh (d), "acosh", arg);
|
|
581 #else
|
|
582 IN_FLOAT (d = log (d + sqrt (d*d - 1.0)), "acosh", arg);
|
|
583 #endif
|
683
|
584 return make_float (d);
|
|
585 }
|
|
586
|
|
587 DEFUN ("asinh", Fasinh, Sasinh, 1, 1, 0,
|
41001
|
588 doc: /* Return the inverse hyperbolic sine of ARG. */)
|
|
589 (arg)
|
2094
|
590 register Lisp_Object arg;
|
683
|
591 {
|
2094
|
592 double d = extract_float (arg);
|
|
593 #ifdef HAVE_INVERSE_HYPERBOLIC
|
|
594 IN_FLOAT (d = asinh (d), "asinh", arg);
|
|
595 #else
|
|
596 IN_FLOAT (d = log (d + sqrt (d*d + 1.0)), "asinh", arg);
|
|
597 #endif
|
683
|
598 return make_float (d);
|
|
599 }
|
|
600
|
|
601 DEFUN ("atanh", Fatanh, Satanh, 1, 1, 0,
|
41001
|
602 doc: /* Return the inverse hyperbolic tangent of ARG. */)
|
|
603 (arg)
|
2094
|
604 register Lisp_Object arg;
|
683
|
605 {
|
2094
|
606 double d = extract_float (arg);
|
|
607 #ifdef FLOAT_CHECK_DOMAIN
|
|
608 if (d >= 1.0 || d <= -1.0)
|
|
609 domain_error ("atanh", arg);
|
|
610 #endif
|
|
611 #ifdef HAVE_INVERSE_HYPERBOLIC
|
|
612 IN_FLOAT (d = atanh (d), "atanh", arg);
|
|
613 #else
|
|
614 IN_FLOAT (d = 0.5 * log ((1.0 + d) / (1.0 - d)), "atanh", arg);
|
|
615 #endif
|
683
|
616 return make_float (d);
|
|
617 }
|
|
618
|
|
619 DEFUN ("cosh", Fcosh, Scosh, 1, 1, 0,
|
41001
|
620 doc: /* Return the hyperbolic cosine of ARG. */)
|
|
621 (arg)
|
2094
|
622 register Lisp_Object arg;
|
683
|
623 {
|
2094
|
624 double d = extract_float (arg);
|
|
625 #ifdef FLOAT_CHECK_DOMAIN
|
|
626 if (d > 710.0 || d < -710.0)
|
|
627 range_error ("cosh", arg);
|
|
628 #endif
|
|
629 IN_FLOAT (d = cosh (d), "cosh", arg);
|
102
|
630 return make_float (d);
|
|
631 }
|
|
632
|
|
633 DEFUN ("sinh", Fsinh, Ssinh, 1, 1, 0,
|
41001
|
634 doc: /* Return the hyperbolic sine of ARG. */)
|
|
635 (arg)
|
2094
|
636 register Lisp_Object arg;
|
102
|
637 {
|
2094
|
638 double d = extract_float (arg);
|
|
639 #ifdef FLOAT_CHECK_DOMAIN
|
|
640 if (d > 710.0 || d < -710.0)
|
|
641 range_error ("sinh", arg);
|
|
642 #endif
|
|
643 IN_FLOAT (d = sinh (d), "sinh", arg);
|
102
|
644 return make_float (d);
|
|
645 }
|
|
646
|
|
647 DEFUN ("tanh", Ftanh, Stanh, 1, 1, 0,
|
41001
|
648 doc: /* Return the hyperbolic tangent of ARG. */)
|
|
649 (arg)
|
2094
|
650 register Lisp_Object arg;
|
102
|
651 {
|
2094
|
652 double d = extract_float (arg);
|
|
653 IN_FLOAT (d = tanh (d), "tanh", arg);
|
102
|
654 return make_float (d);
|
|
655 }
|
683
|
656 #endif
|
102
|
657
|
|
658 DEFUN ("abs", Fabs, Sabs, 1, 1, 0,
|
41001
|
659 doc: /* Return the absolute value of ARG. */)
|
|
660 (arg)
|
2094
|
661 register Lisp_Object arg;
|
102
|
662 {
|
40656
|
663 CHECK_NUMBER_OR_FLOAT (arg);
|
102
|
664
|
9129
b2d1d925d5cc
(extract_float, Fexpt, Fabs, Ffloat, Fceiling, Ffloor, Fround, Ftruncate): Use
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
665 if (FLOATP (arg))
|
25645
|
666 IN_FLOAT (arg = make_float (fabs (XFLOAT_DATA (arg))), "abs", arg);
|
2094
|
667 else if (XINT (arg) < 0)
|
9365
|
668 XSETINT (arg, - XINT (arg));
|
102
|
669
|
2094
|
670 return arg;
|
102
|
671 }
|
|
672
|
|
673 DEFUN ("float", Ffloat, Sfloat, 1, 1, 0,
|
41001
|
674 doc: /* Return the floating point number equal to ARG. */)
|
|
675 (arg)
|
2094
|
676 register Lisp_Object arg;
|
102
|
677 {
|
40656
|
678 CHECK_NUMBER_OR_FLOAT (arg);
|
102
|
679
|
9129
b2d1d925d5cc
(extract_float, Fexpt, Fabs, Ffloat, Fceiling, Ffloor, Fround, Ftruncate): Use
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
680 if (INTEGERP (arg))
|
2094
|
681 return make_float ((double) XINT (arg));
|
102
|
682 else /* give 'em the same float back */
|
2094
|
683 return arg;
|
102
|
684 }
|
|
685
|
|
686 DEFUN ("logb", Flogb, Slogb, 1, 1, 0,
|
41001
|
687 doc: /* Returns largest integer <= the base 2 log of the magnitude of ARG.
|
|
688 This is the same as the exponent of a float. */)
|
2094
|
689 (arg)
|
|
690 Lisp_Object arg;
|
102
|
691 {
|
2119
|
692 Lisp_Object val;
|
11265
|
693 EMACS_INT value;
|
2129
6741f5f8ed54
(Flogb): Fix arg names. Don't confuse Lisp_Object with integer.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
694 double f = extract_float (arg);
|
2119
|
695
|
6359
|
696 if (f == 0.0)
|
53002
|
697 value = MOST_NEGATIVE_FIXNUM;
|
6359
|
698 else
|
|
699 {
|
4501
|
700 #ifdef HAVE_LOGB
|
6359
|
701 IN_FLOAT (value = logb (f), "logb", arg);
|
4501
|
702 #else
|
|
703 #ifdef HAVE_FREXP
|
11859
|
704 int ivalue;
|
|
705 IN_FLOAT (frexp (f, &ivalue), "logb", arg);
|
|
706 value = ivalue - 1;
|
2205
|
707 #else
|
6359
|
708 int i;
|
|
709 double d;
|
|
710 if (f < 0.0)
|
|
711 f = -f;
|
|
712 value = -1;
|
|
713 while (f < 0.5)
|
|
714 {
|
|
715 for (i = 1, d = 0.5; d * d >= f; i += i)
|
|
716 d *= d;
|
|
717 f /= d;
|
|
718 value -= i;
|
|
719 }
|
|
720 while (f >= 1.0)
|
|
721 {
|
|
722 for (i = 1, d = 2.0; d * d <= f; i += i)
|
|
723 d *= d;
|
|
724 f /= d;
|
|
725 value += i;
|
|
726 }
|
4501
|
727 #endif
|
2205
|
728 #endif
|
6359
|
729 }
|
9267
1b685d477c10
(FLOAT_TO_INT, FLOAT_TO_INT2, Fexpt, Flogb, Ffloor): Use new accessor macros
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
730 XSETINT (val, value);
|
2119
|
731 return val;
|
102
|
732 }
|
|
733
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
734
|
16857
|
735 /* the rounding functions */
|
|
736
|
|
737 static Lisp_Object
|
|
738 rounding_driver (arg, divisor, double_round, int_round2, name)
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
739 register Lisp_Object arg, divisor;
|
16857
|
740 double (*double_round) ();
|
|
741 EMACS_INT (*int_round2) ();
|
|
742 char *name;
|
102
|
743 {
|
40656
|
744 CHECK_NUMBER_OR_FLOAT (arg);
|
102
|
745
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
746 if (! NILP (divisor))
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
747 {
|
12537
|
748 EMACS_INT i1, i2;
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
749
|
40656
|
750 CHECK_NUMBER_OR_FLOAT (divisor);
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
751
|
9129
b2d1d925d5cc
(extract_float, Fexpt, Fabs, Ffloat, Fceiling, Ffloor, Fround, Ftruncate): Use
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
752 if (FLOATP (arg) || FLOATP (divisor))
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
753 {
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
754 double f1, f2;
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
755
|
25645
|
756 f1 = FLOATP (arg) ? XFLOAT_DATA (arg) : XINT (arg);
|
|
757 f2 = (FLOATP (divisor) ? XFLOAT_DATA (divisor) : XINT (divisor));
|
16786
|
758 if (! IEEE_FLOATING_POINT && f2 == 0)
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
759 Fsignal (Qarith_error, Qnil);
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
760
|
16857
|
761 IN_FLOAT2 (f1 = (*double_round) (f1 / f2), name, arg, divisor);
|
|
762 FLOAT_TO_INT2 (f1, arg, name, arg, divisor);
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
763 return arg;
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
764 }
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
765
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
766 i1 = XINT (arg);
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
767 i2 = XINT (divisor);
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
768
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
769 if (i2 == 0)
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
770 Fsignal (Qarith_error, Qnil);
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
771
|
16857
|
772 XSETINT (arg, (*int_round2) (i1, i2));
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
773 return arg;
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
774 }
|
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
775
|
9129
b2d1d925d5cc
(extract_float, Fexpt, Fabs, Ffloat, Fceiling, Ffloor, Fround, Ftruncate): Use
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
776 if (FLOATP (arg))
|
6375
|
777 {
|
|
778 double d;
|
16857
|
779
|
25645
|
780 IN_FLOAT (d = (*double_round) (XFLOAT_DATA (arg)), name, arg);
|
16857
|
781 FLOAT_TO_INT (d, arg, name, arg);
|
6375
|
782 }
|
102
|
783
|
2094
|
784 return arg;
|
102
|
785 }
|
|
786
|
16857
|
787 /* With C's /, the result is implementation-defined if either operand
|
|
788 is negative, so take care with negative operands in the following
|
|
789 integer functions. */
|
|
790
|
|
791 static EMACS_INT
|
|
792 ceiling2 (i1, i2)
|
|
793 EMACS_INT i1, i2;
|
|
794 {
|
|
795 return (i2 < 0
|
|
796 ? (i1 < 0 ? ((-1 - i1) / -i2) + 1 : - (i1 / -i2))
|
|
797 : (i1 <= 0 ? - (-i1 / i2) : ((i1 - 1) / i2) + 1));
|
|
798 }
|
|
799
|
|
800 static EMACS_INT
|
|
801 floor2 (i1, i2)
|
|
802 EMACS_INT i1, i2;
|
|
803 {
|
|
804 return (i2 < 0
|
|
805 ? (i1 <= 0 ? -i1 / -i2 : -1 - ((i1 - 1) / -i2))
|
|
806 : (i1 < 0 ? -1 - ((-1 - i1) / i2) : i1 / i2));
|
|
807 }
|
|
808
|
|
809 static EMACS_INT
|
|
810 truncate2 (i1, i2)
|
|
811 EMACS_INT i1, i2;
|
|
812 {
|
|
813 return (i2 < 0
|
|
814 ? (i1 < 0 ? -i1 / -i2 : - (i1 / -i2))
|
|
815 : (i1 < 0 ? - (-i1 / i2) : i1 / i2));
|
|
816 }
|
|
817
|
|
818 static EMACS_INT
|
|
819 round2 (i1, i2)
|
|
820 EMACS_INT i1, i2;
|
|
821 {
|
|
822 /* The C language's division operator gives us one remainder R, but
|
|
823 we want the remainder R1 on the other side of 0 if R1 is closer
|
|
824 to 0 than R is; because we want to round to even, we also want R1
|
|
825 if R and R1 are the same distance from 0 and if C's quotient is
|
|
826 odd. */
|
|
827 EMACS_INT q = i1 / i2;
|
|
828 EMACS_INT r = i1 % i2;
|
|
829 EMACS_INT abs_r = r < 0 ? -r : r;
|
|
830 EMACS_INT abs_r1 = (i2 < 0 ? -i2 : i2) - abs_r;
|
|
831 return q + (abs_r + (q & 1) <= abs_r1 ? 0 : (i2 ^ r) < 0 ? -1 : 1);
|
|
832 }
|
|
833
|
19737
|
834 /* The code uses emacs_rint, so that it works to undefine HAVE_RINT
|
|
835 if `rint' exists but does not work right. */
|
|
836 #ifdef HAVE_RINT
|
|
837 #define emacs_rint rint
|
|
838 #else
|
16929
|
839 static double
|
19737
|
840 emacs_rint (d)
|
16929
|
841 double d;
|
|
842 {
|
16956
|
843 return floor (d + 0.5);
|
16929
|
844 }
|
|
845 #endif
|
|
846
|
16857
|
847 static double
|
|
848 double_identity (d)
|
|
849 double d;
|
|
850 {
|
|
851 return d;
|
|
852 }
|
|
853
|
|
854 DEFUN ("ceiling", Fceiling, Sceiling, 1, 2, 0,
|
41028
|
855 doc: /* Return the smallest integer no less than ARG.
|
|
856 This rounds the value towards +inf.
|
41001
|
857 With optional DIVISOR, return the smallest integer no less than ARG/DIVISOR. */)
|
|
858 (arg, divisor)
|
16857
|
859 Lisp_Object arg, divisor;
|
|
860 {
|
|
861 return rounding_driver (arg, divisor, ceil, ceiling2, "ceiling");
|
|
862 }
|
|
863
|
|
864 DEFUN ("floor", Ffloor, Sfloor, 1, 2, 0,
|
41028
|
865 doc: /* Return the largest integer no greater than ARG.
|
60651
|
866 This rounds the value towards -inf.
|
41001
|
867 With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR. */)
|
|
868 (arg, divisor)
|
16857
|
869 Lisp_Object arg, divisor;
|
|
870 {
|
|
871 return rounding_driver (arg, divisor, floor, floor2, "floor");
|
|
872 }
|
|
873
|
|
874 DEFUN ("round", Fround, Sround, 1, 2, 0,
|
41001
|
875 doc: /* Return the nearest integer to ARG.
|
43179
|
876 With optional DIVISOR, return the nearest integer to ARG/DIVISOR.
|
|
877
|
43181
|
878 Rounding a value equidistant between two integers may choose the
|
|
879 integer closer to zero, or it may prefer an even integer, depending on
|
|
880 your machine. For example, \(round 2.5\) can return 3 on some
|
43189
|
881 systems, but 2 on others. */)
|
41001
|
882 (arg, divisor)
|
16857
|
883 Lisp_Object arg, divisor;
|
|
884 {
|
19737
|
885 return rounding_driver (arg, divisor, emacs_rint, round2, "round");
|
16857
|
886 }
|
|
887
|
|
888 DEFUN ("truncate", Ftruncate, Struncate, 1, 2, 0,
|
41001
|
889 doc: /* Truncate a floating point number to an int.
|
|
890 Rounds ARG toward zero.
|
|
891 With optional DIVISOR, truncate ARG/DIVISOR. */)
|
|
892 (arg, divisor)
|
16857
|
893 Lisp_Object arg, divisor;
|
|
894 {
|
|
895 return rounding_driver (arg, divisor, double_identity, truncate2,
|
|
896 "truncate");
|
|
897 }
|
|
898
|
4506
6131dad14f6f
(Ffloor): Optional second operand specifies divisor, as in Common Lisp.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
899
|
16786
|
900 Lisp_Object
|
|
901 fmod_float (x, y)
|
|
902 register Lisp_Object x, y;
|
|
903 {
|
|
904 double f1, f2;
|
|
905
|
25645
|
906 f1 = FLOATP (x) ? XFLOAT_DATA (x) : XINT (x);
|
|
907 f2 = FLOATP (y) ? XFLOAT_DATA (y) : XINT (y);
|
16786
|
908
|
|
909 if (! IEEE_FLOATING_POINT && f2 == 0)
|
|
910 Fsignal (Qarith_error, Qnil);
|
|
911
|
|
912 /* If the "remainder" comes out with the wrong sign, fix it. */
|
|
913 IN_FLOAT2 ((f1 = fmod (f1, f2),
|
|
914 f1 = (f2 < 0 ? f1 > 0 : f1 < 0) ? f1 + f2 : f1),
|
|
915 "mod", x, y);
|
|
916 return make_float (f1);
|
|
917 }
|
102
|
918
|
2094
|
919 /* It's not clear these are worth adding. */
|
|
920
|
|
921 DEFUN ("fceiling", Ffceiling, Sfceiling, 1, 1, 0,
|
41001
|
922 doc: /* Return the smallest integer no less than ARG, as a float.
|
|
923 \(Round toward +inf.\) */)
|
|
924 (arg)
|
2094
|
925 register Lisp_Object arg;
|
|
926 {
|
|
927 double d = extract_float (arg);
|
|
928 IN_FLOAT (d = ceil (d), "fceiling", arg);
|
|
929 return make_float (d);
|
|
930 }
|
|
931
|
|
932 DEFUN ("ffloor", Fffloor, Sffloor, 1, 1, 0,
|
41001
|
933 doc: /* Return the largest integer no greater than ARG, as a float.
|
|
934 \(Round towards -inf.\) */)
|
|
935 (arg)
|
2094
|
936 register Lisp_Object arg;
|
|
937 {
|
|
938 double d = extract_float (arg);
|
|
939 IN_FLOAT (d = floor (d), "ffloor", arg);
|
|
940 return make_float (d);
|
|
941 }
|
|
942
|
|
943 DEFUN ("fround", Ffround, Sfround, 1, 1, 0,
|
41001
|
944 doc: /* Return the nearest integer to ARG, as a float. */)
|
|
945 (arg)
|
2094
|
946 register Lisp_Object arg;
|
|
947 {
|
|
948 double d = extract_float (arg);
|
19737
|
949 IN_FLOAT (d = emacs_rint (d), "fround", arg);
|
2094
|
950 return make_float (d);
|
|
951 }
|
|
952
|
|
953 DEFUN ("ftruncate", Fftruncate, Sftruncate, 1, 1, 0,
|
41001
|
954 doc: /* Truncate a floating point number to an integral float value.
|
|
955 Rounds the value toward zero. */)
|
|
956 (arg)
|
2094
|
957 register Lisp_Object arg;
|
|
958 {
|
|
959 double d = extract_float (arg);
|
|
960 if (d >= 0.0)
|
|
961 IN_FLOAT (d = floor (d), "ftruncate", arg);
|
|
962 else
|
5495
|
963 IN_FLOAT (d = ceil (d), "ftruncate", arg);
|
2094
|
964 return make_float (d);
|
|
965 }
|
|
966
|
|
967 #ifdef FLOAT_CATCH_SIGILL
|
621
|
968 static SIGTYPE
|
102
|
969 float_error (signo)
|
|
970 int signo;
|
|
971 {
|
|
972 if (! in_float)
|
|
973 fatal_error_signal (signo);
|
|
974
|
16220
|
975 #ifdef BSD_SYSTEM
|
102
|
976 #ifdef BSD4_1
|
|
977 sigrelse (SIGILL);
|
|
978 #else /* not BSD4_1 */
|
638
|
979 sigsetmask (SIGEMPTYMASK);
|
102
|
980 #endif /* not BSD4_1 */
|
485
|
981 #else
|
|
982 /* Must reestablish handler each time it is called. */
|
|
983 signal (SIGILL, float_error);
|
16220
|
984 #endif /* BSD_SYSTEM */
|
102
|
985
|
58986
|
986 SIGNAL_THREAD_CHECK (signo);
|
102
|
987 in_float = 0;
|
|
988
|
|
989 Fsignal (Qarith_error, Fcons (float_error_arg, Qnil));
|
|
990 }
|
|
991
|
2094
|
992 /* Another idea was to replace the library function `infnan'
|
|
993 where SIGILL is signaled. */
|
|
994
|
|
995 #endif /* FLOAT_CATCH_SIGILL */
|
|
996
|
|
997 #ifdef HAVE_MATHERR
|
49600
|
998 int
|
2094
|
999 matherr (x)
|
|
1000 struct exception *x;
|
|
1001 {
|
|
1002 Lisp_Object args;
|
|
1003 if (! in_float)
|
|
1004 /* Not called from emacs-lisp float routines; do the default thing. */
|
|
1005 return 0;
|
|
1006 if (!strcmp (x->name, "pow"))
|
|
1007 x->name = "expt";
|
|
1008
|
|
1009 args
|
|
1010 = Fcons (build_string (x->name),
|
|
1011 Fcons (make_float (x->arg1),
|
|
1012 ((!strcmp (x->name, "log") || !strcmp (x->name, "pow"))
|
|
1013 ? Fcons (make_float (x->arg2), Qnil)
|
|
1014 : Qnil)));
|
|
1015 switch (x->type)
|
|
1016 {
|
|
1017 case DOMAIN: Fsignal (Qdomain_error, args); break;
|
|
1018 case SING: Fsignal (Qsingularity_error, args); break;
|
|
1019 case OVERFLOW: Fsignal (Qoverflow_error, args); break;
|
|
1020 case UNDERFLOW: Fsignal (Qunderflow_error, args); break;
|
|
1021 default: Fsignal (Qarith_error, args); break;
|
|
1022 }
|
|
1023 return (1); /* don't set errno or print a message */
|
|
1024 }
|
|
1025 #endif /* HAVE_MATHERR */
|
|
1026
|
21514
|
1027 void
|
102
|
1028 init_floatfns ()
|
|
1029 {
|
2094
|
1030 #ifdef FLOAT_CATCH_SIGILL
|
102
|
1031 signal (SIGILL, float_error);
|
49600
|
1032 #endif
|
102
|
1033 in_float = 0;
|
|
1034 }
|
|
1035
|
21514
|
1036 void
|
102
|
1037 syms_of_floatfns ()
|
|
1038 {
|
|
1039 defsubr (&Sacos);
|
683
|
1040 defsubr (&Sasin);
|
|
1041 defsubr (&Satan);
|
|
1042 defsubr (&Scos);
|
|
1043 defsubr (&Ssin);
|
|
1044 defsubr (&Stan);
|
|
1045 #if 0
|
102
|
1046 defsubr (&Sacosh);
|
|
1047 defsubr (&Sasinh);
|
|
1048 defsubr (&Satanh);
|
683
|
1049 defsubr (&Scosh);
|
|
1050 defsubr (&Ssinh);
|
|
1051 defsubr (&Stanh);
|
102
|
1052 defsubr (&Sbessel_y0);
|
|
1053 defsubr (&Sbessel_y1);
|
|
1054 defsubr (&Sbessel_yn);
|
|
1055 defsubr (&Sbessel_j0);
|
|
1056 defsubr (&Sbessel_j1);
|
|
1057 defsubr (&Sbessel_jn);
|
|
1058 defsubr (&Serf);
|
|
1059 defsubr (&Serfc);
|
683
|
1060 defsubr (&Slog_gamma);
|
2094
|
1061 defsubr (&Scube_root);
|
5595
|
1062 #endif
|
2094
|
1063 defsubr (&Sfceiling);
|
|
1064 defsubr (&Sffloor);
|
|
1065 defsubr (&Sfround);
|
|
1066 defsubr (&Sftruncate);
|
102
|
1067 defsubr (&Sexp);
|
683
|
1068 defsubr (&Sexpt);
|
102
|
1069 defsubr (&Slog);
|
|
1070 defsubr (&Slog10);
|
|
1071 defsubr (&Ssqrt);
|
|
1072
|
|
1073 defsubr (&Sabs);
|
|
1074 defsubr (&Sfloat);
|
|
1075 defsubr (&Slogb);
|
|
1076 defsubr (&Sceiling);
|
16857
|
1077 defsubr (&Sfloor);
|
102
|
1078 defsubr (&Sround);
|
|
1079 defsubr (&Struncate);
|
|
1080 }
|
52401
|
1081
|
|
1082 /* arch-tag: be05bf9d-049e-4e31-91b9-e6153d483ae7
|
|
1083 (do not change this comment) */
|