Java.Math

来源:互联网 发布:噪声计算软件 编辑:程序博客网 时间:2024/05/18 15:53

Java.Math是一个很常用的类,其中有很多方法在开发中用到。这个类中的所有方法都是静态的,所以使

用格式是:Math.方法名。下面列出一些常用方法:

abs:
static int abs(int a),返回int值的绝对值;
static long abs(long a),返回long值的绝对值;
static float abs(float a),返回float值的绝对值;
static double abs(double a),返回double值的绝对值;

max:
static int max(int a, int b),返回两个 int 值中较大的一个;
static long max(long a, long b),返回两个 long 值中较大的一个;
static float max(float a, float b),返回两个 float 值中较大的一个;
static double max(double a, double b),返回两个 double 值中较大的一个;

min:
static int min(int a, int b),返回两个 int 值中较小的一个;
static long min(long a, long b),返回两个 long 值中较小的一个;
static float min(float a, float b),返回两个 float 值中较小的一个;
static double min(double a, double b),返回两个 double 值中较小的一个;

random:
static double random(),返回带正号的 double 值,大于或等于 0.0,小于 1.0;

round:
static int round(float a),返回最接近参数的 int;
static long round(double a),返回最接近参数的 long;

sqrt,cbrt:
static double sqrt(double a),返回正确舍入的 double 值的正平方根;
static double cbrt(double a),返回 double 值的立方根;

rint:
static double rint(double a),返回其值最接近参数并且是整数的 double 值;

正弦,余弦,正切:
static double sin(double a),返回角的三角正弦;
static double cos(double a),返回角的三角余弦;
static double tan(double a),返回角的三角正切;

反正弦,反余弦,反正切:
static double asin(double a),返回角的反正弦,范围在 -pi/2 到 pi/2 之间;
static double acos(double a),返回角的反余弦,范围在 0.0 到 pi 之间;
static double atan(double a),返回角的反正切,范围在 -pi/2 到 pi/2 之间;

floor:
static double floor(double a),返回最大的(最接近正无穷大)double 值,该值小于或等于参数,并且等于某个整数。

下面有一些代码及运行结果:

 double a = Math.sqrt(66);                     8.12403840463596                          平方根
 double b = Math.random();                  0.6944619576018056                      随机数
 int c = Math.round(5.5f);                        6   
 long d = Math.round(5.4d);                  5
 double f = Math.cbrt(5);                        1.709975946676697                         立方根
 double e = Math.rint(5.6);                    6.0

想获得更多信息,请查询API. 

原创粉丝点击