Daedius
Daedius

Reputation: 33

Java: Variable assignment issue

just a quick question and I'm probably gonna feel stupid for asking but still would like to know why it is so...!

Anyways, quick example:

x is a double.

double conversion = (x-32)*5/9;

This does the maths just fine.

double  conversion = (x-32)*(5/9);

This isn't fine because the (5/9) is being treated as an int, thus result is overall 0.

double  conversion = (x-32)*(5f/9f);

This does the maths just fine, as it explicitly makes the 5/9 values a float.

So my question is: Why does the first equation work perfectly fine? ( double conversion = (x-32)*5/9; )

Why isn't the 5/9 being made a 0 if it were an int supposedly? What makes the 5/9 different from (5/9) ?

Upvotes: 1

Views: 109

Answers (7)

Elliott Frisch
Elliott Frisch

Reputation: 201409

It's a matter of when the conversion takes place. 5/9 consists of one 5 and one 9 (both ints) being divided with integer division. If either is a float (5f/9 or 5/9f) they will divide as floats.

Upvotes: 0

Naveen Ramawat
Naveen Ramawat

Reputation: 1445

Your value of x might be double and it is making entire equation in double because brackets execute first.

Upvotes: 0

Chuidiang
Chuidiang

Reputation: 1055

The reason is the order of the operations.

(x-32)*5/9 makes first (x-32)*5 and then divides the result by 9

(x-32)*(5/9) makes first (x-32) and (5/9). After both results are multiplied.

Upvotes: 0

Sanjay Manohar
Sanjay Manohar

Reputation: 7026

It is the order it's done in. If you multiply by 5, you get a large number. If you then divide by 9, you still get an int, But the remainder is discarded.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

The difference is between whether you do the multiplication first or the division first - and what the types of those operations are.

This:

(x - 32) * 5 / 9

is equivalent to:

((x - 32) * 5) / 9

So if the type of x is double, then the type of x - 32 is double, so the 5 is promoted to double, the multiplication is done in double arithmetic, giving a double result, and then the division is also done in double arithmetic.

Even if x is an integer type, you're doing the multiplication first, which will presumably give you a value bigger than 9 (in your test case), leaving you with a non-zero result. For example, if x is 45, then x-32 is 13, (x - 32) * 5 is 65, and the overall result is 7, then converted to 7.0 on assignment. That's not the same result you'll get if x is a double with the value 45.0, but it's still better than multiplying by 0...

Upvotes: 4

Adam Yost
Adam Yost

Reputation: 3625

Assuming x is a double then your first equation divides a double by an integer due to order of operations.

(x-32) = y-> y*5 = z -> z/9

at each stage a double is being operated on, overriding integer arithmetic.

Upvotes: 0

light_303
light_303

Reputation: 2111

You basically answered your own question. Evaluation order makes all the difference.

basic left to right evaluation results in different type casting than your explicit evaluation order in your second example

Upvotes: 0

Related Questions