This question already has answers here:
Java division by zero doesnt throw an ArithmeticException - why?
(8 answers)
Double divide by zero: Why is the result inconsistent?
(3 answers)
why there is no ArithmeticException( divide by zero) when both values are double? [duplicate]
(3 answers)
Why is number divided by zero infinity in Java?
(5 answers)
Why does integer division by zero 1/0 give error but floating point 1/0.0 returns "Inf"?
(5 answers)
Closed 3 years ago.
I am trying to run below program and expecting Arithmetic Exception because we are dividing double value by zero but getting Infinity as output.
This is below program. But if we are using int instead of double then getting Arithmetic Exception
public static void main(String z[]) {
Double a = 10.0;
Double b = 0.0;
System.out.println(a/b);
}
Output: Infinity
public static void main(String z[]) {
int a = 10;
int b = 0;
System.out.println(a/b);
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at (Test.java:94)
Can someone please explain why we are getting Infinity for Double
value?
Refer to the language spec:
if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.
...
The result of a floating-point division is determined by the rules of IEEE 754 arithmetic:
Division of a nonzero finite value by a zero results in a signed infinity.
Floating point division can have a non-exceptional result because there is a special value to indicate it. There is no such special value for integer division, so returning any integer result would be incorrect; the only option is to throw an exception.
In IEEE 754 floating point operations, dividing a value by 0 (resp. 0.0) is well-defined and results in an "infinite" value, either positive or negative, depending on the involved values.
In integer domain, no such thing is defined and thus you get an ArithmeticException.
Related
This question already has answers here:
How can I divide properly using BigDecimal
(2 answers)
Closed 5 years ago.
The title says it all: How do I divide a BigInteger by a floating point number in Java? I don’t need the fraction part of the division, it is okay to have it either rounded or truncated (however I would be interested which one applies).
The “obvious” does not even compile:
BigInteger x = BigInteger.valueOf(73).pow(42);
BigInteger y = x.divide(Math.PI); // The method divide(BigInteger) in the type BigInteger is
// not applicable for the arguments (double)
System.out.println(y);
I expected this one to work:
BigInteger y = new BigDecimal(x).divide(BigDecimal.valueOf(Math.PI)).toBigInteger();
Unluckily, it gives an ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. This is true for π, of course…
Of course, this one works, but it is way too slow…
BigInteger y = BigInteger.valueOf(-1);
BigDecimal σ = BigDecimal.ZERO;
while(σ.compareTo(new BigDecimal(x)) < 0) {
y = y.add(BigInteger.ONE);
σ = σ.add(BigDecimal.valueOf(Math.PI));
}
What’s the correct, canonical way?
You have to add RoundingMode to divide function, otherwise java doesn't know how to round the division and gives you ArithmeticException
BigInteger y = new BigDecimal(y).divide(BigDecimal.valueOf(Math.PI), RoundingMode.HALF_UP).toBigInteger();
All Rounding types are well explained in the documentation link above.
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 7 years ago.
Why is it that division of an integer by 0 gives ArithmeticException whereas division of a non-zero double or float by 0,prints Infinity.
Also division of an int 0 by 0 gives ArithmeticException whereas division of a double or float 0 by 0 gives NaN(Not a number).
public class First
{
public static void main(String[] args)
{
System.out.println(10/0); //Arithmetic Exception
System.out.println(10.0/0); //Prints Infinity
}
}
java follows IEEE floating point standards/practices, and those incorporate infinity [+ and -] as a value that can be calculated with, and NaN as a representation for results that are not a number.
(And obviously, those floating point standards do not have a counterpart for integer arithmetic.)
Division by a floating point 0.0 yields NaN or +/-Inf.
Division by an integer 0 is not covered by IEEE 754, and generates an exception - (i.e because an int can't represent NaN or Inf).
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
if 123/33 prints out 3 and 3 is an integer if we cast it to float ( (float)123/33 )how do we get the decimal places from the integer 3. does 3 contains floating points internally what ?
public static void test() {
System.out.println("==========");
System.out.println(123/33); // prints 3
System.out.println((float)123/33); // prints 3.7272727
//so if we cast it to float we get the decimal points also (.7272727)
}
The cast does not apply to the entire expression 123/33. You're casting the value 123 to a float, which causes any further operations to use floating-point math.
123 will be casted(converted) to float and then the division happens, so the result will be float value.
System.out.println("==========");
System.out.println(123/33); // prints 3
System.out.println((float)123); // prints 123.0
System.out.println((float)123/33); // prints 3.7272727
This question already has answers here:
Division in Java always results in zero (0)? [duplicate]
(3 answers)
Is "long x = 1/2" equal to 1 or 0, and why? [duplicate]
(6 answers)
Closed 8 years ago.
I have the following java code
long x=25782
float y=x/1000000000
Instead of y being equal to 0.000025782 as I would expect, it is equals 0.0
What am I doing wrong?
You're dividing by an long type, so it will round down to 0.
Use this instead:
float y=x/1000000000.0;
Integer division gives an integer result
This is integer division; in integer division the answer is always an integer (by truncating the non integer part, not be rounding). Which you are then casting to a float.
The easiest solution is to include at least 1 float. So
long x=25782
float y=x/1000000000f //(or 1000000000.0)
What you are effectively doing
float y=x/1000000000;
float y=(float)(int)(x/1000000000);
float y=(float)(int)(25782/1000000000);
float y=(float)0;
float y=0.0;
You have an integer division, which will round to the right of the decimal point, as integer division gives integer result.
You should cast x to float.
Try:
float y=(float)x/1000000000;
Int and long division always result in int and long.
at least operand in your calculation needs to be a float or double
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
Here's the code:
class testsum
{
public static void main(String arg[])
{
double sum=0;
double fraction;
fraction=-1/9;
System.out.println("fraction: "+fraction);
fraction=-1;
fraction=fraction/9;
System.out.println("fraction: "+fraction);
}
}
the outputs are 0 and then -0.11111111
why was the first output 0 and not -0.11111111111?
It's doing integer division in the first example as this is the default type for a numeric literal. Try changing it to -1.0/9 (or 1d/9d - the d suffix indicates a double) and you should get the same answer.
1 and 9 are both integer. Try
1.0/9
This is why it works for fraction/9, since fraction is a double.
When you do -1/9, it says "-1, that's an int. 9, that's an int. -1 / 9 in integer division is 0. Oh, now I need to cast to double."
Changing it to -1.0 / 9 should solve the problem.
Try wrapping the "-1/9" in brackets.
The first one is 0 because it is doing integer division. -1 and 9 and integers and when divided equal 0. The result is then converted into a double so it can be stored in fraction. The easiest solution is this:
fraction = -1.0/9;
Because -1 and 9 are integers, so -1/9 is an integer division (with the result 0, which when cast to double is 0.0).
To do a floating point division, you should convert one of the numbers to double, (double) 9, 9d or simply 9.0.
In the latter case, fraction is already double (-1.0) so fraction/9 is a floating point division.