Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Why is this math wrong?

DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I hit this on a practice test & dont understand why the answer is 2.0 instead of 2.6......what happens to the rest?
int a = 8;
int b = 3;
float f = a++/b--;
System.out.println(f);
I ran it & sure enough it's 2.0 but why is the .6 dropped when it can obviously be assigned to a float.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
float f = a++ / b--;
when executed would be
float f = a / b ? right ?
next , we do promotions for integers to float.
so it is actually 8.0 / 2.0
Originally posted by DC Dalton:
I hit this on a practice test & dont understand why the answer is 2.0 instead of 2.6......what happens to the rest?
int a = 8;
int b = 3;
float f = a++/b--;
System.out.println(f);
I ran it & sure enough it's 2.0 but why is the .6 dropped when it can obviously be assigned to a float.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Actually, the key here is you 're doing integer division, which is going to truncate the fractional part.

Since the unary operators are both postfix, the the operands evaluated for the division operator are 8 and 3.
Since a and b are both ints, they don't need to be converted to anything, so the division is performed on ints, and the result will be an int as well, and the resulting 2.666... is truncated to 2 by rules of integer division.
Finally, the right hand side of the assignment (2) gets widened to a float by implicit promotion, and that float value, 2.0, is assigneed to f.
Rob
[ January 12, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
why integer division ? don;t they have to be promoted to float ? don;t we do a primitive conversion here ?
what's the rule , for the postfix operands ? why do they have this special exception. any other operands having this exception.

Originally posted by Rob Ross:
Actually, the key here is you 're doing integer division, which is going to truncate the fractional part.

Since the unary operators are both postfix, the the operands evaluated for the division operator are 8 and 3.
Since a and b are both ints, they don't need to be converted to anything, so the division is performed on ints, and the result will be an int as well, and the resulting 2.666... is truncated to 2 by rules of integer division.
Finally, the right hand side of the assignment (2) gets widened to a float by implicit promotion, and that float value, 2.0, is assigneed to f.
Rob
[ January 12, 2002: Message edited by: Rob Ross ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205

why integer division ? don;t they have to be promoted to float ? don;t we do a primitive conversion here ?
what's the rule , for the postfix operands ? why do they have this special exception. any other operands having this exception

Integer division is used, because both operands of the division operator are ints. Promotion rules for binary operands are basically: if one of the operands is a double, convert the other to double, else if one is a float , convert the other to a float, else convert both to int.
Since they're already ints, no further conversion is needed and the sub-expression (a/b) can be evaluated.
The postfix operands are applied before the operands a and b are evaluated, but they do not change the value of either with regards to the division operation; they change the values stored in a and b, but this is seperate from the actual division operation. If they had been prefix, then a and b would have been updated first, before the values of a and b are evaluated, so the operands to the division operator would have been 9 and 4.
Finally, the result of the int division 8/3 is 2 and is assigned to a float variable, so it is widened according to the rules of assignemnt promotion.
Rob
[ January 13, 2002: Message edited by: Rob Ross ]

I agree. Here's the link: http://aspose.com/file-tools

subject: Why is this math wrong?