1. Math类概述

Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

2. 成员变量 和 成员方法(常用的)

(1)成员变量

 public  static  final  double PI
 public  static  final  double E

(2)成员方法

 public static int abs(int a):绝对值
public static double ceil(double a):向上取整
public static double floor(double a):向下取整
public static int max(int a,int b):最大值 (min自学)
public static double pow(double a,double b):a的b次幂
public static double random():随机数 [0.0,1.0),0.0 <= x < 1.0
public static int round(float a) 四舍五入(参数为double的自学)
public static double sqrt(double a):正平方根

3. 演示案例

 package cn.itcast_01;

 /*
* Math:用于数学运算的类。
* 成员变量:
* public static final double PI
* public static final double E
* 成员方法:
* public static int abs(int a):绝对值
* public static double ceil(double a):向上取整
* public static double floor(double a):向下取整
* public static int max(int a,int b):最大值 (min自学)
* public static double pow(double a,double b):a的b次幂
* public static double random():随机数 [0.0,1.0)
* public static int round(float a) 四舍五入(参数为double的自学)
* public static double sqrt(double a):正平方根
*/
public class MathDemo {
public static void main(String[] args) {
// public static final double PI
System.out.println("PI:" + Math.PI);
// 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 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));
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 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 sqrt(double a):正平方根
System.out.println("sqrt:"+Math.sqrt(4));
}
}

运行效果如下:

4. 总结一下Math类的方式:

 public class MathTest
{
public static void main(String[] args)
{
/*---------下面是三角运算---------*/
//将弧度转换角度
System.out.println("Math.toDegrees(1.57):"
+ Math.toDegrees(1.57));
//将角度转换为弧度
System.out.println("Math.toRadians(90):"
+ Math.toRadians(90));
//计算反余弦,返回的角度范围在 0.0 到 pi 之间。
System.out.println("Math.acos(1.2):" + Math.acos(1.2));
//计算反正弦;返回的角度范围在 -pi/2 到 pi/2 之间。
System.out.println("Math.asin(0.8):" + Math.asin(0.8));
//计算反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
System.out.println("Math.atan(2.3):" + Math.atan(2.3));
//计算三角余弦。
System.out.println("Math.cos(1.57):" + Math.cos(1.57));
//计算值的双曲余弦。
System.out.println("Math.cosh(1.2 ):" + Math.cosh(1.2 ));
//计算正弦
System.out.println("Math.sin(1.57 ):" + Math.sin(1.57 ));
//计算双曲正弦
System.out.println("Math.sinh(1.2 ):" + Math.sinh(1.2 ));
//计算三角正切
System.out.println("Math.tan(0.8 ):" + Math.tan(0.8 ));
//计算双曲正切
System.out.println("Math.tanh(2.1 ):" + Math.tanh(2.1 ));
//将矩形坐标 (x, y) 转换成极坐标 (r, thet));
System.out.println("Math.atan2(0.1, 0.2):" + Math.atan2(0.1, 0.2));
/*---------下面是取整运算---------*/
//取整,返回小于目标数的最大整数。
System.out.println("Math.floor(-1.2 ):" + Math.floor(-1.2 ));
//取整,返回大于目标数的最小整数。
System.out.println("Math.ceil(1.2):" + Math.ceil(1.2));
//四舍五入取整
System.out.println("Math.round(2.3 ):" + Math.round(2.3 ));
/*---------下面是乘方、开方、指数运算---------*/
//计算平方根。
System.out.println("Math.sqrt(2.3 ):" + Math.sqrt(2.3 ));
//计算立方根。
System.out.println("Math.cbrt(9):" + Math.cbrt(9));
//返回欧拉数 e 的n次幂。
System.out.println("Math.exp(2):" + Math.exp(2));
//返回 sqrt(x2 +y2),没有中间溢出或下溢。
System.out.println("Math.hypot(4 , 4):" + Math.hypot(4 , 4));
// 按照 IEEE 754 标准的规定,对两个参数进行余数运算。
System.out.println("Math.IEEEremainder(5 , 2):"
+ Math.IEEEremainder(5 , 2));
//计算乘方
System.out.println("Math.pow(3, 2):" + Math.pow(3, 2));
//计算自然对数
System.out.println("Math.log(12):" + Math.log(12));
//计算底数为 10 的对数。
System.out.println("Math.log10(9):" + Math.log10(9));
//返回参数与 1 之和的自然对数。
System.out.println("Math.log1p(9):" + Math.log1p(9));
/*---------下面是符号相关的运算---------*/
//计算绝对值。
System.out.println("Math.abs(-4.5):" + Math.abs(-4.5));
//符号赋值,返回带有第二个浮点数符号的第一个浮点参数。
System.out.println("Math.copySign(1.2, -1.0):"
+ Math.copySign(1.2, -1.0));
//符号函数;如果参数为 0,则返回 0;如果参数大于 0,
// 则返回 1.0;如果参数小于 0,则返回 -1.0。
System.out.println("Math.signum(2.3):" + Math.signum(2.3));
/*---------下面是大小相关的运算---------*/
//找出最大值
System.out.println("Math.max(2.3 , 4.5):" + Math.max(2.3 , 4.5));
//计算最小值
System.out.println("Math.min(1.2 , 3.4):" + Math.min(1.2 , 3.4));
//返回第一个参数和第二个参数之间与第一个参数相邻的浮点数。
System.out.println("Math.nextAfter(1.2, 1.0):"
+ Math.nextAfter(1.2, 1.0));
//返回比目标数略大的浮点数
System.out.println("Math.nextUp(1.2 ):" + Math.nextUp(1.2 ));
//返回一个伪随机数,该值大于等于 0.0 且小于 1.0。
System.out.println("Math.random():" + Math.random());
}
}

Java基础知识强化79:被遗忘的Java Math类的更多相关文章

  1. Java基础知识强化26(1):Object类之Object类的概述

    1.Object类 类Object是类层次结构的根类,每个类都使用 Object作为超类.所有对象(包括数组)都实现这个类的方法 每个类直接或者间接继承自Object类   2.Object类无参构造 ...

  2. Java基础知识强化之集合框架笔记76:ConcurrentHashMap之 ConcurrentHashMap简介

    1. ConcurrentHashMap简介: ConcurrentHashMap是一个线程安全的Hash Table,它的主要功能是提供了一组和Hashtable功能相同但是线程安全的方法.Conc ...

  3. Java基础知识强化之多线程笔记01:多线程基础知识(详见Android(java)笔记61~76)

    1. 基础知识: Android(java)学习笔记61:多线程程序的引入    ~    Android(java)学习笔记76:多线程-定时器概述和使用 

  4. Java基础知识强化104:Serializable接口 与 Parcelable接口

    1. 什么是 序列化 和 反序列化 ?     序列化 :序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间.序 ...

  5. Java基础知识强化之多线程笔记06:Lock接口 (区别于Synchronized块)

    1. 简介 我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.locks包下提供了另外一种方式 ...

  6. Java基础知识强化之网络编程笔记14:TCP之多个客户端上传到一个服务器的思考(多线程改进)

    1. 多个客户端上传到一个服务器的思考 通过while循环可以改进一个服务器接收多个客户端. 但是这个是有问题的.如果是这种情况,假设我还有张三,李四,王五这三个人分别执行客户端  张三:好好学习.a ...

  7. Java基础知识强化之IO流笔记60:打印流 之 改进复制文本文件的案例

    1. 使用打印流改进复制文本文件的案例 2. 代码示例: package cn.itcast_03; import java.io.BufferedReader; import java.io.Buf ...

  8. Java基础知识强化之网络编程笔记01:InetAddress类的概述和使用

    1. InetAddress类 InetAddress是Java对IP地址的封装,在java.net中有许多类都使用到了InetAddress,包括ServerSocket,Socket,Datagr ...

  9. Java基础知识强化之集合框架笔记34:List练习之集合的嵌套遍历

    1. 需求: 我们班有学生,每一个学生是不是一个对象.所以我们可以使用一个集合表示我们班级的学生.ArrayList<Student> 但是呢,我们旁边是不是还有班级,每个班级是不是也是一 ...

随机推荐

  1. 【C#学习笔记】一、基础知识

    1.1数据类型(P43) 类型 别名 允许的值 sbyte System.SByte -128~127 byte System.Byte 0~255 short System.Int16 -32768 ...

  2. CSP内容安全策略

    在浏览网页的过程中,尤其是移动端的网页,经常看到有很多无关的广告,其实大部分广告都是所在的网络劫持了网站响应的内容,并在其中植入了广告代码.为了防止这种情况发生,我们可以使用CSP来快速的阻止这种广告 ...

  3. cas配置全攻略(转)

    转:http://www.blogjava.net/tufanshu/archive/2011/01/21/343290.html 经过将近两天的测试,参考众多网友的贡献,终于完成了对cas的主要配置 ...

  4. POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

  5. 12 Integer to Roman(int转罗马数字Medium)

    题目意思:1-3999转罗马数字 思路:从大往小减 ps:这题有点蛋疼 class Solution { public: string intToRoman(int num) { string a[] ...

  6. ibatis ORA-00911: 无效字符

    检查下xml文件中 sql的最后是不是写了 “;” 最容易犯这个毛病,都不知道吃了多少次亏了. 什么ORA-00911: 无效字符 什么The error occurred while applyin ...

  7. Python新手学习基础之函数-可变参数**

    可变参数( ** ) 讲好了一颗*,那如果函数的最后一个参数带有 ** 前缀: 所有正常参数之外的其他的关键字参数都将被放置在一个字典中传递给函数. 要好好理解* 和 ** 两种可变参数哦~ 看个** ...

  8. CodeForces 569A 第六周比赛C踢

    C - C Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. HDU1372:Knight Moves(经典BFS题)

    HDU1372:Knight Moves(BFS)   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  10. 【Maven实战】依赖的聚合和版本管理

    1.在之前的文章中,我们已经建立了四个Maven项目,但是此时如果我们要对这四个项目进行编译打包时,必须一个一个的进行执行命令,而聚合就是指只要我们在其中一个项目中编写一些代码,则在进行此项目的编译和 ...