Question

So, I'm a tad confused. I was under the impression that this should work: In this case, both a and b are ints (Counters to be exact).

As the result of a / b may possibly contain decimal places, ints obviously won't work. Therefore, I delared a new double and performed the calculation inside it like this:

double texturefactor = ((a / b) * 10);

This doesn't work as I expected, and the result of a / b is always that which I would get if I performed the calculation using an int to store the results. On the other hand, this works:

double calculate1 = a;
double calculate2 = b;
double texturefactor = ((calculate1 / calculate2) * 10);

Couple of perhaps stupid questions- 1. I'm sure this ought to work- I know that in certain situations VS will complain that I've tried to implicitly convert from one type to another- That's what I'm trying to do! Why doesn't it, and have I missed something? :) 2. Should I just convert the counters a and b to doubles and save myself the trouble of the conversion, or is that trouble?

Was it helpful?

Solution 2

The expression, including the types of subexpressions, is evaluated from inside to outside:

double texturefactor = ((a / b) * 10);

When the compiler analyses a / b, it has no idea that the result will later on be converted to double, so it just compiles the computation as an integer division.

Explicitly casting one of the two operands to double right there is enough to avoid that confusion:

double texturefactor = (((double)a / b) * 10);

OTHER TIPS

The result of a / b is another integer, so even if you do this, the damage has already been done:

double texturefactor = ((a / b) * 10);

Try casting one of your inner variables to a double:

var texturefactor = (( (double)a / b) * 10);

The result of (double)a / b will be a double, and you won't lose your fraction.

To answer your second question:

Should I just convert the counters a and b to doubles

That'd work too. If you change those to double, then you wouldn't have to perform the above cast.

When you have the expression (a / b), the C# compiler ignores that it will later be assigned to a double. It focuses only on that expression, and sees int / int, so it uses integer division. This means that your result is rounded down to the next integer before it's ever converted to a double.

You need to make sure C# treats the division as a division of doubles, not ints, for example:

double texturefactor = (((double)a / b) * 10); // or
double texturefactor = 10d * a / b; // or
double texturefactor = 10.0 * a / b;

The reason this double texturefactor = ((a / b) * 10); not working is that all the parameters on the right hand side are of type int thus the calculations are being done in int not double as you think.

You can explicitly cast a or b to double. Like

double texturefactor = (((double) a / b) * 10);

In your second code since you defined a and b as double, now the calculations are done in double type.

You need to cast the int values to double:

int a = some_value;
int b = some_value;
double texturefactor = (double)a / b * 10;

Integer division will not have decimal values just because you are assigning their values to a double variable.

If you want precision, then your second method is quite good.

double calculate1 = a;
double calculate2 = b;
double texturefactor = ((calculate1 / calculate2) * 10);

The above is good.

I will not advise you to cast it into double as there is some overhead to pay.

Result of dividing two integers is always a integer. So you need to

double texturefactor = (((double)a / b) * 10);

Therefore, I delared a new double and performed the calculation inside it like this:

Ah, no.

double texturefactor = ((a / b) * 10);

Basic syntax. YOu declare a double (texturefactor). You assign it the result of a pure integer opeation - (a/b)*10

Due to precedences this is a/b (integer, throw away digits) times 10. THEN implicit converison.

Typical beginner mistake - but one MANY do.

You want a double arithmetic, make sure any of the elements is a double, like for example:

((double) a/b)*10

will do a floating piont conversion for all elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top