michael@0: /**************************************************************** michael@0: * michael@0: * The author of this software is David M. Gay. michael@0: * michael@0: * Copyright (c) 1991, 1996 by Lucent Technologies. michael@0: * michael@0: * Permission to use, copy, modify, and distribute this software for any michael@0: * purpose without fee is hereby granted, provided that this entire notice michael@0: * is included in all copies of any software which is or includes a copy michael@0: * or modification of this software and in all copies of the supporting michael@0: * documentation for such software. michael@0: * michael@0: * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED michael@0: * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY michael@0: * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY michael@0: * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. michael@0: * michael@0: ***************************************************************/ michael@0: michael@0: /* g_fmt(buf,x) stores the closest decimal approximation to x in buf; michael@0: * it suffices to declare buf michael@0: * char buf[32]; michael@0: */ michael@0: michael@0: #include "dmg_fp.h" michael@0: michael@0: namespace dmg_fp { michael@0: michael@0: char * michael@0: g_fmt(register char *b, double x) michael@0: { michael@0: register int i, k; michael@0: register char *s; michael@0: int decpt, j, sign; michael@0: char *b0, *s0, *se; michael@0: michael@0: b0 = b; michael@0: #ifdef IGNORE_ZERO_SIGN michael@0: if (!x) { michael@0: *b++ = '0'; michael@0: *b = 0; michael@0: goto done; michael@0: } michael@0: #endif michael@0: s = s0 = dtoa(x, 0, 0, &decpt, &sign, &se); michael@0: if (sign) michael@0: *b++ = '-'; michael@0: if (decpt == 9999) /* Infinity or Nan */ { michael@0: while((*b++ = *s++)) {} michael@0: goto done0; michael@0: } michael@0: if (decpt <= -4 || decpt > se - s + 5) { michael@0: *b++ = *s++; michael@0: if (*s) { michael@0: *b++ = '.'; michael@0: while((*b = *s++)) michael@0: b++; michael@0: } michael@0: *b++ = 'e'; michael@0: /* sprintf(b, "%+.2d", decpt - 1); */ michael@0: if (--decpt < 0) { michael@0: *b++ = '-'; michael@0: decpt = -decpt; michael@0: } michael@0: else michael@0: *b++ = '+'; michael@0: for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10) {} michael@0: for(;;) { michael@0: i = decpt / k; michael@0: *b++ = i + '0'; michael@0: if (--j <= 0) michael@0: break; michael@0: decpt -= i*k; michael@0: decpt *= 10; michael@0: } michael@0: *b = 0; michael@0: } michael@0: else if (decpt <= 0) { michael@0: *b++ = '.'; michael@0: for(; decpt < 0; decpt++) michael@0: *b++ = '0'; michael@0: while((*b++ = *s++)) {} michael@0: } michael@0: else { michael@0: while((*b = *s++)) { michael@0: b++; michael@0: if (--decpt == 0 && *s) michael@0: *b++ = '.'; michael@0: } michael@0: for(; decpt > 0; decpt--) michael@0: *b++ = '0'; michael@0: *b = 0; michael@0: } michael@0: done0: michael@0: freedtoa(s0); michael@0: #ifdef IGNORE_ZERO_SIGN michael@0: done: michael@0: #endif michael@0: return b0; michael@0: } michael@0: michael@0: } // namespace dmg_fp