Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

Printing Double Values with a Precision, Derived from the Given Error-Bound

3.89/5 (5 votes)
14 Nov 2006CPOL2 min read 2  
In this article, we will consider one way of printing double values with a precision derived from the given error-bound.

Introduction

In general, C has a rather powerful way of printing different values; for example, using print patterns as 1.3lf in the example below:

double value = 1.543605908721;

printf("Value 1.3lf", value);

The only problem with this approach is that this way of defining the printing format is absolutely non-flexible.

What if one wants to print values in different formats, depending on the input of the program itself?

/*One of examples is printing an array of doubles with the given error bound.*/

In this paper, we will discuss this problem in some detail.

Originally, the article was inspired by the problem of printing probability values up to the "right digit" after the decimal point. The "right digit" is the one that corresponds to a pre-set error bound.

All examples below are adapted from Markov Reward Model Checker (MRMC) tool sources, which are written by me and are freely distributed under the GPL license.

The Task

Having an array of double values, probs, and an error bound, error_bound (the same for all array elements), we would like to print those values, excluding all digits after the decimal point that are insignificant, with respect to the error bound.

For example:

double * probs = {0.565820439, 0.33339};
double error_bound = 1.0e-3;

//We would like to have the following output:

//Result: ( 0.5658, 0.3333)

Note that the output above is made up to the 4th digit after the decimal point but not up to the 3rd as one would be expecting. The reason is that, for example, for values 0.565820439 and 0.564710439 which have a difference 1.1e-3 (greater than the given error_bound), printing them up to the 3rd digit after the decimal point would not let us recognize this difference: 0.565 and 0.564.

Solution

The main task, as surprising as it could be, is to compute up to which digit after the decimal point the values should be printed. Consider the following function that computes it:

/**
* This method is used for getting
* the printing pattern from the error bound.
* NOTE: If we have error 1.231e-10 then the precision
* is 10+1 digits after the decimal point,
* but not 13 (as for 1.231e-10 == 1231.0e-13)!
* @param error_bound: the error bound
* @return:  the precision for the printing pattern by taking the position
*           of the first significant digit after the decimal point + 1
*/
int get_error_bound_precision(double error_bound){
    int precision = 0;
    BOOL not_found = TRUE;
    double integer = 0, fraction = 0;
    
    //NOTE: If we have error 1.1e-10 then the presision

    //is 10 digits after the decimal point, but not 11.

    while ( not_found ){
        error_bound *= 10;
        fraction = modf(error_bound, &integer);
        if ( integer > 0.0 ){
            not_found = FALSE;
        }
        precision++;
    }
    
    //We need this +1 here to add an extra digit

    return precision + 1;
}

What is done here, is basically going down the digits of the error_bound and looking for the first non zero one.

The rest is fairly easy, we have to create the pattern for printing our double values, which is done by using the sprintf operator, see the function below:

/**
* This method is used for printing the state
* probabilities vector with the given error bound.
* @param size:  the size of the probabilities vector
* @param probs:  the vector of probabilities
* @param error_bound:  the error bound
*/
void print_state_probs(int size, double * probs, double error_bound ){
    printf("Result: ");
    
    //Calculate the right pattern according to the precision

    char buffer[255];
    sprintf(buffer, "%%1.%dlf", get_error_bound_precision(error_bound));
    print_pattern_vec_double(buffer, size, probs);
}

and then call the following simple function that would print the array of doubles using the pattern we derived earlier.

/**
* This method is used for printing an array
* of double values according to the given pattern.
* @param pattern:  the pattern for printing each array element
* @param length: the array lenght
* @param vec: the array of doubles to be printed
*/
void print_pattern_vec_double(const char * pattern, int length, double * vec)
{
    int i;
    printf("( ");
    for( i = 0 ; i < length ; i++ ){
        //NOTE: this is where the pattern for printing is used

        printf( pattern, vec[i] );
        
        if (i != length-1) printf(", ");
    }
    printf(" )\n");
}

Conclusion

Here, we considered one of the simplest ways to print double values with the precision defined by some error bound.

Below, I will mention two more ways of solving the problem, which I've considered during my investigation:

  1. Having double error_bound = 1.47003454e-10 and printing values with the desired precision, taking into account the last non-zero digit of error_bound, is not feasible because actually the value stored in the double error_bound variable is not exactly 1.47003454e-10 but is something like 1.47003454000000000000329999961234769e-10, which is the desired error plus some noise.
  2. Using the IEEE floating-point standard to compute the desired precision from error_bound, does not really help. From the first glance, there is a part that gives you some knowledge about the exponential part of the number, but it is all about the power of 2, whereas we are interested in the power of 10.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)