-1

Trying to test precision making a sum from 1 to a million but my output keeps coming out as inf.

    #include <stdio.h>



int main()
{
float x=0.0 ;
double y =0.0;
int  i;
for(i=1; i<100000; i = i+1)
{
x=(float)  x+( 1.0f/(3.0f*(float)(i*i)));
y=(double) y+ ( 1.0/(3.0*(double)(i*i)));
}
printf("Natural Order \n");
printf("Single Precision: ");
printf("%f", x);
printf("\n");
printf("Double Precision: ");
printf("%lf", y);
printf("\n");
}

I have changed i range many times but still getting inf as my output.

prazuber
  • 1,352
  • 10
  • 26

4 Answers4

3

i^2 is not i-squared, but a binary manipulation (xor). Use e.g. i*i instead. There is also the pow function for higher powers.

jofel
  • 3,297
  • 17
  • 31
3

What do you want is probably the following:

int main()
{
    float x=0.0 ;
    double y =0.0;
    int  i;

    for(i=1; i<10; i = i+1)
    {
       x= x+( 1.0f / ( 3.0f * (float)(i)*(float)(i)   ));
       y= y+(  1.0 / ( 3.0  * (double)(i)*(double)(i) ));
    }

    printf("Single Precision: ");
    printf("%f", x);
    printf("\n");
    printf("Double Precision: ");
    printf("%f", y);
    printf("\n");

    return 0;
}

is not like excel: ^ is not the way to pow a number/variable.

In languace ^ is XOR bitwise operation

Your code does i XOR i that is 0 when i=2. Then the printf outputs inf because of the division by zero.

I did a lot of code optimization changing LD A, 0 with XOR A ...Z80 talking :)

Take note that printf %f format promote passed variable to double.

LPs
  • 16,045
  • 8
  • 30
  • 61
3

The expression i^2 means i BITWISE-XOR 2, not repeated multiplication.

Some values of i^2 are zero - when i=2 - which means you sometimes divide by zero.

Replace (i^2) with (i*i).

antiduh
  • 11,853
  • 4
  • 43
  • 66
  • 1
    Not for some value: only for i=2 – LPs Feb 02 '16 at 17:01
  • @LPs - You're confusing properties of the expression with the conditions necessary to invoke those properties. The expression "some values of `i^2` are zero" is absolutely true - the condition that satisfies that expression is `i=2`. Since I've stated both the property and its condition, your criticism is pointless. – antiduh Feb 02 '16 at 17:09
  • I wrote before your edit. You wrote: _Some values of i^2 are zero (odd values of i)_ Now the answer is ok. – LPs Feb 03 '16 at 07:10
-1
printf("Double Precision: ");
printf("%lf", y);               //double has %lf format specifier.

Actually, according to the link float will automatically be promoted to double when needed, but depending on a compiler is a bad habit. Plus the code will not be clear to other readers.

Community
  • 1
  • 1
Mayank Verma
  • 633
  • 6
  • 19
  • `%lf` is used for `scanf` only. `Printf` always promote `%f` passed variable to `double` and does not care to `l` part of format. – LPs Feb 02 '16 at 19:42
  • What I said was the code will not be clear to other readers. When did I say `printf` won't promote to `double`? Where am I wrong if I said the code won't be clear? – Mayank Verma Feb 03 '16 at 08:05
  • `float` is promoted to `double` always, not when needed. – LPs Feb 03 '16 at 08:17