0

I'm working on Responsive Design for my game. I recalculate position for my object in function of the screen size. I'm doing the good calcul but my function give me the wrong result always returning 0. I need the return value to be an Int.

Here the code :

public int rdnW(int a)
{
    double b;

    b = (a / 800 * 100);
    return (int)(b * Widths / 100);
}

With breakpoints i have see that b is always = 0; In my case a = 258. Thanks

pinckerman
  • 4,115
  • 6
  • 33
  • 42
Gabson
  • 421
  • 1
  • 4
  • 20
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 12 '13 at 13:40
  • 1
    Hard to believe that a post that is possibly a duplicate of scores of questions has 6 answers without a single close vote. – devnull Dec 12 '13 at 13:46

4 Answers4

7

You are performing integer division. Cast one of the values to double or float or specify one of the constants as a double: i.e. 100.0 instead of 100.

Example

b = (a / 800.0 * 100.0);
odyss-jii
  • 2,619
  • 15
  • 21
3

Use

b = ((double)a / 800 * 100);

That's because the expression is evaluated as integer because all the factors are integer. After the evaluation of the expression it is saved into a double variable.

You can also use something similar to:

b = (a / 800.0 * 100);

or

b = (a / 800 * 100.0); //in your case this will return zero because (a/800) will be evaluated as integer

It is necessary that at least one of the factors is a double to "tell" to the compiler that the expression have a double type

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
  • I think the last one would be evaluated as `(a / 800) * 100.0` which would not fix the (whole) problem. –  Dec 12 '13 at 13:43
2

You should cast a to double so the result of the calculation will not be rounded.

b = ((double)a / 800 * 100);
John Woo
  • 258,903
  • 69
  • 498
  • 492
2

Because what you done is here integer division. If a and b are integer and a < b , a / b always give you 0 as a result regardless which type you assign it or cast it.

In your cause, a is probably less than 800 so that's why it gives 0 as a result.

.NET has 3 type of division. From 7.7.2 Division operator

  • Integer division
  • Floating-point division
  • Decimal division

From Integer division part;

The division rounds the result towards zero, and the absolute value of the result is the largest possible integer that is less than the absolute value of the quotient of the two operands.

From / Operator (C# Reference)

When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2. To determine the remainder of 7 / 3, use the remainder operator (%). To obtain a quotient as a rational number or fraction, give the dividend or divisor type float or type double.

As a solution, you can cast double your a or 800 like;

b = ((double)a / 800 * 100);

or

b = (a / 800d * 100);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364