1

I write the following code, but I have a problem. When I divide 1 by 1059255, the result will be zero, because the result of division is close to zero and it's getting rounded.

for(x = 2 ; x <= 1059255; x++)
{
    y += (1/1059255) * x;
}

What changes needed to get the correct result?

woony
  • 240
  • 4
  • 8

9 Answers9

6

This is integer division. If you divide x/y where x is smaller than y the result will ALWAYS be 0. cast either of those numbers to float.

Here are some possibilities:

y += (1.0f/1059255) * x; // float literal divide by integer, this will work

or

y += (static_cast<float>(1)/1059255) * x; // casting integer literal to float, this works too

obviously you can make the denominator a float too and this can all be done with doubles as well.

Borgleader
  • 15,826
  • 5
  • 46
  • 62
2

That's because it is an integer division. Make either operand float or double:

y +=  (1.0/1059255)*x;
BlackBear
  • 22,411
  • 10
  • 48
  • 86
1

Because 1/1059255 is 0 in integer division

Change it to 1.0/1059255 will get your expected result.

Note that 1.0 is of type double, 1.0f is of type float, choose the one appropriate.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

You would want something like so:

const int start = 2;
const int end = 1059256;
float result = 0.0f;
for(int x = start; x < end; ++i){
  result += 1.0f / float(MAX) * x;
}

what you are doing is integer division, which effectively discards decimal points and just keeps the whole integer.

dchhetri
  • 6,926
  • 4
  • 43
  • 56
1

You need to use types that can represent numbers other than integers. Whether you want a floating-point or a fixed-point type depends on how important precision is. It is very easy to do it with floating-point numbers, however.

double x, y = 0;
for(x = 2 ; x <= 1059255; x++)
{
    y +=  (1.0/1059255.0)*x;
}

If you find that the amount of error is unacceptable, look into fixed-point numbers.

Eric Finn
  • 8,629
  • 3
  • 33
  • 42
1

In C++ division of two integers results in an integer. In this case you're dividing two integers, so the result will always be truncated to 0.

I can't tell for sure from your question, but you probably wanted floating point division which you can force by adding .0 suffix to the literal. Also I don't see any reason to change a division+multiply into a single division:

for(x = 2 ; x <= 1059255; x++)
{
    y += x / 1059255.0;
}

But if this is your real code there's no reason to do the loop at all as you can always compute the result in constant time (since the inner loop term linearly scales with the loop index value).

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

Use floating point number types like float or double.

Sven
  • 1,748
  • 1
  • 14
  • 14
0

try this:

for(x = 2 ; x <= 1059255; x++)
{
  y +=  (1.0/1059255.0)*x;
}
Smern
  • 18,746
  • 21
  • 72
  • 90
Harry
  • 1,362
  • 12
  • 19
0

Use floats inside the division so that the multiplication also gets right:

for(x = 2 ; x <= 1059255; x++)
{
    y +=  (float(1)/1059255)*x;
}