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