/*PrintE82 - series of routines to mimic 1p, e8.2 fortran output */ #include #include "cddefines.h" #include "printe82.h" /*********************************************************** * contains the following sets of routines to get around * * the MS C++ compilers unusual exponential output. * * PrintEfmt <= much faster, no overhead with unix * * PrintE93 * * PrintE82 * * PrintE71 * **********************************************************/ /**********************************************************/ /* * Instead of printf'ing with %e or %.5e or whatever, call * efmt("%e", val) and print the result with %s. This lets * us work around bugs in VS C 6.0. */ char *PrintEfmt(const char *fmt, double val /* , char *buf */) { static char buf[30]; /* or pass it in */ /* create the string */ sprintf(buf, fmt, val); /* we need to fix e in format if ms vs */ # ifdef _MSC_VER { /* code to fix broken ms v e format. works only for case where there is * a leading space in the format - for formats like 7.1, 8.2, 9.3, 10.4, etc * result will have 1 too many characters */ char *ep , buf2[30]; /* msvc behaves badly in different ways for positive vs negative sign vals, * if val is positive must create a leading space */ if( val >= 0.) { strcpy(buf2 , " " ); strcat(buf2 , buf); strcpy( buf , buf2); } /* allow for both e and E formats */ if ((ep = strchr(buf, 'e')) == NULL) { ep = strchr(buf, 'E'); } /* ep can still be NULL if val is Inf or NaN */ if (ep != NULL) { /* move pointer two char past the e, to pick up the e and sign */ ep += 2; /* terminate buf where the e is, *ep points to this location */ *ep = '\0'; /* skip next char, */ ++ep; /* copy resulting string to return string */ strcat( buf, ep ); } } # endif return buf; } /**********************************************************/ void PrintE82( FILE* ioOut, double value ) { double frac , xlog , xfloor , tvalue; int iExp; if( value < 0 ) { fprintf(ioOut,"********"); } else if( value == 0 ) { fprintf(ioOut,"0.00E+00"); } else { /* round number off for 8.2 format (not needed since can't be negative) */ tvalue = value; xlog = log10( tvalue ); xfloor = floor(xlog); /* this is now the fractional part */ frac = tvalue*pow(10.,-xfloor); /*this is the possibly signed exponential part */ iExp = (int)xfloor; if( frac>9.9945 ) { frac /= 10.; iExp += 1 ; } /* print the fractional part*/ fprintf(ioOut,"%.2f",frac); /* E for exponent */ fprintf(ioOut,"E"); /* if positive throw a + sign*/ if(iExp>=0 ) { fprintf(ioOut,"+"); } fprintf(ioOut,"%.2d",iExp); } return; } /* *============================================================================== */ void PrintE71( FILE* ioOut, double value ) { double frac , xlog , xfloor , tvalue; int iExp; if( value < 0 ) { fprintf(ioOut,"*******"); } else if( value == 0 ) { fprintf(ioOut,"0.0E+00"); } else { /* round number off for 8.2 format (not needed since can't be negative) */ tvalue = value; xlog = log10( tvalue ); xfloor = floor(xlog); /* this is now the fractional part */ frac = tvalue*pow(10.,-xfloor); /*this is the possibly signed exponential part */ iExp = (int)xfloor; if( frac>9.9945 ) { frac /= 10.; iExp += 1 ; } /* print the fractional part*/ fprintf(ioOut,"%.1f",frac); /* E for exponent */ fprintf(ioOut,"E"); /* if positive throw a + sign*/ if(iExp>=0 ) { fprintf(ioOut,"+"); } fprintf(ioOut,"%.2d",iExp); } return; } /* *============================================================================== */ void PrintE93( FILE* ioOut, double value ) { double frac , xlog , xfloor, tvalue; int iExp; if( value < 0 ) { fprintf(ioOut,"*********"); } else if( value == 0 ) { fprintf(ioOut,"0.000E+00"); } else { /* round number off for 9.3 format, neg numb not possilbe */ tvalue = value; xlog = log10( tvalue ); xfloor = floor(xlog); /* this is now the fractional part */ frac = tvalue*pow(10.,-xfloor); /*this is the possibly signed exponential part */ iExp = (int)xfloor; if( frac>9.99949 ) { frac /= 10.; iExp += 1 ; } /* print the fractional part*/ fprintf(ioOut,"%5.3f",frac); /* E for exponent */ fprintf(ioOut,"E"); /* if positive throw a + sign*/ if(iExp>=0 ) { fprintf(ioOut,"+"); } fprintf(ioOut,"%.2d",iExp); } return; }