This site requires JavaScript, please enable it in your browser!
Greenfoot back
Super_Hippo
Super_Hippo wrote ...

2017/11/8

Problem with ?-: instead of if-else

1
2
Super_Hippo Super_Hippo

2017/11/8

#
(tkt = 1.0) How can the following give the result "60"
img.drawString(""+ (60/tkt>10 ? (int)(60/tkt) : 0), 0, 15);
while this one gives "60.0"?
img.drawString(""+ (60/tkt>10 ? (int)(60/tkt) : 60/tkt), 0, 15);
Somehow the code which shouldn't be executed has an influence on the other part... Doing the following which should do the same as the second one, it works as it should:
if (60/tkt>10) img.drawString(""+ (int)(60/tkt), 0, 15); 
else img.drawString(""+ 60/tkt, 0, 15);
So printing 60 (and not 60.0) on the image for tkt=1.0, but for example 7.5 when tkt=8.0. Is it a bug in java or am i missing something obvious here?
danpost danpost

2017/11/8

#
Out of curiosity, what happens when you change line 2 to the following?
else img.drawString(""+(60/tkt), 0, 15);
Super_Hippo Super_Hippo

2017/11/8

#
It's still correct then.
danpost danpost

2017/11/8

#
Super_Hippo wrote...
It's still correct then.
Not obvious !! Try comparing the different results with the following:
img.drawString(""+ (60/tkt>=10 ? 60/tkt : (int)(60/tkt)), 0, 15);
Super_Hippo Super_Hippo

2017/11/8

#
tkt → result 1.0 → 60.0 2.0 → 30.0 3.0 → 15.0 4.0 → 7.0 5.0 → 3.0 That's the other way around. It rounds at tkt = 4.0 and 5.0 but still keeps the .0 in the end.
danpost danpost

2017/11/8

#
Shouldn't the result of tkt=3 be 20; 4 be 15; and 5 be 12; and 6 be 10; then, 7 be 8.6; 8 be 7.5; 9 be 6.7; etc? Your numerical results do not match (regardless of how they are returned). You results seem to be for 60/(2^tkt).
Super_Hippo Super_Hippo

2017/11/8

#
Oh yes, it is. Theoretically, it is 0 → 120 1 → 60 2 → 30 3 → 15 4 → 7.5 5 → 3.75 (Other values are not possible.) Sorry for that. Still, the extra .0 only go away when using if-else over ?-: for some reason.
danpost danpost

2017/11/8

#
Did you try putting '(double)' in from of the non-integer part?
Super_Hippo Super_Hippo

2017/11/8

#
Not sure where the double should go, now it is pretty much everywhere with more ( ) than needed but it doesn't seem to matter either.
img.drawString(""+ ((((double)(60))/tkt>((double)(10))) ? (((int)(((double)(60))))/tkt) : (((double)(60))/tkt)), 0, 15);
Still same results.
danpost danpost

2017/11/8

#
I was thinking something like this:
img.drawString(""+ (60/tkt>10 ? (int)(60/tkt) : (double)(60.0/tkt)), 0, 15);
Or, maybe just adding the '.0' without the '(double)' does the trick:
img.drawString(""+(60/tkt>10 ? 60/tkt : 60.0/tkt), 0, 15);
Super_Hippo Super_Hippo

2017/11/8

#
Both versions don't change anything. I really don't have any clue why it is like that.
danpost danpost

2017/11/8

#
Must be the way it compiles the operation. Probably cannot have two different Type outputs for the operation. Sort of like a return from a method; the most specific type that can return all possible values is probably used.
nccb nccb

2017/11/8

#
If you have code like A ? B : C, then A must be boolean, whereas B and C must be the same type. If you have int and int, you get int. Double and double makes double. Java has a set of rules about how to convert the two expressions if they are of different types: if you have int and double, the type is double. Hence although you aren't executing the other branch, the type is determined at compile-time by looking at both halves of the expression. Because you're then converting to String, you see a .0 on the end if you have arranged it to be a double, or without .0 if you get it into an int. In the first line of the original post, you made an int (it's int in both parts of the conditional) whereas on the second line of original post, you made a double (int and double means both are changed to double to have the same type).
danpost danpost

2017/11/8

#
@nccb, thank you for confirming my suspicions.
Super_Hippo Super_Hippo

2017/11/8

#
Okay, I didn't know that they have to be the same type (or they will become the same type automatically). I thought it would be the same as doing the longer way with if and else. Thank you both for explaining!
There are more replies on the next page.
1
2