Changeset e63326b


Ignore:
Timestamp:
May 21, 2019, 2:23:43 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
5b35c21
Parents:
292642a
Message:

clean up always printing decimal point for floating-point numbers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r292642a re63326b  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun May 19 10:48:27 2019
    13 // Update Count     : 654
     12// Last Modified On : Tue May 21 13:01:26 2019
     13// Update Count     : 674
    1414//
    1515
     
    154154        } // ?|?
    155155
    156         static void checkDecPt( ostype & os, const char * buf, int len ) {
    157                 for ( int i = 0;; i += 1 ) {
    158                         if ( i == len ) { fmt( os, "." ); break; }
    159                         if ( buf[i] == '.' ) break;
    160                 } // for
    161         } // checkDecPt
     156        #define PrintWithDP( os, format, val, ... ) \
     157                { \
     158                        enum { size = 48 }; \
     159                        char buf[size]; \
     160                        int len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
     161                        fmt( os, "%s", buf ); \
     162                        if ( isfinite( val ) ) {                                        /* if number, always print decimal point */ \
     163                                for ( int i = 0;; i += 1 ) { \
     164                                        if ( i == len ) { fmt( os, "." ); break; } \
     165                                        if ( buf[i] == '.' ) break; \
     166                                } /* for */ \
     167                        } /* if */ \
     168                }
    162169
    163170        ostype & ?|?( ostype & os, float f ) {
    164171                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    165                 char buf[48];
    166                 int len = snprintf( buf, 48, "%g", f );
    167                 fmt( os, "%s", buf );
    168                 if ( isfinite( f ) ) checkDecPt( os, buf, len ); // always print decimal point
     172                PrintWithDP( os, "%g", f );
    169173                return os;
    170174        } // ?|?
     
    175179        ostype & ?|?( ostype & os, double d ) {
    176180                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    177                 char buf[48];
    178                 int len = snprintf( buf, 48, "%.*lg", DBL_DIG, d );
    179                 fmt( os, "%s", buf );
    180                 if ( isfinite( d ) ) checkDecPt( os, buf, len ); // always print decimal point
     181                PrintWithDP( os, "%.*lg", d, DBL_DIG );
    181182                return os;
    182183        } // ?|?
     
    187188        ostype & ?|?( ostype & os, long double ld ) {
    188189                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    189                 char buf[48];
    190                 int len = snprintf( buf, 48, "%.*Lg", LDBL_DIG, ld );
    191                 fmt( os, "%s", buf );
    192                 if ( isfinite( ld ) ) checkDecPt( os, buf, len ); // always print decimal point
     190                PrintWithDP( os, "%.*Lg", ld, LDBL_DIG );
    193191                return os;
    194192        } // ?|?
     
    201199//              os | crealf( fc ) | nonl;
    202200                float f = crealf( fc );
    203                 char buf[48];
    204                 int len = snprintf( buf, 48, "%g", f );
    205                 fmt( os, "%s", buf );
    206                 if ( isfinite( f ) ) checkDecPt( os, buf, len ); // always print decimal point
     201                PrintWithDP( os, "%g", f );
    207202                f = cimagf( fc );
    208                 len = snprintf( buf, 48, "%+g", f );
    209                 fmt( os, "%s", buf );
    210                 if ( isfinite( f ) ) checkDecPt( os, buf, len ); // always print decimal point
     203                PrintWithDP( os, "%+g", f );
    211204                fmt( os, "i" );
    212205                return os;
     
    220213//              os | creal( dc ) | nonl;
    221214                double d = creal( dc );
    222                 char buf[48];
    223                 int len = snprintf( buf, 48, "%.*lg", DBL_DIG, d );
    224                 fmt( os, "%s", buf );
    225                 if ( isfinite( d ) ) checkDecPt( os, buf, len ); // always print decimal point
     215                PrintWithDP( os, "%.*lg", d, DBL_DIG );
    226216                d = cimag( dc );
    227                 len = snprintf( buf, 48, "%+.*lg", DBL_DIG, d );
    228                 fmt( os, "%s", buf );
    229                 if ( isfinite( d ) ) checkDecPt( os, buf, len ); // always print decimal point
     217                PrintWithDP( os, "%+.*lg", d, DBL_DIG );
    230218                fmt( os, "i" );
    231219                return os;
     
    239227//              os | creall( ldc ) || nonl;
    240228                long double ld = creall( ldc );
    241                 char buf[48];
    242                 int len = snprintf( buf, 48, "%.*Lg", LDBL_DIG, ld );
    243                 fmt( os, "%s", buf );
    244                 if ( isfinite( ld ) ) checkDecPt( os, buf, len ); // always print decimal point
     229                PrintWithDP( os, "%.*Lg", ld, LDBL_DIG );
    245230                ld = cimagl( ldc );
    246                 len = snprintf( buf, 48, "%+.*Lg", LDBL_DIG, ld );
    247                 fmt( os, "%s", buf );
    248                 if ( isfinite( ld ) ) checkDecPt( os, buf, len ); // always print decimal point
     231                PrintWithDP( os, "%+.*Lg", ld, LDBL_DIG );
    249232                fmt( os, "i" );
    250233                return os;
Note: See TracChangeset for help on using the changeset viewer.