How to write formatted floatting point values?

How to write formatted floatting point values?

Post by Philippe Mor » Wed, 02 Jul 2003 06:34:30



How does I write a flotting point value with a fixed number of decimal with
Code Warrior.

I am doing a POL application in C++. POL::CStrStream does not seems to
support any formatting options. std::stringstream or sprintf causes the
application not to link and StrPrintF does not support floatting points...

Philippe

 
 
 

How to write formatted floatting point values?

Post by Luc Le Blan » Wed, 02 Jul 2003 13:58:24


Philippe Mori a crit :

> How does I write a flotting point value with a fixed number of decimal with
> Code Warrior.

> I am doing a POL application in C++. POL::CStrStream does not seems to
> support any formatting options. std::stringstream or sprintf causes the
> application not to link and StrPrintF does not support floatting points...

> Philippe

There is nothing built-in. Here's what I use, adapted from a classic. I have
not tested it with very small numbers.

--
Luc Le Blanc

Char            gDecimal = '.';        // or ','

/**********************************************************************/
/* Formatting parameters                                              */
/**********************************************************************/
#define MIN_FLOAT    4
#define NUM_DIGITS  15
/**********************************************************************/
/* FP conversion constants                                            */
/**********************************************************************/
double pow1[] = {
    1e256, 1e128, 1e064,
    1e032, 1e016, 1e008,
    1e004, 1e002, 1e001

};

double pow2[] = {
    1e-256, 1e-128, 1e-064,
    1e-032, 1e-016, 1e-008,
    1e-004, 1e-002, 1e-001

};

/***********************************************************************
 pdouble.c - Contains a routine to print double-precision floating
    point numbers and associated routines, written by Fred Bayer,
    author of LispMe.  It is available from
    http://www.lispme.de/lispme/gcc/tech.html

 This version was formatted and tweaked slightly by Warren Young
    <tang...@cyberport.com>

 This routine is in the Public Domain.

 Code edited 2001-01-14 by Ben Combee <bcom...@metrowerks.com> to
 work with CodeWarrior for Palm OS 7 and 8.

 Code edited and renamed 2002-12-09 by Luc Le Blanc to control
 number of decimals (max/min) and return the resulting string
***********************************************************************/

// *** StrDToA ***
Char *StrDToA( Char *s, double x, UInt16 maxDecimals, UInt16 minDecimals )
{
    FlpCompDouble       fcd;
    double             *pd, *pd1;
    Int16               e, e1, i, numDecimals;
    Int16               decimal = 0;
    Boolean             dot = false;
    Char                sign = '\0';
    Char                *string = s;

    if ( ( maxDecimals > NUM_DIGITS ) || ( minDecimals > maxDecimals ) )
    {
        StrCopy( s, "[error]" );
        return string;
    }

    /*------------------------------------------------------------------*/
    /* check for NAN, +INF, -INF, 0                                     */
    /*------------------------------------------------------------------*/
    fcd.d = x;
    if ( ( fcd.ul[ 0 ] & 0x7ff00000 ) == 0x7ff00000 )
        if ( fcd.fdb.manH == 0 && fcd.fdb.manL == 0 )
            if ( fcd.fdb.sign )
                StrCopy( s, "[-inf]" );
            else
                StrCopy( s, "[inf]" );
        else
            StrCopy( s, "[nan]" );
    else if ( FlpIsZero( fcd ) )
        StrCopy( s, "0" );
    else
    {
        /*----------------------------------------------------------------*/
        /* Make positive and store sign                                   */
        /*----------------------------------------------------------------*/
        if ( FlpGetSign( fcd ) )
        {
            *s++ = '-';
            fcd.d -= Roundings[ maxDecimals ];
            FlpSetPositive( fcd.ul );
        }
        else
            fcd.d += Roundings[ maxDecimals ];

        if ( ( unsigned ) fcd.fdb.exp < 0x3ff )   // meaning x < 1.0
        {
            /*--------------------------------------------------------------*/
            /* Build negative exponent                                      */
            /*--------------------------------------------------------------*/
            for ( e = 1, e1 = 256, pd = pow1, pd1 = pow2; e1;
                 e1 >>= 1, ++pd, ++pd1 )
                if ( *pd1 > fcd.d )
                {
                    e += e1;
                    fcd.d = fcd.d * *pd;
                }
            fcd.d = fcd.d * 10.0;

            /*--------------------------------------------------------------*/
            /* Only print big exponents                                     */
            /*--------------------------------------------------------------*/
            if ( e <= MIN_FLOAT )
            {
                *s++ = '0';
                *s++ = gDecimal;
                decimal = -1;
                while ( --e )
                    *s++ = '0';
            }
            else
                sign = '-';
        }
        else
        {
            /*--------------------------------------------------------------*/
            /* Build positive exponent                                      */
            /*--------------------------------------------------------------*/
            for ( e = 0, e1 = 256, pd = pow1, pd1 = pow2; e1;
                 e1 >>= 1, ++pd, ++pd1 )
                if ( *pd <= fcd.d )
                {
                    e += e1;
                    fcd.d = fcd.d * *pd1;
                }

            if ( e < NUM_DIGITS )
                decimal = e;
            else
                sign = '+';
        }

        /*----------------------------------------------------------------*/
        /* Extract decimal digits of mantissa                             */
        /*----------------------------------------------------------------*/
        if ( decimal < 0 )
            dot = true;

        for ( i = 0, numDecimals = 0; ( i < NUM_DIGITS ) && ( numDecimals <
maxDecimals ); ++i, --decimal )
        {
            Int32 d = fcd.d;
            *s++ = d + '0';

            if ( decimal == 0 )
            {
                *s++ = gDecimal;
                dot = true;
            }
            else if ( dot )
                numDecimals++;

            fcd.d = fcd.d - ( double )d;
            fcd.d = fcd.d * 10.0;
        }

        /*----------------------------------------------------------------*/
        /* Remove trailing zeroes and maybe decimal poin                  */
        /*----------------------------------------------------------------*/
        maxDecimals -= minDecimals;
        {
            while ( ( maxDecimals > 0 ) && ( s[ -1 ] == '0' ) )
            {
                *--s = '\0';
                maxDecimals--;
                if ( s[ -1 ] == gDecimal )
                {
                    *--s = '\0';
                    break;
                }
            }
        }

        /*----------------------------------------------------------------*/
        /* Append exponent                                                */
        /*----------------------------------------------------------------*/
        if ( sign )
        {
            *s++ = 'e';
            *s++ = sign;
            StrIToA( s, e );
        }
        else
            *s = '\0';
    }

    return string;

}


 
 
 

1. converting ieee-float-format to pw-wave-float-format

Hi everybody!!

My problem is the following:

I have an 4 byte float in the ieee-format and should convert it into the
pw_wave-format.

To do so, I had to shift the byte by 2, that means:
byte1  byte2  byte3  byte4 had to be shifted:

--> byte3  byte4  byte1  byte2

in pw_wave I do the following

readu,1,flo
print,flo  ---> byte3  byte4  byte1  byte2
print,float(flo,0)  ---> floaatingpointnumber

now my problem realy starts:
the result is only one quarter of the originally number, that means, if I had
a 10 then i get as result in pw_wave 2.5!!!

Are there any solutions on that problem?
whats the correct definition of the ieee-float-format?

Any solutions right here
or to

thanks a lot

Michael

2. chpasswd command

3. Compressing floating-point values - suggestions?

4. Battery Level Monitor

5. SNMP Floating Point Values

6. Is 8.9.1 more trouble than it's worth!!!

7. Floating point values in SMIv2

8. vxWorks boot over ethernet

9. Floating Point Values

10. Floating-point format in PowerPC

11. Floating point format conversion: IEEE - FFP

12. IEEE floating point format (IEEE 754) document wanted

13. how to set precision of floating point numbers and problem converting float to String