当前位置: 首页 > news >正文

蓝桥杯备考随手记: Math 类中常用方法

Java的Math类是一个包含数学操作方法的实用工具类。它提供了许多用于执行各种数学计算的静态方法。

下面是Math类中一些常用的方法:

  1. abs():返回参数的绝对值。
    int absoluteValue = Math.abs(-10);
    System.out.println(absoluteValue); // Output: 10
  2. ceil():返回大于或等于参数的最小整数。
    double ceilValue = Math.ceil(3.14);
    System.out.println(ceilValue); // Output: 4.0
  3. floor():返回小于或等于参数的最大整数。
    double floorValue = Math.floor(3.14);
    System.out.println(floorValue); // Output: 3.0
    
  4. round():返回四舍五入到最接近参数的整数。
    long roundedValue = Math.round(3.14);
    System.out.println(roundedValue); // Output: 3
  5. max():返回两个参数中较大的那个值。
    int maxValue = Math.max(10, 5);
    System.out.println(maxValue); // Output: 10
    
  6. min():返回两个参数中较小的那个值。
    int minValue = Math.min(10, 5);
    System.out.println(minValue); // Output: 5
    
  7. pow():返回第一个参数的第二个参数次幂。
    double power = Math.pow(2, 3);
    System.out.println(power); // Output: 8.0
    
  8. sqrt():返回参数的平方根。
    double squareRoot = Math.sqrt(9);
    System.out.println(squareRoot); // Output: 3.0
  9. random():返回一个大于等于0.0且小于1.0的随机数。
    double randomNumber = Math.random();
    System.out.println(randomNumber); // Output: 0.123456789 (随机值)
    
  10. sin()、cos()、tan():返回参数的正弦、余弦和正切。
    double sinValue = Math.sin(Math.PI/2);
    System.out.println(sinValue); // Output: 1.0double cosValue = Math.cos(Math.PI);
    System.out.println(cosValue); // Output: -1.0double tanValue = Math.tan(0);
    System.out.println(tanValue); // Output: 0.0
    

这些只是Math类中的一小部分常用方法,还有其他方法可以进行更复杂的数学操作。根据具体需求,我们可以选择合适的方法来执行数学计算。熟悉这些方法可以大大简化数值计算的过程,提高效率。

http://www.lryc.cn/news/328789.html

相关文章:

  • 外包干了4年,技术退步明显。。。。
  • 亚远景科技-Hardware Engineering SPICE课程大纲
  • JDK8的下载安装与环境变量配置教程
  • 深入探讨分布式ID生成方案
  • 花钱的艺术:消费和投资如何分配
  • git 代码库查看方法
  • MySql的下载与安装
  • python学习17:python中的while循环
  • Android中的导航navigation的使用
  • Clip算法解读
  • 使用第三方远程连接工具ssh连接vagrant创建的虚拟机
  • linux查找指定目录下包含指定字符串文件,包含子目录
  • 27. BI - PageRank 的那些相关算法 - PersonRank, TextRank, EdgeRank
  • [数据集][目标检测]公共场所危险物品检测数据集VOC+YOLO格式1431张6类别
  • 创业项目开发(持续更新)
  • 基于SpringBoot的“校园台球厅人员与设备管理系统”的设计与实现(源码+数据库+文档+PPT)
  • 【Java数据结构】关于栈的操作出栈,压栈,中缀表达式,后缀表达式,逆波兰表达式详解
  • wireshark 使用
  • C++纯虚函数的使用
  • 读所罗门的密码笔记06_共生思想(上)
  • QA:ubuntu22.04.4桌面版虚拟机鼠标丢失的解决方法
  • idea从零开发Android 安卓 (超详细)
  • 1.5T数据惨遭Lockbit3.0窃取,亚信安全发布《勒索家族和勒索事件监控报告》
  • 喜讯!聚铭网络荣获《日志分类方法及系统》发明专利
  • 每日一博 - 关于日志记录的最佳实践
  • 针对pycharm打开新项目需要重新下载tensorflow的问题解决
  • <商务世界>《第29课 外贸展会上应注意的事项》
  • sklearn主成分分析PCA
  • linux命令之tput
  • python基础——文件操作【文件编码、文件的打开与关闭操作、文件读写操作】