Java 之 数学相关类 Math、BigInteger、BigDecimal
2024-10-20 20:43:32
一、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));//往大的方向舍去
}
最新文章
- SPIRE.DOC - .NET开发者的福利
- HashMap的一些用法
- jquery动画效果---animate()--滚屏
- 基于swift语言iOS8的蓝牙连接(初步)
- 如何提高手机APP的用户体验?
- 体验Openstack--感觉这个比HADOOP还要基础啊
- 开启g++ 编辑器 c++11特性
- service入门—电话监听器
- 第四十三节,文件、文件夹、压缩包、处理模块shutil
- OpenShift实战(一):OpenShift高级安装
- Android快速开发常用知识点系列目录
- HBase应用快速开发
- C++ 中递归实现 二项式展开式(a+b)^ n 的表达式
- 星云的Linux专用学习手册
- Nginx技术研究系列3-OpenResty安装配置
- Java 日期与数字转换
- 详解python2 和 python3的区别-乾颐堂
- 【codeforces】【比赛题解】#851 CF Round #432 (Div.2)
- iOS-高仿微信摇一摇动画效果加震动音效
- MyEclipse 对项目进行build path无效