Java 之 数学相关类 Math、BigInteger、BigDecimal
一、java.lang.Math 类
一、Math 类概述
java.lang.Math 类包含用于执行基本数学运算的方法,如指数、对数、平方根和三角函数。类似于这样的类,其所有方法均为静态方法,并且不会创建对象,调用非常简单。
二、基本运算的方法
public static double abs(double a) : 返回 double 值的绝对值
public static double ceil(double a) : 返回大于等于参数的最小的整数。
public static double floor(double a) :返回小于等于参数最大的整数。
public static long round(double a) : 返回最接近参数的 long。(相当于四舍五入方法)
random() 返回0.0到1.0的随机数
Math.PI 代表近似的圆周率常量(double)
sqrt 平方根
pow(double a,doble b) a的b次幂
log 自然对数
exp e为底指数
max(double a,double b)
min(double a,double b)
toDegrees(double angrad) 弧度—>角度
toRadians(double angdeg) 角度—>弧度
acos,asin,atan,cos,sin,tan 三角函数
Demo:使用 Math 相关的API,计算在 -10.8 到 5.9 之间,绝对值大于 6 或者小于 2.1 的整数有多少个?
public class MathTest {
public static void main(String[] args) {
// 定义最小值
double min = ‐10.8;
// 定义最大值
double max = 5.9;
// 定义变量计数
int count = 0;
// 范围内循环
for (double i = Math.ceil(min); i <= max; i++) {
// 获取绝对值并判断
if (Math.abs(i) > 6 || Math.abs(i) < 2.1) {
// 计数
count++;
}
}
System.out.println("个数为: " + count + " 个");
}
}
二、 java.math包的BigInteger和BigDecimal
1、BigInteger
Integer类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的,最大为263-1如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算了。
java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。
(1)构造方法
BigInteger(String val):根据字符串构建BigInteger对象
(2)常用方法
BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger。
BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger。
BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger。
BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数相除只保留整数部分。
BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。
BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。
Demo:
@Test
public void test1(){
// long num1 = 12345678901234567890L;//out of range 超过long的范围
BigInteger num1 = new BigInteger("12345678901234567890");
BigInteger num2 = new BigInteger("92345678901234567890"); // System.out.println("和:" + (num1 + num2));//错误的
System.out.println("和:" + num1.add(num2));
System.out.println("减:" + num1.subtract(num2));
System.out.println("乘:" + num1.multiply(num2));
System.out.println("除:" + num2.divide(num1));//两个整数相除只保留整数部分
System.out.println("幂次方:" + num2.pow(5));
}
2、BigDecimal
一般的Float类和Double类可以用来做科学计算或工程计算,但是在商业计算中,要求数字精度比较高,所以用到java.math.BigDecimal类。BigDecimal类支持不可变的、任意精度的有符号十进制定点数。
(1)构造方法
BigDecimal(double val)
BigDecimal(String val)
(2)常用方法
BigDecimal add(BigDecimal augend) :返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。
BigDecimal subtract(BigDecimal subtrahend) :返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。
BigDecimal multiply(BigDecimal multiplicand):返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。
BigDecimal pow(int n) :返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。
BigDecimal divide(BigDecimal divisor): 返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException。
BigDecimal divide(BigDecimal divisor, int roundingMode) :返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。
BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) :返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
Demo:
@Test
public void test2(){
BigDecimal num1 = new BigDecimal("-12.1234567890123456567899554544444332");
BigDecimal num2 = new BigDecimal("89.6734567890123456567899554544444333");
System.out.println("和:" + num1.add(num2));
System.out.println("减:" + num1.subtract(num2));
System.out.println("乘:" + num1.multiply(num2));
System.out.println("除:" + num2.divide(new BigDecimal("2")));//可以整除(除尽)就对,不能整除就报异常
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_HALF_UP));
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_DOWN));//往零的方向舍去
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_FLOOR));//往小的方向舍去
System.out.println("除:" + num2.divide(num1,BigDecimal.ROUND_CEILING));//往大的方向舍去
}
Java 之 数学相关类 Math、BigInteger、BigDecimal的更多相关文章
- Java基础语法<五> 大数值BigInteger BigDecimal
笔记整理 来源于<Java核心技术卷 I > <Java编程思想> 如果基本的整数和浮点数精度不能够满足需求,那么可以使用java.math包中的两个很有平有用的类:BigIn ...
- 17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal
01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据, ...
- Python学习笔记17:标准库之数学相关(math包,random包)
前面几节看得真心累.如今先来点简单easy理解的内容. 一 math包 math包主要处理数学相关的运算. 常数 math.e # 自然常数e math.pi # 圆周率pi 运算函数 math ...
- 数学工具类Math
概述 java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数.类似这样的工具 类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单 基本运算的方法 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第8节 Math类_18_数学工具类Math
常用几个数学的方法 abs绝对值 ceil向上取整,它并不是四舍五入 floor向下取整 round四舍五入 PI 按住Ctrl+鼠标左键 进入Math这个类的源码里面 Ctrl+F12 然后输入PI ...
- Java线程池相关类-Executor框架
1.Executor 接口源码: public interface Executor { /** * Executes the given command at some time in the fu ...
- Java工具类——数学相关的类
Java工具类--数学相关的类 在上一篇文章中,我们系统学习了 Java 里面的包装类,那么这篇文章,我们就来学习一下Java提供好的类--数学相关的类. 一.数学类介绍 在最早期学习 Java 基础 ...
- Java 数学操作类
数学操作类 Math类 数学计算操作类 类属性值 Math.E ^ Math.PI 圆周率 类方法 Math类中,一切方法都是 static 型,因为Math类中没有普通属性. round() 方法 ...
- 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类
Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...
随机推荐
- 05-C#笔记-基本变量
1. 不支持括号初始化: 2. 支持强制类型转化: 3.运算规则同C++ 参考: http://www.runoob.com/csharp/csharp-variables.html
- python nose 自写插件打乱class类中用例执行顺序,但将test_a和test_z排除
在使用nose时,有这样一个需求,用例执行打乱,但部分用例因场景原因必须先执行,这类用例在写用例时人为的加上了test_a或test_z字样 网上找了一圈,都没找到合适的方法,只有自己写插件了 已写完 ...
- 解决.Net Core 3.0 不支持 Autofac 问题
Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading ...
- Windbg Scratch Pad(便笺簿)窗口的使用
“便笺簿”窗口是一个剪贴板,您可以在其中键入和保存文本. 打开便笺簿 通过菜单View--->Scratch Pad 通过快捷键Alt+8 通过工具栏 使用便笺簿 用上面的方式打开的窗口如下: ...
- Python power函数
power函数 from math import pow def power(x, y): if y == 0: return 1 tot = 1 for i in range(y): tot *= ...
- 深入js系列-环境
javascript运行环境 js如果只在引擎中运行,它会严格遵循并且可以预测的,但是js几乎都在宿主环境中运行,浏览器或者Node环境 ECMAScript中的Annex B 介绍了浏览器兼容性问题 ...
- Echarts数据更新大坑
今天使用了一个Echarts来实现柱状图和直线图统计组合,每次通过axios(ajax库)来请求新数据来刷新数据,但是发现请求数据确实是对应变化到了options变量中,后台数据条数只有一条,但是图表 ...
- Generator生成器函数执行过程的理解
一个最基本的Generator函数格式如下,函数体内部要使用yield修饰符则必须在函数名前加上*号 ; function *testYield(x){ console.log('before yie ...
- github执行clone操作时报错
在执行github上的clone操作时,报 ssh_exchange_identification: Connection closed by remote host 在网上找了好多种解决办法,都没有 ...
- solr配置同义词,停止词,和扩展词库(IK分词器为例)
定义 同义词:搜索结果里出现的同义词.如我们输入”还行”,得到的结果包括同义词”还可以”. 停止词:在搜索时不用出现在结果里的词.比如is .a .are .”的”,“得”,“我” 等,这些词会在句子 ...