experimental/utility/old/LAGraph_mmwrite.c
static bool print_double
(
    FILE *f,        // file to print to
    double x        // value to print
)
{

    char s [MAXLINE], *p ;
    int64_t i, dest = 0, src = 0 ;
    int width, ok ;

    //--------------------------------------------------------------------------
    // handle Inf and NaN
    //--------------------------------------------------------------------------

    if (isnan (x))
    {
        return (fprintf (f, "nan") > 0) ;
    }
    if (isinf (x))
    {
        return (fprintf (f, (x < 0) ? "-inf" : "inf") > 0) ;
    }

    //--------------------------------------------------------------------------
    // find the smallest acceptable precision
    //--------------------------------------------------------------------------

    for (width = 6 ; width < 20 ; width++)
    {
        double y ;
        sprintf (s, "%.*g", width, x) ;
        sscanf (s, "%lg", &y) ;
        if (x == y) break ;
    }

    //--------------------------------------------------------------------------
    // shorten the string
    //--------------------------------------------------------------------------

    // change "e+0" to "e", change "e+" to "e", and change "e-0" to "e-"
    for (i = 0 ; i < MAXLINE && s [i] != '\0' ; i++)
    {
        if (s [i] == 'e')
        {
            if (s [i+1] == '+')
            {
                dest = i+1 ;
                if (s [i+2] == '0')
                {
                    // delete characters s[i+1] and s[i+2]
                    src = i+3 ;
                }
                else
                {
                    // delete characters s[i+1]
                    src = i+2 ;
                }
            }
            else if (s [i+1] == '-')
            {
                dest = i+2 ;
                if (s [i+2] == '0')
                {
                    // delete character s[i+2]
                    src = i+3 ;
                }
                else
                {
                    // no change
                    break ;
                }
            }
            while (s [src] != '\0')
            {
                s [dest++] = s [src++] ;
            }
            s [dest] = '\0' ;
            break ;
        }
    }

    // delete the leading "0" if present and not necessary
    p = s ;
    s [MAXLINE-1] = '\0' ;
    i = strlen (s) ;
    if (i > 2 && s [0] == '0' && s [1] == '.')
    {
        // change "0.x" to ".x"
        p = s + 1 ;
    }
    else if (i > 3 && s [0] == '-' && s [1] == '0' && s [2] == '.')
    {
        // change "-0.x" to "-.x"
        s [1] = '-' ;
        p = s + 1 ;
    }

#if 0
    // double-check
    i = sscanf (p, "%lg", &z) ;
    if (i != 1 || y != z)
    {
        // oops! something went wrong in the "e+0" edit, above.
        // this "cannot" happen
        sprintf (s, "%.*g", width, x) ;
        p = s ;
    }
#endif

    //--------------------------------------------------------------------------
    // print the value to the file
    //--------------------------------------------------------------------------

    return (fprintf (f, "%s", p) > 0) ;
}
// Print a double value to the file, using the shortest format that ensures the
// value is written precisely.  Returns true if successful, false if an I/O
// error occurred.
Print a double value to the file, using the shortest format that ensures the
value is written precisely.  Returns true if successful, false if an I/O
error occurred.
Print a double value to the file using the shortest format that ensures the value is written precisely . Returns true if successful false if an I / O error occurred .
static bool print_double
(
    FILE *f,        
    double x        
)
{
    char s [MAXLINE], *p ;
    int64_t i, dest = 0, src = 0 ;
    int width, ok ;
    if (isnan (x))
    {
        return (fprintf (f, "nan") > 0) ;
    }
    if (isinf (x))
    {
        return (fprintf (f, (x < 0) ? "-inf" : "inf") > 0) ;
    }
    for (width = 6 ; width < 20 ; width++)
    {
        double y ;
        sprintf (s, "%.*g", width, x) ;
        sscanf (s, "%lg", &y) ;
        if (x == y) break ;
    }
    for (i = 0 ; i < MAXLINE && s [i] != '\0' ; i++)
    {
        if (s [i] == 'e')
        {
            if (s [i+1] == '+')
            {
                dest = i+1 ;
                if (s [i+2] == '0')
                {
                    src = i+3 ;
                }
                else
                {
                    src = i+2 ;
                }
            }
            else if (s [i+1] == '-')
            {
                dest = i+2 ;
                if (s [i+2] == '0')
                {
                    src = i+3 ;
                }
                else
                {
                    break ;
                }
            }
            while (s [src] != '\0')
            {
                s [dest++] = s [src++] ;
            }
            s [dest] = '\0' ;
            break ;
        }
    }
    p = s ;
    s [MAXLINE-1] = '\0' ;
    i = strlen (s) ;
    if (i > 2 && s [0] == '0' && s [1] == '.')
    {
        p = s + 1 ;
    }
    else if (i > 3 && s [0] == '-' && s [1] == '0' && s [2] == '.')
    {
        s [1] = '-' ;
        p = s + 1 ;
    }
#if 0
    i = sscanf (p, "%lg", &z) ;
    if (i != 1 || y != z)
    {
        sprintf (s, "%.*g", width, x) ;
        p = s ;
    }
#endif
    return (fprintf (f, "%s", p) > 0) ;
}
static bool print_double ( FILE * f , double x ) { char s [ MAXLINE ] , * p ; int64_t i , dest = 0 , src = 0 ; int width , ok ; if ( isnan ( x ) ) { return ( fprintf ( f , " " ) > 0 ) ; } if ( isinf ( x ) ) { return ( fprintf ( f , ( x < 0 ) ? " " : " " ) > 0 ) ; } for ( width = 6 ; width < 20 ; width ++ ) { double y ; sprintf ( s , " " , width , x ) ; sscanf ( s , " " , & y ) ; if ( x == y ) break ; } for ( i = 0 ; i < MAXLINE && s [ i ] != ' \0 ' ; i ++ ) { if ( s [ i ] == ' ' ) { if ( s [ i + 1 ] == ' ' ) { dest = i + 1 ; if ( s [ i + 2 ] == ' ' ) { src = i + 3 ; } else { src = i + 2 ; } } else if ( s [ i + 1 ] == ' ' ) { dest = i + 2 ; if ( s [ i + 2 ] == ' ' ) { src = i + 3 ; } else { break ; } } while ( s [ src ] != ' \0 ' ) { s [ dest ++ ] = s [ src ++ ] ; } s [ dest ] = ' \0 ' ; break ; } } p = s ; s [ MAXLINE - 1 ] = ' \0 ' ; i = strlen ( s ) ; if ( i > 2 && s [ 0 ] == ' ' && s [ 1 ] == ' ' ) { p = s + 1 ; } else if ( i > 3 && s [ 0 ] == ' ' && s [ 1 ] == ' ' && s [ 2 ] == ' ' ) { s [ 1 ] = ' ' ; p = s + 1 ; } #if 0 i = sscanf ( p , " " , & z ) ; if ( i != 1 || y != z ) { sprintf ( s , " " , width , x ) ; p = s ; } #endif return ( fprintf ( f , " " , p ) > 0 ) ; }
Print a double value to the file, using the shortest format that ensures the
value is written precisely.
Print a double value to the file using the shortest format that ensures the value is written precisely .
// file to print to // value to print //-------------------------------------------------------------------------- // handle Inf and NaN //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // find the smallest acceptable precision //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // shorten the string //-------------------------------------------------------------------------- // change "e+0" to "e", change "e+" to "e", and change "e-0" to "e-" // delete characters s[i+1] and s[i+2] // delete characters s[i+1] // delete character s[i+2] // no change // delete the leading "0" if present and not necessary // change "0.x" to ".x" // change "-0.x" to "-.x" // double-check // oops! something went wrong in the "e+0" edit, above. // this "cannot" happen //-------------------------------------------------------------------------- // print the value to the file //--------------------------------------------------------------------------