+ 1
My code display 3.0. why my code don't display the result 3.5. thanks you.
Display result. https://code.sololearn.com/c8N4W57gPH3V/?ref=app
15 Respostas
+ 3
Rithea Sreng for that we have to do explicit type conversation (type casting). Your method is also fine but I prefer using decimal point when dealing with Constanta otherwise sometimes the answer is calculated is integer and then that integer is converted to double/float which leads to undesired output.
+ 3
Rithea Sreng , Jayakrishnađźđł is absolutely correct. Your code is working just because you are type casting only "a".
according to him if you type case the whole expression to double then it will not make any difference as ultimately calculations are happening as integers and then resultant integer is casted to double which will give rise to unwanted answer.
Here, have a look at this code for example đ
https://code.sololearn.com/cXsFsJZ9q8ga/?ref=app
+ 2
Malick Diagne your answer is calculated as an integer not a floating point character.
Just change 2 to 2.0 to tell system that you want to divide by a floating point character and not an integer.
Here is the fixđ
https://code.sololearn.com/c9LN7s98JqsU/?ref=app
+ 1
Arsenic is right. Avoid using casting as far as possible .Use casting when essential.
+ 1
Rithea Sreng
In your first post, 'middle' variable is double type. In java, lower type to higher type is automatically casted for destination type(but reverse not possible. Then you need explicit casting). So No need to add casting double. (with or without is same result there). As@Arsenic said, "atleast one value should be double or float type, if want result with decimal value in division....
+ 1
I read it. Just clarification replied.
I said, ( from your comments)
middle =(double)(var1.point1 + var2.point2 ) / 2 ;
is same as :
middle =(var1.point1 + var2.point2 ) / 2 ;
which is the trouble, from question actually.
But
middle =(var1.point1 + var2.point2 ) / 2. 0;
will result as required.
int a=3;
int b=2;
double c= ????; //double value of a/b
Here c=a/b;
System.out.println(c); prints 1.0
c= (double) (a/b); (Edit from : (double) a/b).
System.out.println(c); prints same 1.0
No difference.
To get 1.5 as a result a or b sholud be double or float.
Check out these.. If there is any wrong, reply me.. Rithea Sreng
0
Rithea Sreng oh from your comments, I just replayed that you meant taking about (double) (a/b) ; not (double) a/b;
Here (double) a / b will first cast a to double type which is same to making 3.0. What the difference..? This is what @arsenic suggested. (make 2 to 2.0).
I mean also about (double) (a/b), not (double) a / b..
Now braces added I added see., may be if you mean same, then that 2nd reply your Ms is not sync with first. Confusion is there..
0
Rithea Sreng
Yes. You not added (double) a/b also. so there is confusion to others to understand as:
In 1st reply you said that, but after not said difference in ,you should add braces,
so it mean that..
And from
double c= ????; //double value of a/b
from this, double value of a/b
is it (double value of a) / b?
Or
double value of (a/b)
(in sync with your 1st answer.)
How one can thought..? You should clarify there..
But cool, I replayed to you as you may not noticed, not to argue.. If you already know it, then its fine. Let it be...