java中常见的math方法

java.lang.Math :

 
绝对值:
static int abs(int a) 
static long abs(long a) 
static float abs(float a) 
static double abs(double a) 
 
极值:
static int max(int a, int b) 
static long max(long a, long b) 
static float max(float a, float b) 
static double max(double a, double b) 
static int min(int a, int b) 
static long min(long a, long b) 
static float min(float a, float b) 
static double min(double a, double b) 
 
三角:
static double sin(double a)          //正弦函数
static double sinh(double x)        //双曲正弦函数
 
static double cos(double a)         //余弦函数
static double cosh(double x)       //双曲余弦函数
 
static double tan(double a)         //正切函数
static double tanh(double x)       //双曲正切函数
 
static double asin(double a)       
static double acos(double a) 
static double atan(double a) 
static double atan2(double y, double x) 
 
对数,指数:
static double log(double a) 
static double log10(double a) 
static double log1p(double x)             // ln (x+1)
 
static double exp(double a)                
static double expm1(double x)           // e^a - 1
 
幂,根:
static double pow(double a, double b) 
static double sqrt(double a) 
static double cbrt(double a)                              // cube root
static double hypot(double x, double y)           // sqrt(x2 +y2)
static double scalb(double d, int scaleFactor)   // d × 2scaleFactor
static float scalb(float f, int scaleFactor)            // f × 2scaleFactor
 
随机:
static double random() 
 
最接近的整数:
static long round(double a) 
static int round(float a) 
 
浮点数:
static double nextAfter(double start, double direction) 
static float nextAfter(float start, double direction) 
static double nextDown(double d) 
static float nextDown(float f) 
static double nextUp(double d) 
static float nextUp(float f) 
static double ceil(double a) 
static double floor(double a) 
static int floorDiv(int x, int y) 
static long floorDiv(long x, long y) 
static int floorMod(int x, int y) 
static long floorMod(long x, long y) 
 
 
无溢出计算:如果溢出则抛异常
static int toIntExact(long value) 
static int addExact(int x, int y) 
static long addExact(long x, long y) 
static int subtractExact(int x, int y) 
static long subtractExact(long x, long y) 
static int multiplyExact(int x, int y) 
static long multiplyExact(long x, long y) 
static int negateExact(int a) 
static long negateExact(long a) 
static int incrementExact(int a) 
static long incrementExact(long a) 
static int decrementExact(int a) 
static long decrementExact(long a) 
 
符号:
static double signum(double d) 
static float signum(float f) 
 
角度转换:
static double toDegrees(double angrad) 
static double toRadians(double angdeg) 
 
其他:
static int getExponent(double d) 
static int getExponent(float f) 
 
static double rint(double a)                 // round int
 
static double copySign(double magnitude, double sign)        //采用 magnitude 的值和 sign 的符号
static float copySign(float magnitude, float sign) 
 
static double IEEEremainder(double f1, double f2)              // f1 除以 f2的余数
 
static double ulp(double d) 
static float ulp(float f) 
 
 
 1 package com.hone.test;
 2 
 3 /**
 4  * 测试java.lang.Math的方法
 5  * @author Xia
 6  *
 7  */
 8 public class Maths {
 9 
10     public static void main(String[] args) {
11         double d = 123.456;
12         double d2 = -123.456;
13         
14         int i = 123;
15         int b = 389;
16         System.out.printf("%.2f%n", d2);                //按照格式输出2位小数
17         System.out.printf("%.3f%n", Math.abs(d2));        //Math.abs(d2) 获取绝对值
18         
19         //------------没有溢出计算,如果有溢出则抛出异常--------------------
20         System.out.println(Math.addExact(i, b));
21         System.out.println(Math.incrementExact(i));        //如果溢出则加1   ------124
22         
23         //------------极值--------------------
24         System.out.println(Math.max(d, d2));            //选择更大的一个值      123.456
25         System.out.println(Math.min(i, b));            //选择更小的一个值          123
26         
27         //------------对数,指数--------------------
28         System.out.println(Math.log(d));                //输出以e为底的对数
29         System.out.println(Math.log10(100.0));             //输出以10为底的对数
30         System.out.println(Math.log10(100.0));             //输出以10为底的对数
31         
32         System.out.println(Math.exp(1));                 //输出以e为底的指数
33         
34         //------------幂,根:--------------------
35         System.out.println(Math.pow(d, d2));            //表示d^d2
36         System.out.println(Math.sqrt(d));                 //取d的均方根
37         System.out.println(Math.cbrt(1000));             //立方根
38         
39         //------------随机数--------------------
40         System.out.println(Math.random());                //取(0,1)之间的随机数
41         
42         //------------最接近的整数:--------------------
43         System.out.println(Math.round(d));                //四舍五入取整
44         
45         System.out.println(Math.ceil(d));                 //向上取整
46         System.out.println(Math.floor(d));                 //向下取整
47     }
48 }

 

 
 
 
 
 
 

转载于:https://www.cnblogs.com/xiaxj/p/7856496.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值