1. ## Cannot divide

Hello,

I have two variables, x and y, inside a loop. X gets the value of an AP (1,3,5,7,...) while Y gets the value of a GP (1,-1,1,-1). My problem is that the value of 1(x*y) is rounded by the computer to 0. Here is part of the source code of my program.

...
float value;
...
while (loop_count != max){
...
value += 1/(x*y);
...

What am I doing wrong? How can I fix this?

Thanks a lot!

2. It may be a problem with the integer division. For starters, you can see if value += 1.0/(x*y) fixes this. Integer division of 1 by some large number will get truncated to 0 and then assigned to value.

3. ...also look what type is your 'x' and 'y' variable, and how they are initialized.
Code:
`double x = 4.0; //something like that`
axon

4. Originally posted by hk_mp5kpdw
It may be a problem with the integer division. For starters, you can see if value += 1.0/(x*y) fixes this. Integer division of 1 by some large number will get truncated to 0 and then assigned to value.
Thank you, it worked out.
I've got another question:

The program I'm creating calculates pi with Leibniz procedure [4*(1/1 - 1/3 + 1/5 - 1/7 + ...)]. The problem is that it only gives precision of 4 characters (3,1415). How can I increase this?

Thanks!