floatfns.c 13.4 KB
Newer Older
Mike Rowan's avatar
Mike Rowan committed
1
/* Primitive operations on floating point for GNU Emacs Lisp interpreter.
Jim Blandy's avatar
Jim Blandy committed
2
   Copyright (C) 1988, 1992 Free Software Foundation, Inc.
Mike Rowan's avatar
Mike Rowan committed
3 4 5 6 7

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Jim Blandy's avatar
Jim Blandy committed
8
the Free Software Foundation; either version 2, or (at your option)
Mike Rowan's avatar
Mike Rowan committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
any later version.

GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */


#include <signal.h>

#include "config.h"
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
25
#include "syssignal.h"
Mike Rowan's avatar
Mike Rowan committed
26 27 28 29

Lisp_Object Qarith_error;

#ifdef LISP_FLOAT_TYPE
Jim Blandy's avatar
Jim Blandy committed
30

Mike Rowan's avatar
Mike Rowan committed
31
#include <math.h>
Jim Blandy's avatar
Jim Blandy committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <errno.h>

extern int errno;

/* Avoid traps on VMS from sinh and cosh.
   All the other functions set errno instead.  */

#ifdef VMS
#undef cosh
#undef sinh
#define cosh(x) ((exp(x)+exp(-x))*0.5)
#define sinh(x) ((exp(x)-exp(-x))*0.5)
#endif /* VMS */

Jim Blandy's avatar
Jim Blandy committed
46
static SIGTYPE float_error ();
Mike Rowan's avatar
Mike Rowan committed
47 48 49 50 51 52 53

/* Nonzero while executing in floating point.
   This tells float_error what to do.  */

static int in_float;

/* If an argument is out of range for a mathematical function,
Jim Blandy's avatar
Jim Blandy committed
54
   here is the actual argument value to use in the error message.  */
Mike Rowan's avatar
Mike Rowan committed
55 56 57

static Lisp_Object float_error_arg;

Jim Blandy's avatar
Jim Blandy committed
58 59 60
/* Evaluate the floating point expression D, recording NUM
   as the original argument for error messages.
   D is normally an assignment expression.
61 62 63 64 65
   Handle errors which may result in signals or may set errno.

   Note that float_error may be declared to return void, so you can't
   just cast the zero after the colon to (SIGTYPE) to make the types
   check properly.  */
Jim Blandy's avatar
Jim Blandy committed
66 67

#define IN_FLOAT(D, NUM) \
Jim Blandy's avatar
Jim Blandy committed
68
(in_float = 1, errno = 0, float_error_arg = NUM, (D),			\
69
 (errno == ERANGE || errno == EDOM ? (float_error (),0) : 0),		\
Jim Blandy's avatar
Jim Blandy committed
70
 in_float = 0)
Mike Rowan's avatar
Mike Rowan committed
71 72 73 74 75 76 77 78 79 80 81 82 83

/* Extract a Lisp number as a `double', or signal an error.  */

double
extract_float (num)
     Lisp_Object num;
{
  CHECK_NUMBER_OR_FLOAT (num, 0);

  if (XTYPE (num) == Lisp_Float)
    return XFLOAT (num)->data;
  return (double) XINT (num);
}
84 85

/* Trig functions.  */
Mike Rowan's avatar
Mike Rowan committed
86 87 88 89 90 91 92 93 94 95 96

DEFUN ("acos", Facos, Sacos, 1, 1, 0,
  "Return the inverse cosine of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = acos (d), num);
  return make_float (d);
}

97 98
DEFUN ("asin", Fasin, Sasin, 1, 1, 0,
  "Return the inverse sine of ARG.")
Mike Rowan's avatar
Mike Rowan committed
99 100 101 102
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
103
  IN_FLOAT (d = asin (d), num);
Mike Rowan's avatar
Mike Rowan committed
104 105 106
  return make_float (d);
}

107 108
DEFUN ("atan", Fatan, Satan, 1, 1, 0,
  "Return the inverse tangent of ARG.")
Mike Rowan's avatar
Mike Rowan committed
109 110 111 112
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
113
  IN_FLOAT (d = atan (d), num);
Mike Rowan's avatar
Mike Rowan committed
114 115 116
  return make_float (d);
}

117 118
DEFUN ("cos", Fcos, Scos, 1, 1, 0,
  "Return the cosine of ARG.")
Mike Rowan's avatar
Mike Rowan committed
119 120 121 122
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
123
  IN_FLOAT (d = cos (d), num);
Mike Rowan's avatar
Mike Rowan committed
124 125 126
  return make_float (d);
}

127 128
DEFUN ("sin", Fsin, Ssin, 1, 1, 0,
  "Return the sine of ARG.")
Mike Rowan's avatar
Mike Rowan committed
129 130 131 132
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
133
  IN_FLOAT (d = sin (d), num);
Mike Rowan's avatar
Mike Rowan committed
134 135 136
  return make_float (d);
}

137 138
DEFUN ("tan", Ftan, Stan, 1, 1, 0,
  "Return the tangent of ARG.")
Mike Rowan's avatar
Mike Rowan committed
139 140 141 142
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
143
  IN_FLOAT (d = tan (d), num);
Mike Rowan's avatar
Mike Rowan committed
144 145 146
  return make_float (d);
}

147 148
#if 0 /* Leave these out unless we find there's a reason for them.  */

Mike Rowan's avatar
Mike Rowan committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
DEFUN ("bessel-j0", Fbessel_j0, Sbessel_j0, 1, 1, 0,
  "Return the bessel function j0 of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = j0 (d), num);
  return make_float (d);
}

DEFUN ("bessel-j1", Fbessel_j1, Sbessel_j1, 1, 1, 0,
  "Return the bessel function j1 of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = j1 (d), num);
  return make_float (d);
}

DEFUN ("bessel-jn", Fbessel_jn, Sbessel_jn, 2, 2, 0,
  "Return the order N bessel function output jn of ARG.\n\
The first arg (the order) is truncated to an integer.")
  (num1, num2)
     register Lisp_Object num1, num2;
{
  int i1 = extract_float (num1);
  double f2 = extract_float (num2);

  IN_FLOAT (f2 = jn (i1, f2), num1);
  return make_float (f2);
}

DEFUN ("bessel-y0", Fbessel_y0, Sbessel_y0, 1, 1, 0,
  "Return the bessel function y0 of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = y0 (d), num);
  return make_float (d);
}

DEFUN ("bessel-y1", Fbessel_y1, Sbessel_y1, 1, 1, 0,
  "Return the bessel function y1 of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = y1 (d), num);
  return make_float (d);
}

DEFUN ("bessel-yn", Fbessel_yn, Sbessel_yn, 2, 2, 0,
  "Return the order N bessel function output yn of ARG.\n\
The first arg (the order) is truncated to an integer.")
  (num1, num2)
     register Lisp_Object num1, num2;
{
  int i1 = extract_float (num1);
  double f2 = extract_float (num2);

  IN_FLOAT (f2 = yn (i1, f2), num1);
  return make_float (f2);
}

215 216 217
#endif

#if 0 /* Leave these out unless we see they are worth having.  */
Mike Rowan's avatar
Mike Rowan committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

DEFUN ("erf", Ferf, Serf, 1, 1, 0,
  "Return the mathematical error function of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = erf (d), num);
  return make_float (d);
}

DEFUN ("erfc", Ferfc, Serfc, 1, 1, 0,
  "Return the complementary error function of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = erfc (d), num);
  return make_float (d);
}

DEFUN ("log-gamma", Flog_gamma, Slog_gamma, 1, 1, 0,
  "Return the log gamma of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = lgamma (d), num);
  return make_float (d);
}

Richard M. Stallman's avatar
Richard M. Stallman committed
249
DEFUN ("cbrt", Fcbrt, Scbrt, 1, 1, 0,
250
  "Return the cube root of ARG.")
Mike Rowan's avatar
Mike Rowan committed
251 252 253 254
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
255
  IN_FLOAT (d = cbrt (d), num);
Mike Rowan's avatar
Mike Rowan committed
256 257 258
  return make_float (d);
}

Richard M. Stallman's avatar
Richard M. Stallman committed
259 260
#endif

261 262
DEFUN ("exp", Fexp, Sexp, 1, 1, 0,
  "Return the exponential base e of ARG.")
Mike Rowan's avatar
Mike Rowan committed
263 264 265 266
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
267
  IN_FLOAT (d = exp (d), num);
Mike Rowan's avatar
Mike Rowan committed
268 269 270 271
  return make_float (d);
}

DEFUN ("expt", Fexpt, Sexpt, 2, 2, 0,
272
  "Return the exponential X ** Y.")
Mike Rowan's avatar
Mike Rowan committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  (num1, num2)
     register Lisp_Object num1, num2;
{
  double f1, f2;

  CHECK_NUMBER_OR_FLOAT (num1, 0);
  CHECK_NUMBER_OR_FLOAT (num2, 0);
  if ((XTYPE (num1) == Lisp_Int) && /* common lisp spec */
      (XTYPE (num2) == Lisp_Int)) /* don't promote, if both are ints */
    {				/* this can be improved by pre-calculating */
      int acc, x, y;		/* some binary powers of x then acumulating */
      /* these, therby saving some time. -wsr */
      x = XINT (num1);
      y = XINT (num2);
      acc = 1;
      
      if (y < 0)
	{
	  for (; y < 0; y++)
	    acc /= x;
	}
      else
	{
	  for (; y > 0; y--)
	    acc *= x;
	}
299 300
      XFASTINT (x) = acc;
      return x;
Mike Rowan's avatar
Mike Rowan committed
301 302 303 304 305 306
    }
  f1 = (XTYPE (num1) == Lisp_Float) ? XFLOAT (num1)->data : XINT (num1);
  f2 = (XTYPE (num2) == Lisp_Float) ? XFLOAT (num2)->data : XINT (num2);
  IN_FLOAT (f1 = pow (f1, f2), num1);
  return make_float (f1);
}
307

308
DEFUN ("log", Flog, Slog, 1, 2, 0,
309
  "Return the natural logarithm of NUM.\n\
310 311
If second optional argument BASE is given, return log NUM using that base.")
  (num, base)
312
     register Lisp_Object num, base;
Mike Rowan's avatar
Mike Rowan committed
313 314
{
  double d = extract_float (num);
315 316 317 318 319 320 321 322 323

  if (NILP (base))
    IN_FLOAT (d = log (d), num);
  else
    {
      double b = extract_float (base);

      IN_FLOAT (d = log (num) / log (b), num);
    }
Mike Rowan's avatar
Mike Rowan committed
324 325 326
  return make_float (d);
}

327 328
DEFUN ("log10", Flog10, Slog10, 1, 1, 0,
  "Return the logarithm base 10 of ARG.")
Mike Rowan's avatar
Mike Rowan committed
329 330 331 332
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
333 334 335 336
  IN_FLOAT (d = log10 (d), num);
  return make_float (d);
}

Mike Rowan's avatar
Mike Rowan committed
337 338 339 340 341 342 343 344 345
DEFUN ("sqrt", Fsqrt, Ssqrt, 1, 1, 0,
  "Return the square root of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = sqrt (d), num);
  return make_float (d);
}
346

Richard M. Stallman's avatar
Richard M. Stallman committed
347
#if 0 /* Not clearly worth adding.  */
Mike Rowan's avatar
Mike Rowan committed
348

349 350
DEFUN ("acosh", Facosh, Sacosh, 1, 1, 0,
  "Return the inverse hyperbolic cosine of ARG.")
Mike Rowan's avatar
Mike Rowan committed
351 352 353 354
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
  IN_FLOAT (d = acosh (d), num);
  return make_float (d);
}

DEFUN ("asinh", Fasinh, Sasinh, 1, 1, 0,
  "Return the inverse hyperbolic sine of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = asinh (d), num);
  return make_float (d);
}

DEFUN ("atanh", Fatanh, Satanh, 1, 1, 0,
  "Return the inverse hyperbolic tangent of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = atanh (d), num);
  return make_float (d);
}

DEFUN ("cosh", Fcosh, Scosh, 1, 1, 0,
  "Return the hyperbolic cosine of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = cosh (d), num);
  return make_float (d);
}

DEFUN ("sinh", Fsinh, Ssinh, 1, 1, 0,
  "Return the hyperbolic sine of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = sinh (d), num);
Mike Rowan's avatar
Mike Rowan committed
396 397 398 399 400 401 402 403 404 405 406 407
  return make_float (d);
}

DEFUN ("tanh", Ftanh, Stanh, 1, 1, 0,
  "Return the hyperbolic tangent of ARG.")
  (num)
     register Lisp_Object num;
{
  double d = extract_float (num);
  IN_FLOAT (d = tanh (d), num);
  return make_float (d);
}
408
#endif
Mike Rowan's avatar
Mike Rowan committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

DEFUN ("abs", Fabs, Sabs, 1, 1, 0,
  "Return the absolute value of ARG.")
  (num)
     register Lisp_Object num;
{
  CHECK_NUMBER_OR_FLOAT (num, 0);

  if (XTYPE (num) == Lisp_Float)
    IN_FLOAT (num = make_float (fabs (XFLOAT (num)->data)), num);
  else if (XINT (num) < 0)
    XSETINT (num, - XFASTINT (num));

  return num;
}

DEFUN ("float", Ffloat, Sfloat, 1, 1, 0,
  "Return the floating point number equal to ARG.")
  (num)
     register Lisp_Object num;
{
  CHECK_NUMBER_OR_FLOAT (num, 0);

  if (XTYPE (num) == Lisp_Int)
    return make_float ((double) XINT (num));
  else				/* give 'em the same float back */
    return num;
}

DEFUN ("logb", Flogb, Slogb, 1, 1, 0,
  "Returns the integer that is the base 2 log of ARG.\n\
This is the same as the exponent of a float.")
     (num)
Lisp_Object num;
{
444 445 446
  /* System V apparently doesn't have a `logb' function.  It might be
     better to use it on systems that have it, but Ultrix (at least)
     doesn't declare it properly in <math.h>; does anyone really care? */
447
  return Flog (num, make_number (2));
Mike Rowan's avatar
Mike Rowan committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
}

/* the rounding functions  */

DEFUN ("ceiling", Fceiling, Sceiling, 1, 1, 0,
  "Return the smallest integer no less than ARG.  (Round toward +inf.)")
  (num)
     register Lisp_Object num;
{
  CHECK_NUMBER_OR_FLOAT (num, 0);

  if (XTYPE (num) == Lisp_Float)
    IN_FLOAT (XSET (num, Lisp_Int, ceil (XFLOAT (num)->data)), num);

  return num;
}

DEFUN ("floor", Ffloor, Sfloor, 1, 1, 0,
  "Return the largest integer no greater than ARG.  (Round towards -inf.)")
  (num)
     register Lisp_Object num;
{
  CHECK_NUMBER_OR_FLOAT (num, 0);

  if (XTYPE (num) == Lisp_Float)
    IN_FLOAT (XSET (num, Lisp_Int, floor (XFLOAT (num)->data)), num);

  return num;
}

DEFUN ("round", Fround, Sround, 1, 1, 0,
  "Return the nearest integer to ARG.")
  (num)
     register Lisp_Object num;
{
  CHECK_NUMBER_OR_FLOAT (num, 0);

  if (XTYPE (num) == Lisp_Float)
486 487 488
    {
      /* Screw the prevailing rounding mode.  */
      IN_FLOAT (XSET (num, Lisp_Int, floor (XFLOAT (num)->data + 0.5)), num);
489 490 491 492 493 494

      /* It used to be that on non-USG systems we'd use the `rint'
	 function.  But that seems not to be declared properly in
	 <math.h> on Ultrix, I don't want to declare it myself because
	 that might conflict with <math.h> on other systems, and I
	 don't see what's wrong with the code above anyway.  */
495
    }
Mike Rowan's avatar
Mike Rowan committed
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

  return num;
}

DEFUN ("truncate", Ftruncate, Struncate, 1, 1, 0,
       "Truncate a floating point number to an int.\n\
Rounds the value toward zero.")
  (num)
     register Lisp_Object num;
{
  CHECK_NUMBER_OR_FLOAT (num, 0);

  if (XTYPE (num) == Lisp_Float)
    XSET (num, Lisp_Int, (int) XFLOAT (num)->data);

  return num;
}

Jim Blandy's avatar
Jim Blandy committed
514
static SIGTYPE
Mike Rowan's avatar
Mike Rowan committed
515 516 517 518 519 520
float_error (signo)
     int signo;
{
  if (! in_float)
    fatal_error_signal (signo);

Jim Blandy's avatar
Jim Blandy committed
521
#ifdef BSD
Mike Rowan's avatar
Mike Rowan committed
522 523 524
#ifdef BSD4_1
  sigrelse (SIGILL);
#else /* not BSD4_1 */
Jim Blandy's avatar
Jim Blandy committed
525
  sigsetmask (SIGEMPTYMASK);
Mike Rowan's avatar
Mike Rowan committed
526
#endif /* not BSD4_1 */
Jim Blandy's avatar
Jim Blandy committed
527 528 529 530
#else
  /* Must reestablish handler each time it is called.  */
  signal (SIGILL, float_error);
#endif /* BSD */
Mike Rowan's avatar
Mike Rowan committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

  in_float = 0;

  Fsignal (Qarith_error, Fcons (float_error_arg, Qnil));
}

init_floatfns ()
{
  signal (SIGILL, float_error);
  in_float = 0;
}

syms_of_floatfns ()
{
  defsubr (&Sacos);
  defsubr (&Sasin);
  defsubr (&Satan);
548 549 550 551 552 553
  defsubr (&Scos);
  defsubr (&Ssin);
  defsubr (&Stan);
#if 0
  defsubr (&Sacosh);
  defsubr (&Sasinh);
Mike Rowan's avatar
Mike Rowan committed
554
  defsubr (&Satanh);
555 556 557
  defsubr (&Scosh);
  defsubr (&Ssinh);
  defsubr (&Stanh);
Mike Rowan's avatar
Mike Rowan committed
558 559 560 561 562 563 564 565
  defsubr (&Sbessel_y0);
  defsubr (&Sbessel_y1);
  defsubr (&Sbessel_yn);
  defsubr (&Sbessel_j0);
  defsubr (&Sbessel_j1);
  defsubr (&Sbessel_jn);
  defsubr (&Serf);
  defsubr (&Serfc);
566
  defsubr (&Slog_gamma);
Richard M. Stallman's avatar
Richard M. Stallman committed
567
  defsubr (&Scbrt);
568
#endif
Mike Rowan's avatar
Mike Rowan committed
569
  defsubr (&Sexp);
570
  defsubr (&Sexpt);
Mike Rowan's avatar
Mike Rowan committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
  defsubr (&Slog);
  defsubr (&Slog10);
  defsubr (&Ssqrt);

  defsubr (&Sabs);
  defsubr (&Sfloat);
  defsubr (&Slogb);
  defsubr (&Sceiling);
  defsubr (&Sfloor);
  defsubr (&Sround);
  defsubr (&Struncate);
}

#else /* not LISP_FLOAT_TYPE */

init_floatfns ()
{}

syms_of_floatfns ()
{}

#endif /* not LISP_FLOAT_TYPE */