This question already has answers here:
Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java
(6 answers)
Closed 8 years ago.
I noticed something weird earlier today. I was writing some code that was supposed to make graphs in complex quadrants. Anyway, I typed int i = 1/0; and it wouldn't compile. When I changed the code to double i = 1.0/0.0; the code compiled fine. When I ran the code it gave an error / by 0. I was expecting that... But why does it compile fine when using doubles and not integers? I am using the Blue J IDE
Dividing an int value by zero would result in a ArithmeticException, hence the expression 1 / 0 is illegal.
The result of dividing a double value by zero is infinity or NaN *, so the expression 1.0 / 0.0 is legal.
*) See t_over's comment for specifics:
Related
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 4 years ago.
I want the help to produce the result of below calculation as 1.12 but the result is coming up 1.0
double k=(112)/100;
System.out.println(k);
You are doing Integer division causing it to lose the precision:
Replace
double k=(112)/100;
with
double k=(112.0)/100;
double k-=((double)112/100) worked for me as suggested by agni
when we do division like (112/100) JVM gives int as an o/p, you are storing that 'int' in 'double' so JVM adds '.0' to the o/p causing loss of precision. so, here you have to tell the JVM to give o/p without any loss. for that reason we have to mention like `double k = (double)112/100; which is similar to "typecasting".
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 6 years ago.
I'm currently working on a Trafficsimulation which is based on a grid system. For some reason the line of code, which calculates how many tiles i have to add always returns 0. I have tried it without the variables but it still doesn't work.
double blocksToAdd = o.getVelocity()*((1000/Main.FPS)/1000);
Currently the velocity is equal to 1.0f and the Simulation runs at 10 FPS, so blocksToAdd should be 0.1, but it always returns 0.
Since Main.FPS is an int, 1000/Main.FPS is also an int, equal to 100. You then proceed to calculate 100/1000. Since this is an integer division, only the "whole" part is taken, giving 0.
Using floating point literals will cause Java to use floating point division, which should produce the correct result:
double blocksToAdd = o.getVelocity() * ((1000.0 /Main.FPS ) / 1000.0);
// Here --------------------------------------^--------------------^
Most likely due to integer division tuncating the fraction.
Replace the first 1000 with 1000.0 and all will be well. (The latter is a floating point double literal which causes the division to be computed in floating point.) There are other remedies but I find this one to be the clearest.
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I used the following code:
double pow = 3/7;
double num = 85;
System.out.println(Math.pow(num, pow));
Expected result:
6.71...
The output is
1.0
Any idea why?
3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.
Change it to 3.0/7 in order to get a floating point result.
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.