除法运算谁不会啊,很多人不屑一顾,真是无知者无畏!
其实除法、求余运算有一些陷阱。一旦计算发生了问题,还很不好找。不好找的原因主要是问题的偶然性太强,如果你知道可能发生什么问题,你的代码就可以写得更安全。
数学除法规定,0不能做除数,因为会得到一个无穷大数据。
西面看看Java中如何处理这些特殊情况:
1、整数的除法:
0做除数抛运行时异常;两整数商会做取整运算,Float或Double与一个整数做除法运算,则商位Float或者Double类型,例如:
System.out.println(
"------------Int相关除法----------");
System.out.println( "12/10="+12/10);
System.out.println( "12f/10="+12f/10);
System.out.println( "12d/10="+12d/10);
System.out.println( "12/10f="+12/10d);
System.out.println( "12/10d="+12/10f);
System.out.println( "12/10="+12/10);
System.out.println( "12f/10="+12f/10);
System.out.println( "12d/10="+12d/10);
System.out.println( "12/10f="+12/10d);
System.out.println( "12/10d="+12/10f);
------------Int相关除法----------
12/10=1
12f/10=1.2
12d/10=1.2
12/10f=1.2
12/10d=1.2
12/10=1
12f/10=1.2
12d/10=1.2
12/10f=1.2
12/10d=1.2
2、Double(或Float)除法运算:
0可以做除数,得到的是一个分正负的无穷大;当两个数的绝对值均为0.0时候,商等于NaN。当0.0/x,x不等0.0时候,得到的一个带符号位0.0:
package lavasoft.zerotest;
/**
* 浮点型数据的除法运算测试
*
* @author leizhimin 2009-12-21 9:00:37
*/
public class TestZero {
public static void main(String[] args) {
System.out.println( "------------Double型----------");
Double x1 = div(2.3, 0.0);
Double x2 = div(2.3, -0.0);
Double x3 = div(0.0, 0.0);
Double x4 = div(0.0, -0.0);
Double x5 = div(0.0, 0.1);
Double x6 = div(0.0, -0.1);
if (x1.isInfinite()) System.out.println( "x1无穷大!");
if (x2.isInfinite()) System.out.println( "x2无穷大!");
if (x3.isNaN()) System.out.println( "x3非数字!");
if (x4.isNaN()) System.out.println( "x4非数字!");
if (x1 == Double.POSITIVE_INFINITY) System.out.println( "x1 = Double.POSITIVE_INFINITY");
if (x2 == Double.NEGATIVE_INFINITY) System.out.println( "x1 = Double.NEGATIVE_INFINITY");
if (x3 == Double.NaN) System.out.println( "x3 = Double.NaN");
if (x4 == Double.NaN) System.out.println( "x4 = -Double.NaN");
System.out.println( "------------Float型----------");
Float y1 = div(2.3f, 0.0f);
Float y2 = div(2.3f, -0.0f);
Float y3 = div(0.0f, 0.0f);
Float y4 = div(0.0f, -0.0f);
Float y5 = div(0.0f, -0.1f);
System.out.println( "------------比较测试----------");
Float a = 99999999999999999999999999999999999999f;
Float b = 0.000000000000000000000000000000000000000000001f;
Float t = a / b;
System.out.println(t);
System.out.println(Float.MAX_VALUE);
if (t >= Float.MAX_VALUE) {
System.out.println( "a/b的商已经超过了Float的最大值了!");
}
}
public static Double div( double a, double b) {
double x = a / b;
System.out.println(a + "/" + b + " = " + x);
return x;
}
public static Float div( float a, float b) {
float x = a / b;
System.out.println(a + "/" + b + " = " + x);
return x;
}
}
/**
* 浮点型数据的除法运算测试
*
* @author leizhimin 2009-12-21 9:00:37
*/
public class TestZero {
public static void main(String[] args) {
System.out.println( "------------Double型----------");
Double x1 = div(2.3, 0.0);
Double x2 = div(2.3, -0.0);
Double x3 = div(0.0, 0.0);
Double x4 = div(0.0, -0.0);
Double x5 = div(0.0, 0.1);
Double x6 = div(0.0, -0.1);
if (x1.isInfinite()) System.out.println( "x1无穷大!");
if (x2.isInfinite()) System.out.println( "x2无穷大!");
if (x3.isNaN()) System.out.println( "x3非数字!");
if (x4.isNaN()) System.out.println( "x4非数字!");
if (x1 == Double.POSITIVE_INFINITY) System.out.println( "x1 = Double.POSITIVE_INFINITY");
if (x2 == Double.NEGATIVE_INFINITY) System.out.println( "x1 = Double.NEGATIVE_INFINITY");
if (x3 == Double.NaN) System.out.println( "x3 = Double.NaN");
if (x4 == Double.NaN) System.out.println( "x4 = -Double.NaN");
System.out.println( "------------Float型----------");
Float y1 = div(2.3f, 0.0f);
Float y2 = div(2.3f, -0.0f);
Float y3 = div(0.0f, 0.0f);
Float y4 = div(0.0f, -0.0f);
Float y5 = div(0.0f, -0.1f);
System.out.println( "------------比较测试----------");
Float a = 99999999999999999999999999999999999999f;
Float b = 0.000000000000000000000000000000000000000000001f;
Float t = a / b;
System.out.println(t);
System.out.println(Float.MAX_VALUE);
if (t >= Float.MAX_VALUE) {
System.out.println( "a/b的商已经超过了Float的最大值了!");
}
}
public static Double div( double a, double b) {
double x = a / b;
System.out.println(a + "/" + b + " = " + x);
return x;
}
public static Float div( float a, float b) {
float x = a / b;
System.out.println(a + "/" + b + " = " + x);
return x;
}
}
运算输出:
------------Double型----------
2.3/0.0 = Infinity
2.3/-0.0 = -Infinity
0.0/0.0 = NaN
0.0/-0.0 = NaN
0.0/0.1 = 0.0
0.0/-0.1 = -0.0
x1无穷大!
x2无穷大!
x3非数字!
x4非数字!
x1 = Double.POSITIVE_INFINITY
x1 = Double.NEGATIVE_INFINITY
------------Float型----------
2.3/0.0 = Infinity
2.3/-0.0 = -Infinity
0.0/0.0 = NaN
0.0/-0.0 = NaN
0.0/-0.1 = -0.0
------------比较测试----------
Infinity
3.4028235E38
a/b的商已经超过了Float的最大值了!
Process finished with exit code 0
2.3/0.0 = Infinity
2.3/-0.0 = -Infinity
0.0/0.0 = NaN
0.0/-0.0 = NaN
0.0/0.1 = 0.0
0.0/-0.1 = -0.0
x1无穷大!
x2无穷大!
x3非数字!
x4非数字!
x1 = Double.POSITIVE_INFINITY
x1 = Double.NEGATIVE_INFINITY
------------Float型----------
2.3/0.0 = Infinity
2.3/-0.0 = -Infinity
0.0/0.0 = NaN
0.0/-0.0 = NaN
0.0/-0.1 = -0.0
------------比较测试----------
Infinity
3.4028235E38
a/b的商已经超过了Float的最大值了!
Process finished with exit code 0
陷阱:
零在整数预算中不可以做除数,否则抛运行时异常。
零在浮点运算中可以做除数,返回值为无穷大。
NaN各不相同,可看做是Java设计上的一个缺陷。
浮点型(Float或Double)的除法运算可以接受任何数值,并且结果总是返回一个浮点型的数值。这个数值可能是不合法的,需要进行判断和验证。
3、求余:和除法差不多。
System.out.println(23%4);
System.out.println(23%-4);
System.out.println(-23%4);
System.out.println(23f%4);
System.out.println(23d%4);
System.out.println(23%4f);
System.out.println(23%4d);
System.out.println(23f%0);
System.out.println(23%0.0);
System.out.println(0.0%0.0);
System.out.println(23%-4);
System.out.println(-23%4);
System.out.println(23f%4);
System.out.println(23d%4);
System.out.println(23%4f);
System.out.println(23%4d);
System.out.println(23f%0);
System.out.println(23%0.0);
System.out.println(0.0%0.0);
运行结果:
3
3
-3
3.0
3.0
3.0
3.0
NaN
NaN
NaN
3
-3
3.0
3.0
3.0
3.0
NaN
NaN
NaN
陷阱:
符号位由被除数决定。
对于Double、Float,API都提供了几个有用的判断方法,要注意运用其写出安全的代码:
boolean isInfinite()
如果该 Double 值的大小是无穷大,则返回 true;否则返回 false。
static boolean isInfinite( double v)
如果指定数字的大小是无穷大,则返回 true;否则,返回 false。
boolean isNaN()
如果此 Double 值是非数字(NaN)值,则返回 true;否则,返回 false。
static boolean isNaN( double v)
如果指定的数字是一个非数字 (NaN) 值,则返回 true;否则,返回 false。
如果该 Double 值的大小是无穷大,则返回 true;否则返回 false。
static boolean isInfinite( double v)
如果指定数字的大小是无穷大,则返回 true;否则,返回 false。
boolean isNaN()
如果此 Double 值是非数字(NaN)值,则返回 true;否则,返回 false。
static boolean isNaN( double v)
如果指定的数字是一个非数字 (NaN) 值,则返回 true;否则,返回 false。
Double、Float类中都提供了一些常量,也可用于判断,当然这些常量也很有意思,看看源码就知道了。
public
static
final
double POSITIVE_INFINITY = 1.0 / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
public static final double NaN = 0.0d / 0.0;
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
public static final double NaN = 0.0d / 0.0;