This question already has answers here:
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Closed 7 years ago.
double multiply()
{
double x=(2/3)*3.14*1.02;
System.out.print(x);
double y=0.666*3.14*1.02; /*(2/3)=0.666...*/
System.out.print(y);
}
Output:
x=0.0
y=SomeNumber
please explain this?
(2/3) is 0.
because both are integers. To solve this, use a cast or make it clear that your number is not an integer:
double x=(2/3d)*3.14*1.02;
Now you have an integer divided by a double, which results in a double.
Some more to read about this:
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html
(2/3) is computed first (because of the parentheses), and in integer arithmetic (since the number literals are of type int). The fractional part is discarded.
It is therefore an int type with a value of 0. The entire expression is therefore zero.
The obvious remedy is to remove the parentheses and write 2.0 / 3.0 instead. Some folk prefer an explicit cast, but I find that ugly.
2/3 = 0 because they don't have explicit cast to double they are integers. The whole expression becomes: double x=0*3.14*1.02;
which is 0.
because data type of both 2 and 3 is int and int/int gives you int which in your case 2/3 is 0.
Try using 2.0/3 or 2/3.0 you will get the required answer.
Suffix every number with a 'd' to be sure you are dealing with doubles
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
So I'm trying to do math on ints stored in an array of ints.
float day1Hours = (day1[3]-day1[2]) / 2;
for this specific problem, day1[3] = 19 and day1[2] is 10. So, it should be doing 19-10 = 9, and then dividing that by 2 to make 4.5. But, the output that I am getting is 4. I've also tried storing day1Hours as a double but that made no difference. How would I make this be able to do the math correctly and get those decimal values that I need?
The problem is that you are doing integer division and then converting to a float. Try
float day1Hours = (day1[3]-day1[2]) / 2.0f;
Using a float literal in the denominator will cause the division to be done in floating point, and you won't get integer truncation. (As an alternative to using a float literal, you could cast the numerator or denominator to a float, but that seems somewhat baroque to me. It would be more suitable if both the numerator and denominator were int variables.)
The reason that just changing the type of day1Hours doesn't affect the problem is that the entire right side is evaluated first using the declared data type of day1 and then converted to whatever type is on the left of the assignment.
float day1Hours = (float)(20-9) / 2; //5.5
Problem is that on the right side of equation the numbers are integers and when dividing 2 integers the decimal places are truncated (integer division rounds towards zero) 4.5 -> 4.0.
Try changing 2 -> 2f so the 2 would be considered a float instead of an integer.
I had to calculate this: 1*5000*500/(10000*24645.16239360158)
But then realized I should multiply it by 1000. I get 2 different answers depending on how I do it. I'm not sure why, though, as the placement of parentheses shouldn't matter for multiplication. Would appreciate any insight!
System.out.println(Double.toString(1000*1*5000*500/(10000*24645.16239360158)));
outputs
-7.283243937828597
for sure incorrect because it's negative
System.out.println(Double.toString(1000*(1*5000*500/(10000*24645.16239360158))));
on the other hand, outputs
10.143978603480635
(correct)
Basically in the second case we multiply the result by a 1000 after we've calculated it, and that somehow works.
Thanks!
You need to know that int * int yields an int (5 * 5). And int * double yields an double (5 * 5.0).
As you probably know, those data types have different maximal sizes and store values in a different way.
Here are some of them:
You can use Integer.MAX_VALUE and Integer.MIN_VALUE for the exact bounds.
If you need to exceed those values, but also want to have an int, you should use the class BigInteger. It can handle arbitrary big integers.
Else you will have what is called an overflow, when you do Integer.MAX_VALUE + 1 the result will be Integer.MIN_VALUE. If you recall how this number is stored in bytes, for example something like 111 then +1 would return 1000 but there is no place to store the first 1, you'll receive 000 which is the MIN_VALUE.
You also should know that when you compute 5 / 2 it will not return 2.5 but 2, as you divide two int, the result will also be an int. If you want to have a double, then you need to do something like this 5 / 2.0 or (5 + 0.0) / 2.
this expression 1000*1*5000*500/(10000*24645.16239360158),first calculate 1000*1*5000*500,the result is -1794967296,overflow.
this expression 1000*(1*5000*500/(10000*24645.16239360158)) overflow do not happend.
The evaluation of any expression is done by operator precedence parser. If you want to change the priority of precedence, You would've to use parentheses.
In the first case '/' and '*' having same priority table.
In second case '*' is getting some priority over '/'. Thats why your getting diffrent values as output.
It is because of Java's implicit type casting. By default Java would use int. As user2357112 commented, the number is too large for int.
If you want your first version to work, add a "d" to tell Java to use a double.
Double d = 1000d*1*5000*500/(10000*24645.16239360158)
System.out.println(d);
You'd get:
10.143978603480635
This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Why does this Java division print out zero? [duplicate]
(5 answers)
Closed 8 years ago.
I am using Java 1.6
final double check = 3 / 4;
System.out.println(check);
Console is showing: 0.0
Why is this happening? Shouldn't it come out 0.75?
Make that:
double check = 3.0 / 4;
and it'll work. You got 0 because 3 / 4 is an integer division, whose value is 0.
Because both are integer hence result will also be integer.
Cast any one into double like this:
double check = (double)3 / 4;
By doing:
3 / 4
you are performing an integer division, since 3 and 4 are int constants, not doubles. The result is therefore an int. But since you are assigning it to a double, the result of the division will then be promoted to a double. But at this point it is too late, since the integer division will have produced 0!
You need to do:
3.0 / 4
to achieve your desired result, since in this case, 4 will automatically be promoted to a double, and the result of the division will also be a double.
To be perfectly sure of what happens and if you like symmetry, you can also write:
3.0 / 4.0
You are dividing integers and assigning the result to double.In java division of two int values always yields an int.
So change the statement double check = 3 / 4; to double check = 3.0 / 4;
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Dividing by 100 returns 0
(6 answers)
Closed 9 years ago.
Would someone mind explaining why this doesn't work? All variables except for chance are ints, whereas chance is a double. When I print all the values they are definitely correct... but chance always comes out as 0.0. I know this has something to do with converting ints to doubles, as I have had an issue like this a couple of times before. What is the key to getting it to do what you want?
gladValue = (glad.dexterity+glad.tactical+weaponSkill);
oppValue = (glad.target.dexterity+glad.target.tactical+glad.target.agility);
chance = (gladValue/oppValue)*10.0;
Thanks
You should write gladValue * 10.0 / oppValue instead.
The reason is quite subtle. Your brackets mean that gladValue / oppValue is computed first. But these variables are integers so the result is an integer and therefore you lose the fraction part. Only when it is multipled by 10.0 will it get promoted to a double; but by then it's too late.
If you do as I say then, bearing mind that * and / have the same precedence and the operations happen from left to right, then when computing gladValue * 10.0, gladValue is promoted to floating point and that floating point result is divided by oppValue.
This question already has answers here:
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Closed 9 years ago.
I'm trying to do a very basic operation as follows:
double a=21/5;
System.out.println(a);
However, each time I get 4.0 as the output and not 4.2. I'm encountering this for the first time. I've been using Java for years, but never came across this obscurity.
You are using integer division, which result will always be integer
You should use something like this.
double a=(double)21/5;
You are doing integer division...
Try:
double a = 21.0/5;
Cast the division or specify one of the arguments as a decimal to force the return as a double:
double a = (double)21/5;
-or-
double a = 21.0/5;
Just cast one of the numbers to double:
double a = 21/5.0;
Force the cast to double.
double a = 21.0/5
This is called Arithmetic promotion. This means that all terms in an equation are made equal to the variable type with the highest precision. In this case double.