gsl_ieee-utils__print.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* ieee-utils/print.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  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 <stdio.h>
  21. #include <math.h>
  22. #include "gsl_ieee_utils.h"
  23. /* A table of sign characters, 0=positive, 1=negative. We print a space
  24. instead of a unary + sign for compatibility with bc */
  25. static char signs[2]={' ','-'} ;
  26. void
  27. gsl_ieee_fprintf_float (FILE * stream, const float * x) {
  28. gsl_ieee_float_rep r ;
  29. gsl_ieee_float_to_rep(x, &r) ;
  30. switch (r.type)
  31. {
  32. case GSL_IEEE_TYPE_NAN:
  33. fprintf(stream, "NaN") ;
  34. break ;
  35. case GSL_IEEE_TYPE_INF:
  36. fprintf(stream, "%cInf", signs[r.sign]) ;
  37. break ;
  38. case GSL_IEEE_TYPE_NORMAL:
  39. fprintf(stream, "%c1.%s*2^%d", signs[r.sign], r.mantissa, r.exponent) ;
  40. break ;
  41. case GSL_IEEE_TYPE_DENORMAL:
  42. fprintf(stream, "%c0.%s*2^%d", signs[r.sign], r.mantissa, r.exponent + 1) ;
  43. break ;
  44. case GSL_IEEE_TYPE_ZERO:
  45. fprintf(stream, "%c0", signs[r.sign]) ;
  46. break ;
  47. default:
  48. fprintf(stream, "[non-standard IEEE float]") ;
  49. }
  50. }
  51. void
  52. gsl_ieee_printf_float (const float * x)
  53. {
  54. gsl_ieee_fprintf_float (stdout,x);
  55. }
  56. void
  57. gsl_ieee_fprintf_double (FILE * stream, const double * x) {
  58. gsl_ieee_double_rep r ;
  59. gsl_ieee_double_to_rep (x, &r) ;
  60. switch (r.type)
  61. {
  62. case GSL_IEEE_TYPE_NAN:
  63. fprintf(stream, "NaN") ;
  64. break ;
  65. case GSL_IEEE_TYPE_INF:
  66. fprintf(stream, "%cInf", signs[r.sign]) ;
  67. break ;
  68. case GSL_IEEE_TYPE_NORMAL:
  69. fprintf(stream, "%c1.%s*2^%d", signs[r.sign], r.mantissa, r.exponent) ;
  70. break ;
  71. case GSL_IEEE_TYPE_DENORMAL:
  72. fprintf(stream, "%c0.%s*2^%d", signs[r.sign], r.mantissa, r.exponent + 1) ;
  73. break ;
  74. case GSL_IEEE_TYPE_ZERO:
  75. fprintf(stream, "%c0", signs[r.sign]) ;
  76. break ;
  77. default:
  78. fprintf(stream, "[non-standard IEEE double]") ;
  79. }
  80. }
  81. void
  82. gsl_ieee_printf_double (const double * x)
  83. {
  84. gsl_ieee_fprintf_double (stdout,x);
  85. }