Division by zero in java [duplicate] - java

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).

Related

Not getting ArithmeticException in Java [duplicate]

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.

Difference between number/10 and number*0.1 in java

I've been working on an interview question for 1.5 hours and could not find the bug in my Java program.
And then I found what the problem was, which I don't understand (don't pay attention to the values, there were others, it's about the types):
int size=100;
Integer a=12;
if(a >= size/10)...
//didn't work
is different than
if(a >= size*0.1)...
//worked
I understand that there is a conversion, but still, how is it possible that with a=12, if(a>=size/10) returns false?
Why is that?
/10 is integer division. While *0.1 first converts the first operand to a double and performs a floating point multiplication.
If you use the /10, and the operand is 14, it will result in 1 indeed, 14/10=1.4 but integer division rounds this down. Thus 29/10=2.
If you use *0.1, the Java compiler will first convert the value of size to a double, thus 14.0 and then muliplies it with 0.1 resulting in 1.4.
On the other hand it's not all beaty that comes out of floating points. float and double can't represent every integer, and round off after computation.
For the given values for size however, it will result in the effect because 100 is a multiple of 10 and a float or double is capable of representing any integer value in the range from zero to hundred.
Finally /10 is not always an integer division: if the first operand is a floating point (e.g. 14.0d/10), the compiler will convert this to a floating point division.
Short version:
int/int is an integer division that rounds down to the nearest (lower) integer.
int*double is a double multiplication that - with rounding off errors - results in the floating point value, nearest to the correct result (with decimal digits).
I just tested here:
public class a {
public static void main(String[] args) {
int size = 100;
int a = 12;
System.out.println((a >= size / 10) ? "OK" : "Failed?");
}
}
And it worked. I don't think this is your real problem. Probably it's in another part of your code.

Calculation always results in zero [duplicate]

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

Dividing error in java language

I have a simple java program that does not operate the way that I think it should.
public class Divisor
{
public static void main(String[] args)
{
int answer = 5 / 2;
System.out.println(answer);
}
}
Why is this not printing out 2.5?
5 / 2 is integer division (you're even storing it in an integer variable), if you want it to be 2.5, you need to use floating point division:
double answer = 5.0 / 2.0;
Integer division is always going to be equal to normal mathematical division rounded down to the nearest integer.
Java has integer division which says: integer divided by integer results in integer. 2.5 cannot be represented with integer so the result is floored to 2.0. Moreover, you store the result in integer.
If you need floating point division you can cast one of operands to double and change answer type to double as well. You use literal values here, so changing 5 to 5. makes this literal value double.
In the end the following should work for you:
double answer = 5. / 2;
Note, you don't even need a zero sign after a dot symbol!

What's wrong with this division? [duplicate]

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.

Categories