pow.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* $Id$ */
  8. #include <math.h>
  9. #include <errno.h>
  10. extern int errno;
  11. extern double modf(), exp(), log();
  12. double
  13. pow(x,y)
  14. double x,y;
  15. {
  16. /* Simple version for now. The Cody and Waite book has
  17. a very complicated, much more precise version, but
  18. this version has machine-dependant arrays A1 and A2,
  19. and I don't know yet how to solve this ???
  20. */
  21. double dummy;
  22. int result_neg = 0;
  23. if ((x == 0 && y == 0) ||
  24. (x < 0 && modf(y, &dummy) != 0)) {
  25. errno = EDOM;
  26. return 0;
  27. }
  28. if (x == 0) return x;
  29. if (x < 0) {
  30. if (modf(y/2.0, &dummy) != 0) {
  31. /* y was odd */
  32. result_neg = 1;
  33. }
  34. x = -x;
  35. }
  36. x = log(x);
  37. if (x < 0) {
  38. x = -x;
  39. y = -y;
  40. }
  41. if (y > M_LN_MAX_D/x) {
  42. errno = ERANGE;
  43. return 0;
  44. }
  45. if (y < M_LN_MIN_D/x) {
  46. errno = ERANGE;
  47. return 0;
  48. }
  49. x = exp(x * y);
  50. return result_neg ? -x : x;
  51. }