java实现Fmeasure计算_Java基础-007-Java数学运算

Java数学运算

基本数字

通常使用内置数据类型,如:byte、short、int、long、float、double 。

public class HelloWorld {

public static void main(String[] args) {

byte a = 0x32;

short b = 50;

int c = 50;

long d = 50;

float e = 50.001F;

double f = 50.001D;

System.out.println("a = " + a);

System.out.println("b = " + b);

System.out.println("c = " + c);

System.out.println("d = " + d);

System.out.println("e = " + e);

System.out.println("f = " + f);

}

}

数字对象Number

Java语言为每一个内置数据类型提供了对应的包装类。 所有的包装类(Byte、Short、Integer、Long、Float、Double)都是抽象类Number的子类。 由编译器特别支持的包装称为装箱,所以当内置数据类型被当作对象使用的时候,编译器会把内置类型装箱为包装类。相似的,编译器也可以把一个对象拆箱为内置类型。Number类属于java.lang包。

public static void main(String[] args) {

Integer x = 5;

x = x + 10;

System.out.println(x);

}

当x被赋为整型值时,由于x是一个对象,所以编译器要对x进行装箱。然后,为了使x能进行加运算,所以要对x进行拆箱。

BigInteger/BigDecimal

BigInteger/BigDecimal的父类java.lang.Number 。Java Platform SE 8

public static void main(String[] args) {

// 这几个测试,是为了简单超过int范围内,Integer就不能再表示

System.out.println(Integer.MAX_VALUE);

// NumberFormatException

// Integer i = new Integer("2147483648");

// System.out.println(i);

// 通过大整数来创建对象

BigInteger bi = new BigInteger("2147483648");

System.out.println("bi:" + bi);

BigInteger bi1 = new BigInteger("100");

BigInteger bi2 = new BigInteger("50");

// public BigInteger add(BigInteger val):加

System.out.println("add:" + bi1.add(bi2));

// public BigInteger subtract(BigInteger val):减

System.out.println("subtract:" + bi1.subtract(bi2));

// public BigInteger multiply(BigInteger val):乘

System.out.println("multiply:" + bi1.multiply(bi2));

// public BigInteger divide(BigInteger val):除

System.out.println("divide:" + bi1.divide(bi2));

// public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组

BigInteger[] bis = bi1.divideAndRemainder(bi2);

System.out.println("商:" + bis[0]);

System.out.println("余数:" + bis[1]);

}

public static void main(String[] args) {

BigDecimal bd1 = new BigDecimal("0.09");

BigDecimal bd2 = new BigDecimal("0.01");

System.out.println("add:" + bd1.add(bd2));

System.out.println("-------------------");

BigDecimal bd3 = new BigDecimal("1.0");

BigDecimal bd4 = new BigDecimal("0.32");

System.out.println("subtract:" + bd3.subtract(bd4));

System.out.println("-------------------");

BigDecimal bd5 = new BigDecimal("1.015");

BigDecimal bd6 = new BigDecimal("100");

System.out.println("multiply:" + bd5.multiply(bd6));

System.out.println("-------------------");

BigDecimal bd7 = new BigDecimal("1.301");

BigDecimal bd8 = new BigDecimal("100");

System.out.println("divide:" + bd7.divide(bd8));

System.out.println("divide:" + bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));

System.out.println("divide:" + bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));

}

Java Math类

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

Modifier and TypeMethodDescriptionstatic doubleabs(double a)Returns the absolute value of a double value.static floatabs(float a)Returns the absolute value of a float value.static intabs(int a)Returns the absolute value of an int value.static longabs(long a)Returns the absolute value of a long value.static doubleacos(double a)Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.static intaddExact(int x, int y)Returns the sum of its arguments, throwing an exception if the result overflows an int.static longaddExact(long x, long y)Returns the sum of its arguments, throwing an exception if the result overflows a long.static doubleasin(double a)Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.static doubleatan(double a)Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.static doubleatan2(double y, double x)Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).static doublecbrt(double a)Returns the cube root of a double value.static doubleceil(double a)Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.static doublecopySign(double magnitude, double sign)Returns the first floating-point argument with the sign of the second floating-point argument.static floatcopySign(float magnitude, float sign)Returns the first floating-point argument with the sign of the second floating-point argument.static doublecos(double a)Returns the trigonometric cosine of an angle.static doublecosh(double x)Returns the hyperbolic cosine of a double value.static intdecrementExact(int a)Returns the argument decremented by one, throwing an exception if the result overflows an int.static longdecrementExact(long a)Returns the argument decremented by one, throwing an exception if the result overflows a long.static doubleexp(double a)Returns Euler’s number e raised to the power of a double value.static doubleexpm1(double x)Returns ex -1.static doublefloor(double a)Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.static intfloorDiv(int x, int y)Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.static longfloorDiv(long x, long y)Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.static intfloorMod(int x, int y)Returns the floor modulus of the int arguments.static longfloorMod(long x, long y)Returns the floor modulus of the long arguments.static intgetExponent(double d)Returns the unbiased exponent used in the representation of a double.static intgetExponent(float f)Returns the unbiased exponent used in the representation of a float.static doublehypot(double x, double y)Returns sqrt(x2 +y2) without intermediate overflow or underflow.static doubleIEEEremainder(double f1, double f2)Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.static intincrementExact(int a)Returns the argument incremented by one, throwing an exception if the result overflows an int.static longincrementExact(long a)Returns the argument incremented by one, throwing an exception if the result overflows a long.static doublelog(double a)Returns the natural logarithm (base e) of a double value.static doublelog10(double a)Returns the base 10 logarithm of a double value.static doublelog1p(double x)Returns the natural logarithm of the sum of the argument and 1.static doublemax(double a, double b)Returns the greater of two double values.static floatmax(float a, float b)Returns the greater of two float values.static intmax(int a, int b)Returns the greater of two int values.static longmax(long a, long b)Returns the greater of two long values.static doublemin(double a, double b)Returns the smaller of two double values.static floatmin(float a, float b)Returns the smaller of two float values.static intmin(int a, int b)Returns the smaller of two int values.static longmin(long a, long b)Returns the smaller of two long values.static intmultiplyExact(int x, int y)Returns the product of the arguments, throwing an exception if the result overflows an int.static longmultiplyExact(long x, long y)Returns the product of the arguments, throwing an exception if the result overflows a long.static intnegateExact(int a)Returns the negation of the argument, throwing an exception if the result overflows an int.static longnegateExact(long a)Returns the negation of the argument, throwing an exception if the result overflows a long.static doublenextAfter(double start, double direction)Returns the floating-point number adjacent to the first argument in the direction of the second argument.static floatnextAfter(float start, double direction)Returns the floating-point number adjacent to the first argument in the direction of the second argument.static doublenextDown(double d)Returns the floating-point value adjacent to d in the direction of negative infinity.static floatnextDown(float f)Returns the floating-point value adjacent to f in the direction of negative infinity.static doublenextUp(double d)Returns the floating-point value adjacent to d in the direction of positive infinity.static floatnextUp(float f)Returns the floating-point value adjacent to f in the direction of positive infinity.static doublepow(double a, double b)Returns the value of the first argument raised to the power of the second argument.static doublerandom()Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.static doublerint(double a)Returns the double value that is closest in value to the argument and is equal to a mathematical integer.static longround(double a)Returns the closest long to the argument, with ties rounding to positive infinity.static intround(float a)Returns the closest int to the argument, with ties rounding to positive infinity.static doublescalb(double d, int scaleFactor)Returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.static floatscalb(float f, int scaleFactor)Returns f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.static doublesignum(double d)Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.static floatsignum(float f)Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.static doublesin(double a)Returns the trigonometric sine of an angle.static doublesinh(double x)Returns the hyperbolic sine of a double value.static doublesqrt(double a)Returns the correctly rounded positive square root of a double value.static intsubtractExact(int x, int y)Returns the difference of the arguments, throwing an exception if the result overflows an int.static longsubtractExact(long x, long y)Returns the difference of the arguments, throwing an exception if the result overflows a long.static doubletan(double a)Returns the trigonometric tangent of an angle.static doubletanh(double x)Returns the hyperbolic tangent of a double value.static doubletoDegrees(double angrad)Converts an angle measured in radians to an approximately equivalent angle measured in degrees.static inttoIntExact(long value)Returns the value of the long argument; throwing an exception if the value overflows an int.static doubletoRadians(double angdeg)Converts an angle measured in degrees to an approximately equivalent angle measured in radians.static doubleulp(double d)Returns the size of an ulp of the argument.static floatulp(float f)Returns the size of an ulp of the argument.

public static void main(String[] args) {

// public static final double PI

System.out.println("PI:" + Math.PI);

System.out.println("90 度的正弦值:" + Math.sin(Math.PI / 2));

System.out.println("0度的余弦值:" + Math.cos(0));

System.out.println("60度的正切值:" + Math.tan(Math.PI / 3));

System.out.println("1的反正切值: " + Math.atan(1));

System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI / 2));

System.out.println("--------------");

// public static final double E

System.out.println("E:" + Math.E);

System.out.println("--------------");

// public static int abs(int a):绝对值

System.out.println("abs:" + Math.abs(10));

System.out.println("abs:" + Math.abs(-10));

System.out.println("--------------");

// public static int max(int a,int b):最大值

System.out.println("max:" + Math.max(12, 23));

// 需求:我要获取三个数据中的最大值

// 方法的嵌套调用

System.out.println("max:" + Math.max(Math.max(12, 23), 18));

// 需求:我要获取四个数据中的最大值

System.out.println("max:" + Math.max(Math.max(12, 78), Math.max(34, 56)));

System.out.println("--------------");

// public static double pow(double a,double b):a的b次幂

System.out.println("pow:" + Math.pow(2, 3));

System.out.println("--------------");

// public static double random():随机数 [0.0,1.0)

System.out.println("random:" + Math.random());

// 获取一个1-100之间的随机数

System.out.println("random:" + ((int) (Math.random() * 100) + 1));

System.out.println("--------------");

// public static double sqrt(double a):正平方根

System.out.println("sqrt:" + Math.sqrt(4));

System.out.println("--------------");

// public static int round(float a) 四舍五入(参数为double的自学)

System.out.println("round:" + Math.round(12.34f));

System.out.println("round:" + Math.round(12.56f));

System.out.println("--------------");

// public static double ceil(double a):向上取整

System.out.println("ceil:" + Math.ceil(12.34));

System.out.println("ceil:" + Math.ceil(12.56));

System.out.println("--------------");

// public static double floor(double a):向下取整

System.out.println("floor:" + Math.floor(12.34));

System.out.println("floor:" + Math.floor(12.56));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值