Home > Mobile >  Is it possible to increase the precision of a floating point sum of a harmonic series in C past 10^-
Is it possible to increase the precision of a floating point sum of a harmonic series in C past 10^-

Time:01-31

Given a harmonic series 1 - 1/2 1/3 - 1/4... = ln(2), is it possible to get a value of 0.69314718056 using only float values and using only basic operations ( ,-,*,/). Are there any algorithms which can increase the precision of this calculation without going to unreasonably high values of n (current reasonable limit is 1e^10)

What I currently have: this nets me 8 correct digits -> 0.6931471825

EDIT The goal is to compute the most precise summation value using only float datatypes

int main()
{
    float sum = 0;
    int n = 1e9;
    double ans = log(2);
    int i;
    float r = 0;

    for (i = n; i > 0; i--) {
        r = i - (2*(i/2));
        if(r == 0){
            sum -= 1.0000000 / i;
        }else{
            sum  = 1.0000000 / i; 
        }
    }

    printf("\n%.10f", sum);
    printf("\n%.10f", ans);
    return 0;
}

CodePudding user response:

On systems where a float is a single-precision IEEE floating point number, it has 24 bits of precision, which is roughly 7 or (log10(224)) digits of decimal precision.

If you change

double ans = log(2);

to

float ans = log(2);

You'll see you already get the best answer possible.

0.6931471 82464599609375       From log(2), casted to float
0.6931471 82464599609375       From your algorithm
0.6931471 8055994530941723...  Actual value
  \_____/
  7 digits

In fact, if you use %A instead of %f, you'll see you get the same answer to the bit.

0X1.62E43P-1  // From log(2), casted to float
0X1.62E43P-1  // From your algorithm

CodePudding user response:

@ikegami already showed this answer in decimal and hex, but to make it even more clear, here are the numbers in binary.

ln(2) is actually:

0.1011000101110010000101111111011111010001110011111…

Rounded to 24 bits, that is:

0.101100010111001000011000

Converted back to decimal, that is:

0.693147182464599609375

...which is the number you got. You simply can't do any better than that, in the 24 bits of precision you've got available in a single-precision float.

  • Related