Here you are suffering from integer division:
s1 = s1 + 1 / ( i * i );
i is int , and every number without a suffix is implicitly int , so you divide 1/1, which, fortunately, leads to 1.
Things happen when i increases, although if it is greater than 1, you will get 0 for each result until the end of the loop.
Something unusual happens in my debugger when calculating this - division by zero occurs when the number 65536 - you get an overflow of 65536 2 - which is 2 32 which causes the int value to become 0.
Second cycle:
s2 = s2 + 1.0 / i * i;
You are burned by operator priority. Separation and multiplication have a higher priority than adding, so this is actually:
s2 = s2 + ((1.0 / i) * i);
You divide correctly, but the priority of your operator is incorrect.
Third cycle:
s3 = s3 + 1.0 / (i * i) ;
This is the same problem as above, but since you are in a floating-point context , anything divisible by 0 results in a signed Infinity .
source share