gsl_sys__fcmp.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* sys/gsl_compare.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. * Based on fcmp 1.2.2 Copyright (c) 1998-2000 Theodore C. Belding
  20. * University of Michigan Center for the Study of Complex Systems
  21. * Ted.Belding@umich.edu
  22. *
  23. */
  24. #include "gsl__config.h"
  25. #include "gsl_sys.h"
  26. #include <math.h>
  27. int
  28. gsl_fcmp (const double x1, const double x2, const double epsilon)
  29. {
  30. int exponent;
  31. double delta, difference;
  32. /* Find exponent of largest absolute value */
  33. {
  34. double max = (fabs (x1) > fabs (x2)) ? x1 : x2;
  35. frexp (max, &exponent);
  36. }
  37. /* Form a neighborhood of size 2 * delta */
  38. delta = ldexp (epsilon, exponent);
  39. difference = x1 - x2;
  40. if (difference > delta) /* x1 > x2 */
  41. {
  42. return 1;
  43. }
  44. else if (difference < -delta) /* x1 < x2 */
  45. {
  46. return -1;
  47. }
  48. else /* -delta <= difference <= delta */
  49. {
  50. return 0; /* x1 ~=~ x2 */
  51. }
  52. }