我有代码来计算2个数字之间的百分比差异(oldNum - newNum) / oldNum * 100;- 其中两个数字都是doubles.我希望在oldNum为0的情况下添加某种检查/异常处理.但是,当我为oldNum和newNum进行了值为0.0的测试运行时,执行继续,好像什么都没发生并且没有抛出错误.使用ints 运行此代码肯定会导致算术除零异常.为什么Java在涉及到doubles 时会忽略它?
我只是好奇这个:
1/0在Java中进行评估时,会发生以下异常:
线程"main"中的异常java.lang.ArithmeticException:/在Foo.main中为零(Foo.java:3)
但是1/0.0被评估为Infinity.
public class Foo {
public static void main (String[] args) {
System.out.println(1/0.0);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我想了解,为什么POSITIVE_INFINITY和NEGATIVE_INFINITY常量,只对浮点数据类型定义(float,double以及它们的包装),
public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
Run Code Online (Sandbox Code Playgroud)
但不是整体的数据类型(byte,short,int,long和它们的包装).这会影响不同数据类型的除法运算结果.例如:
对于整数类型:
int z = 10/0;
System.out.println(z);
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at TesterClass.main(TesterClass.java:16)
Run Code Online (Sandbox Code Playgroud)
对于浮点类型:
double z = 10/0.0;
System.out.println(z);
Output:
Infinity
Run Code Online (Sandbox Code Playgroud) 当我写的东西像
double a = 0.0;
double b = 0.0;
double c = a/b;
Run Code Online (Sandbox Code Playgroud)
结果是Double.NaN,但是当我为整数尝试相同时,它产生一个ArithmeticException.那么,为什么不存在Integer.NaN呢?
根据http://msdn.microsoft.com/en-us/library/system.dividebyzeroexception.aspx, 只有Int和Decimal在将它们除以0时会抛出DivideByZeroException,但是当你将浮点除以0时,结果是无穷大,负无穷大或NaN.为什么是这样?结果是+ ve无穷大,-ve无穷大还是NaN的例子是什么?
以下代码的输出让我感到困惑.为什么NaN有时和无限其他时间?
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);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
NaN
NaN
Infinity
Infinity
NaN
Exception in thread "main" java.lang.ArithmeticException: / by zero
Run Code Online (Sandbox Code Playgroud)
这里的决定因素是什么?
我一定在这里做了些蠢事.但我似乎无法弄清楚为什么这个简单的代码不起作用.InputMismatchException有效,但ArithmeticException永远不会被捕获.
import java.util.InputMismatchException;
import java.util.Scanner;
public class SubChap02_DivisionByZero {
public static double quotient(double num, double denum) throws ArithmeticException {
return num / denum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double num, denum, result;
boolean continueLoop = true;
do {
try {
System.out.printf("Please enter the numerator: ");
num = scanner.nextFloat();
System.out.printf("Please enter the denumerator: ");
denum = scanner.nextFloat();
result = quotient(num, denum);
continueLoop = false;
System.out.printf("THIS: %.2f/%.2f is %.2f\n", num, denum, result);
scanner.close();
} catch …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试零除的功能。
如果我只是做:System.out.println(5/0),我会得到java.lang.ArithmeticException
我正在尝试测试是否引发了异常。
这是我的单元测试:
@Test(expected = ArithmeticException.class)
public void given_divideByZeroInput_when_divideByZero_then_verify_throwing_exception() {
MathClass sut = new MathClass();
sut.div(10,0);
}
Run Code Online (Sandbox Code Playgroud)
我的div就是这么简单:
public float div(int x, int y) {
return (float) x/y;
}
Run Code Online (Sandbox Code Playgroud)
但是,单元测试失败,说明:
java.lang.AssertionError: Expected exception: java.lang.ArithmeticException
Run Code Online (Sandbox Code Playgroud)
我做错什么了?
我知道有很多方法可以测试抛出的异常,但是我试图坚持简单的方法 @Test(excepted = .. )
在Java中,我除以零,这显然不会产生数字.
public class tempmain {
public static void main(String args[])
{
float a=3/0;
System.out.println(a);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望输出为"NaN",但它显示以下错误:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at tempmain.main(tempmain.java:6)
Run Code Online (Sandbox Code Playgroud)
是否有任何方式(除此之外if(denominator==0))可以显示NaN?