Exemplo n.º 1
0
void __LDFloatToString( char *buf,
                        double const *f,
                        int precision,
                        std::ios::fmtflags format_flags ) {

    int         i;
    CVT_INFO    cvt;
    long_double ld;
    auto char   stkbuf[34];
#ifdef _LONG_DOUBLE_
    double      double_value;

    /* convert this double into a long double */
    double_value = *f;
    __EFG_cnvd2ld( &double_value, &ld );
#else
    ld.u.value = *f;
#endif

    if( (format_flags & (std::ios::scientific|ios::fixed)) == std::ios::scientific ) {
        cvt.flags = E_FMT;
        cvt.scale = 1;
    } else if( (format_flags & (std::ios::scientific|ios::fixed)) == std::ios::fixed ) {
        cvt.flags = F_FMT;
        cvt.scale = 0;
    } else {
        cvt.flags = G_FMT;
        cvt.scale = 1;
        if( precision == 0 ) {
            precision = 1;
        }
    }
    if( (format_flags & std::ios::showpoint) ) {
        cvt.flags |= F_DOT;
    }
    cvt.ndigits = precision;
    cvt.expchar = (format_flags & std::ios::uppercase) ? 'E' : 'e';
    cvt.expwidth = 0;
    __EFG_cvtld( &ld, &cvt, stkbuf );
    // put all the pieces together
    i = 0;
    if( cvt.sign < 0 ) {
        buf[i++] = '-';
    } else if( format_flags & std::ios::showpos ) {
        buf[i++] = '+';
    }
    if( cvt.n1 != 0 ) {
        memcpy( &buf[i], &stkbuf[0], cvt.n1 );
        i += cvt.n1;
    }
    memset( &buf[i], '0', cvt.nz1 );
    i += cvt.nz1;
    if( cvt.n2 != 0 ) {
        memcpy( &buf[i], &stkbuf[cvt.n1], cvt.n2 );
        i += cvt.n2;
    }
    memset( &buf[i], '0', cvt.nz2 );
    i += cvt.nz2;
    buf[i] = '\0';
}
Exemplo n.º 2
0
  ostream &ostream::__outfloat( long double const &f ) {

    int                 digit_offset;
    int                 precision;
    char                buf[ LONGEST_FLOAT * 2 ];
    long_double         ld;
    int                 i;
    int                 len;
    char                *x;
    CVT_INFO            cvt;
    auto char           stkbuf[ LONGEST_FLOAT + 1 ];
    std::ios::fmtflags  format_flags;
#ifdef _LONG_DOUBLE_
    double              double_value;

    /* convert this double into a long double */
    double_value = f;
    __EFG_cnvd2ld( &double_value, &ld );
#else
    ld.value = f;
#endif
    __lock_it( __i_lock );
    precision = this->precision();
    if( precision > MAX_PREC )
        precision = MAX_PREC;

    format_flags = this->flags();
    if(( format_flags & ( std::ios::scientific | ios::fixed )) == std::ios::scientific ) {
        cvt.flags = E_FMT;
        cvt.scale = 1;
    } else if(( format_flags & ( std::ios::scientific | ios::fixed )) == std::ios::fixed ) {
        cvt.flags = F_FMT;
        cvt.scale = 0;
    } else {
        cvt.flags = G_FMT;
        cvt.scale = 1;
        if( precision == 0 ) {
            precision = 1;
        }
    }
    if( format_flags & std::ios::showpoint ) {
        cvt.flags |= F_DOT;
    }
    cvt.ndigits = precision;
    cvt.expchar = ( format_flags & std::ios::uppercase ) ? 'E' : 'e';
    cvt.expwidth = 0;
    __EFG_cvtld( &ld, &cvt, stkbuf );
    // put all the pieces together
    len = cvt.n1 + cvt.nz1 + cvt.n2 + cvt.nz2 + 1;
    if( cvt.sign < 0 ) {
        ++len;
    } else if( format_flags & std::ios::showpos ) {
        ++len;
    }
    if( len > sizeof( buf ) ) {
        x = new char[ len + 1 ];
    } else {
        x = buf;
    }
    i = 0;
    digit_offset = 1;
    if( cvt.sign < 0 ) {
        x[i++] = '-';
    } else if( format_flags & std::ios::showpos ) {
        x[i++] = '+';
    } else {
        digit_offset = 0;
    }
    if( cvt.n1 != 0 ) {
        ::memcpy( &x[i], &stkbuf[0], cvt.n1 );
        i += cvt.n1;
    }
    if( cvt.nz1 != 0 ) {
        ::memset( &x[i], '0', cvt.nz1 );
        i += cvt.nz1;
    }
    if( cvt.n2 != 0 ) {
        ::memcpy( &x[i], &stkbuf[cvt.n1], cvt.n2 );
        i += cvt.n2;
    }
    if( cvt.nz2 != 0 ) {
        ::memset( &x[i], '0', cvt.nz2 );
        i += cvt.nz2;
    }
    x[i] = '\0';

    if( opfx() ) {
        setstate( __WATCOM_ios::writeitem( *this, x, ::strlen( x ), digit_offset ) );
        osfx();
    }
    if( len > sizeof( buf ) ) {
        delete x;
    }
    return( *this );
  }