1 Java .lang.Math类
java.lang.Math提供了一系列 静态方法 用于科学计算;其方法的参数和返回值类型一般为double型。
l abs 绝对值
l acos,asin,atan,cos, sin ,tan 三角函数
l sqrt 平方根
l pow(double a,doble b) a的b次幂
l log 自然对数
l exp e为底指数
l max(double a,double b)
l min(double a,double b)
l random() 返回0.0到1.0的随机数
l Long round(double a) double型数据a转换为long型(四舍五入)
l toDegrees(double angrad) 弧度—>角度
l toRadians(double angdeg) 角度—>弧度
2 java .math包的BigInteger和BigDecimal
Integer 类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的,最大为263-1如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算了。
java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。
l 构造方法
n BigInteger(String val):根据 字符串 构建BigInteger对象
l 常用方法
n BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger。
n BigInteger subtract(BigInteger val) :返回其值为 (this – val) 的 BigInteger。
n BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger。
n BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。
n BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。
n BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
n BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。
@Test
public void test2(){
// long num1 = 12345678901234567890L;//out of range 超过long的范围
BigInteger num1 = new BigInteger("12345678901234567890");
BigInteger num2 = new BigInteger("92345678901234567890");
// System.out.println("和:" + (num1 + num2));//错误的
System.out.println("和:" + num1.add(num2));
System.out.println("减:" + num1.subtract(num2));
System.out.println("乘:" + num1.multiply(num2));
System.out.println("除:" + num2.divide(num1));//两个整数相除只保留整数部分
System.out.println("幂次方:" + num2.pow(5));
}
一般的 Float 类和Double类可以用来做科学计算或工程计算,但是在商业计算中,要求数字精度比较高,所以用到java.math. BigDecimal 类。BigDecimal类支持不可变的、任意精度的有符号十进制定点数。
l 构造器
n BigDecimal(double val)
n BigDecimal(String val)
l 常用方法
n BigDecimal add(BigDecimal augend) :返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。
n BigDecimal subtract(BigDecimal subtrahend) :返回一个 BigDecimal,其值为 (this – subtrahend),其标度为 max(this.scale(), subtrahend.scale())。
n BigDecimal multiply(BigDecimal multiplicand):返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。
n BigDecimal pow(int n) :返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。
n BigDecimal divide(BigDecimal divisor): 返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() – divisor.scale());如果无法表示准确的商值(因为它有无穷的十 进制 扩展),则抛出 Arithmetic Exception。
n BigDecimal divide(BigDecimal divisor, int roundingMode) :返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。
n BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) :返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
@Test
public void test3(){
BigDecimal num1 = new BigDecimal("-12.1234567890123456567899554544444332");
BigDecimal num2 = new BigDecimal("89.6734567890123456567899554544444333");
System.out.println("和:" + num1.add(num2));
System.out.println("减:" + num1.subtract(num2));
System.out.println("乘:" + num1.multiply(num2));
System.out.println("除:" + num2.divide(new BigDecimal("2")));//可以整除(除尽)就对,不能整除就报异常
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_HALF_UP));
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_DOWN));//往零的方向舍去
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_FLOOR));//往小的方向舍去
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_ CEILING ));//往大的方向舍去
}
了解更多内容: