gsl_sys__ldfrexp.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* sys/ldfrexp.c
  2. *
  3. * Copyright (C) 2002, Gert Van den Eynde
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <math.h>
  21. #include "gsl_math.h"
  22. double
  23. gsl_ldexp (const double x, const int e)
  24. {
  25. double p2 = pow (2.0, (double)e);
  26. return x * p2;
  27. }
  28. double
  29. gsl_frexp (const double x, int *e)
  30. {
  31. if (x == 0.0)
  32. {
  33. *e = 0;
  34. return 0.0;
  35. }
  36. else
  37. {
  38. double ex = ceil (log (fabs (x)) / M_LN2);
  39. int ei = (int) ex;
  40. double f = gsl_ldexp (x, -ei);
  41. while (fabs (f) >= 1.0)
  42. {
  43. ei++;
  44. f /= 2.0;
  45. }
  46. while (fabs (f) < 0.5)
  47. {
  48. ei--;
  49. f *= 2.0;
  50. }
  51. *e = ei;
  52. return f;
  53. }
  54. }