天天看点

Java常用类之Math类、BigInteger类

Math类

Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用。

Math类中有两个静态常量:E和PI

System.out.println(Math.E);
System.out.println(Math.PI);
输出
2.718281828459045
3.141592653589793
           

Math类的常用方法

方法名 描述
public static long abs(double a) 返回a的绝对值
public static double max(double a, double b) 返回a,b的最大值
public static double min(double a, double b) 返回a,b的最小值
public static double random() 产生一个0到1之间的随机数(不包括0和1)
public static double pow(double a, double b) 返回a的b次幂
public static double sqrt(double a) 返回a的平方根
public static double log(double a) 返回a的以自然数e为底的对数
public static double sin(double a) 返回正弦值
public static double asin(double a) 返回反正弦值
public static double cos(double a) 返回余弦值
public static double acos(double a) 返回反余弦值
public static double tan(double a) 返回正切值
public static double atan(double a) 返回反正切值
public static int ceil(double a) 返回大于等于( >= )给定参数的的最小整数
public static int ceil(float a) 返回大于等于( >= )给定参数的的最小整数
public static int floor() 返回小于等于(<=)给定参数的最大整数
public static double rint() 返回与参数最接近的整数。返回类型为double
round() 它表示四舍五入,算法为 Math.floor(x+0.5),即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。
public static double exp(double d) 返回自然数e的d次方根
public static double toDegrees(double d) 将弧度转化为角度
public static double toRadians(double d) 将角度转换为弧度
package Liu;

public class ArrayMethodDemo1 {
	
	public static void main(String[] args) {
		double number = 99;
		double PI = Math.PI;
		double E = Math.E;
		System.out.println("PI等于:" + PI);
		System.out.println("E等于:" + E);
		System.out.println("PI和E中最大的是:" + Math.max(PI, E));
		System.out.println("小于等于E的最大整数为:" + Math.floor(E));
		System.out.println("以10为底的PI的对数为:" + Math.log10(PI));
		System.out.println("E的PI次幂为:" + Math.pow(E, PI));
		System.out.println("将PI转换成角度为:" + Math.toDegrees(PI));		
	}
}
输出
PI等于:3.141592653589793
E等于:2.718281828459045
PI和E中最大的是:3.141592653589793
小于等于E的最大整数为:2.0
以10为底的PI的对数为:0.49714987269413385
E的PI次幂为:23.140692632779263
将PI转换成角度为:180.0

           

NumberFormat类

有时我们还可能需要将数字结果进行必要的格式化,可以使用Java.text包中的NumberFormat类。

NumberFormat对象的实例化:public static final NumberFormat getInstance()

该对象调用public final String format(double b)来格式化数字b

该类中有四种常用方法格式化字符串输出设置

public void setMaximumFractionDigits(int newValue);//设置小数部分最大位数
public void setMinimumFractionDigits(int newValue);//设置小数部分最小位数
public void setMaximumIntegerDigits(int newValue);//设置整数部分最大位数
public void setMinimumIntegerDigits(int newValue);//设置整数部分最小位数
           

下面来一个例子

package Liu;
import java.text.NumberFormat;

public class ArrayMethodDemo1 {
	
	public static void main(String[] args) {
		double a = 3.1415926;
		NumberFormat aFormat = NumberFormat.getInstance();		//实例化一个NumberFormat对象
		//设置小数部分最小位数为10位
		aFormat.setMinimumFractionDigits(10);
		//设置整数部分最小位数为3位
		aFormat.setMinimumIntegerDigits(3);
		//格式化数字a
		String s = aFormat.format(a);
		//输出字符串s
		System.out.println(s);
	}
}
输出
003.1415926000
           

BigInteger类

不可变的任意精度的整数。所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。

BigInteger类的构造方法

public BigInteger(String VAL)构造一个十进制的BigInteger对象,该构造方法可以发生NumberFormatException异常,

也就是说,字符串参数VAL中如果含有非数字字符就会发生NumberFormatException异常。

主要有这几类构造方法。

Java常用类之Math类、BigInteger类

最基本的就是public BigInteger(String VAL),构造一个十进制的BigInteger对象。不多解释,官方文档:

Java常用类之Math类、BigInteger类

BigInteger类的成员方法

Method and Type and Description
BigInteger abs() Returns a BigInteger whose value is the absolute value of thisBigInteger.
BigInteger add(BigInteger val) Returns a BigInteger whose value is (this + val).
BigInteger and(BigInteger val) Returns a BigInteger whose value is (this & val).
BigInteger andNot(BigInteger val) Returns a BigInteger whose value is (this & ~val).
int bitCount() Returns the number of bits in the two’s complement representationof this BigInteger that differ from its sign bit.
int bitLength() Returns the number of bits in the minimal two’s-complementrepresentation of this BigInteger, excluding a sign bit.
byte byteValueExact() Converts this BigInteger to a byte, checkingfor lost information.
BigInteger clearBit(int n) Returns a BigInteger whose value is equivalent to this BigIntegerwith the designated bit cleared.
int compareTo(BigInteger val) Compares this BigInteger with the specified BigInteger,return 0,1,-1.
BigInteger divide(BigInteger val) Returns a BigInteger whose value is (this / val).
BigInteger[] divideAndRemainder(BigInteger val) Returns an array of two BigIntegers containing (this / val)followed by (this % val).
double doubleValue() Converts this BigInteger to a double.
boolean equals(Object x) Compares this BigInteger with the specified Object for equality.
float floatValue() Converts this BigInteger to a float.
BigInteger gcd(BigInteger val) Returns a BigInteger whose value is the greatest common divisor of abs(this) and abs(val).
int hashCode() Returns the hash code for this BigInteger.
int intValue() Converts this BigInteger to an int.
int intValueExact() Converts this BigInteger to an int, checkingfor lost information.
long longValue() Converts this BigInteger to a long.
long longValueExact() Converts this BigInteger to a long, checkingfor lost information.
BigInteger max(BigInteger val) Returns the maximum of this BigInteger and val.
BigInteger min(BigInteger val) Returns the minimum of this BigInteger and val.
BigInteger mod(BigInteger m) Returns a BigInteger whose value is (this mod m).
BigInteger modInverse(BigInteger m) Returns a BigInteger whose value is (this-1 mod m).
BigInteger modPow(BigInteger exponent, BigInteger m) Returns a BigInteger whose value is (this^exponent mod m).
BigInteger multiply(BigInteger val) Returns a BigInteger whose value is (this * val).
BigInteger negate() Returns a BigInteger whose value is (-this).
BigInteger xor(BigInteger val) Returns a BigInteger whose value is (this ^ val).
BigInteger subtract(BigInteger val) Returns a BigInteger whose value is (this - val).
BigInteger not() Returns a BigInteger whose value is (~this).
BigInteger pow(int exponent) Returns a BigInteger whose value is (this^exponent).
String toString() 返回当前BigInteger对象的十进制字符串表示
String toString(int p) 返回当前BigInteger对象的p进制字符串表示
BigInteger remainder(BigInteger val) 返回当前大整数对象与参数指定的大整数对象的余

例子

/*
 * 计算两个大整数的和,商,差,积,并计算一个大整数的因子个数
 */
package Liu;

import java.math.BigInteger;
import java.util.Scanner;

public class ArrayMethodDemo1 {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		BigInteger b1 = new BigInteger("987654321987654321987654321");
		BigInteger b2 = new BigInteger("123456789123456789123456789");
		//输出和
		System.out.println(b1 + "+" + b2+ "=" + b1.add(b2));
		//输出差
		System.out.println(b1 + "-" + b2+ "=" + b1.subtract(b2));
		//输出乘积
		System.out.println(b1 + "*" + b2+ "=" + b1.multiply(b2));
		//输出商
		System.out.println(b1 + "/" + b2+ "=" + b1.divide(b2));
		//求因子个数
		int num = 0;
		BigInteger m = new BigInteger("77889988");
		BigInteger Zero = new BigInteger("0");
		BigInteger One = new BigInteger("1");
		BigInteger Two = new BigInteger("2");
		
		for(BigInteger i = Two;i.compareTo(m)<0;i=i.add(One)) {
			if(m.remainder(i).compareTo(Zero)==0) {
				num++;
			}
		}
		System.out.println("大整数77889988共有" + num + "个因子");
	}
}
输出:
987654321987654321987654321+123456789123456789123456789=1111111111111111111111111110
987654321987654321987654321-123456789123456789123456789=864197532864197532864197532
987654321987654321987654321*123456789123456789123456789=121932631356500531591068431581771069347203169112635269
987654321987654321987654321/123456789123456789123456789=8
大整数77889988共有46个因子