dividebyzeroexception

What decides Nan and Infinity in java division operations

血红的双手。 提交于 2019-12-07 06:58:04
问题 Output of the below code confusing me. Why NaN sometimes and Infinity other times ? public static void main (String[] args) { double a = 0.0; double b = 1.0; int c = 0; System.out.println(a/0.0); System.out.println(a/0); System.out.println(b/0.0); System.out.println(b/0); System.out.println(c/0.0); System.out.println(c/0); } Outputs is: NaN NaN Infinity Infinity NaN Exception in thread "main" java.lang.ArithmeticException: / by zero What is the deciding factor here ? 回答1: This is because of

What decides Nan and Infinity in java division operations

痴心易碎 提交于 2019-12-05 08:39:44
Output of the below code confusing me. Why NaN sometimes and Infinity other times ? public static void main (String[] args) { double a = 0.0; double b = 1.0; int c = 0; System.out.println(a/0.0); System.out.println(a/0); System.out.println(b/0.0); System.out.println(b/0); System.out.println(c/0.0); System.out.println(c/0); } Outputs is: NaN NaN Infinity Infinity NaN Exception in thread "main" java.lang.ArithmeticException: / by zero What is the deciding factor here ? This is because of The IEEE Standard for Floating-Point Arithmetic (IEEE 754) which is a technical standard for floating-point

Why does this method return double.PositiveInfinity not DivideByZeroException?

一个人想着一个人 提交于 2019-11-27 07:38:41
问题 I ran the following snippet in the VS2015 C# interactive and got some very weird behavior. > double divide(double a, double b) . { . try . { . return a / b; . } . catch (DivideByZeroException exception) . { . throw new ArgumentException("Argument b must be non zero.", exception); . } . } > divide(3,0) Infinity > 3 / 0 (1,1): error CS0020: Division by constant zero > var b = 0; > 3 / b Attempted to divide by zero. > Why did the method return infinity while 3 / 0 threw an error and 3 / b threw

Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java

让人想犯罪 __ 提交于 2019-11-26 03:55:36
问题 The following statement throws java.lang.ArithmeticException: / by zero as obvious. System.out.println(0/0); because the literal 0 is considered to be an int literal and divide by zero is not allowed in integer arithmetic. The following case however doesn\'t throw any exception like java.lang.ArithmeticException: / by zero . int a = 0; double b = 6.199; System.out.println((b/a)); It displays Infinity . The following statement produces NaN (Not a Number) with no exception. System.out.println