盘点Math类中取整函数、三角函数和指数函数方法

开发 前端
Math类取整函数方法有ceil、floor、rint、round,这些方法通过例子了解它的用法。Math类三角函数方法有sin、cos、tan、toRadians、toDegrees等,这些方法通过例子了解它的用法。

[[397803]]

大家好,我是Java进阶者,今天小编带大家一起来学习Java技术基础!

一、Math类取整函数方法

1.Math类取整函数方法,如下所示:

public static double ceil(double a)方法:返回double类值的最小值,这个值大于或等于。简单来说是向上取整;

public static double floor(double a)方法:返回double类值的最大值,这个值小于或等于。简单来说是向下取整;

public static double rint(double a)方法:返回最接近的参数a的值,并且它的值是double类型的值;

public static int round(float a)方法:返回最接近的参数加上0.5将结果转换为int类型,也就是四舍五入取整;

public static long round(double a)方法:返回最接近的参数加上0.5将结果转换为long类型,也就是四舍五入取整;

2.Math类取整函数方法例子:

  1. public class p71 { 
  2. public static void main(String[] args) { 
  3.         // TODO Auto-generated method stub 
  4.         System.out.println("ceil()方法 :"+Math.ceil(2.1)); 
  5.         System.out.println("ceil()方法 :"+Math.ceil(2.5)); 
  6.         System.out.println("ceil()方法 :"+Math.ceil(2.8)); 
  7.         System.out.println("floor()方法 :"+Math.floor(1.1)); 
  8.         System.out.println("floor()方法 :"+Math.floor(1.5)); 
  9.         System.out.println("floor()方法 :"+Math.floor(1.8)); 
  10.         System.out.println("rint()方法 :"+Math.rint(3.1)); 
  11.         System.out.println("rint()方法 :"+Math.rint(3.5)); 
  12.         System.out.println("rint()方法 :"+Math.rint(3.8)); 
  13.         System.out.println("round()方法 :"+Math.round(5.1)); 
  14.         System.out.println("round()方法 :"+Math.round(5.5)); 
  15.         System.out.println("round()方法 :"+Math.round(5.8)); 
  16.   } 

运行的结果是:

二、Math类三角函数方法

1.Math类三角函数方法,如下所示:

public static double sin(double a)方法:返回参数的正弦值,a是以弧度表示角度;

public static double cos(double a)方法:返回参数的余弦值,a是以弧度表示角度;

public static double tan(double a)方法:返回参数的正切值,a是以弧度表示角度;

public static double asin(double a)方法:返回参数的反正弦值;

public static double acos(double a)方法:返回参数的反余弦值;

public static double atan(double a)方法:返回参数的反正切值;

public static double toRadians(double a) : 把角度转换为弧度;

public static doueble toDegrees(double a) : 把弧度转化为角度;

2.Math类三角函数方法例子

  1. public class p72 { 
  2. public static void main(String[] args) { 
  3.         // TODO Auto-generated method stub 
  4.         double p=Math.PI; 
  5.         System.out.println("30度的正弦值:"+Math.sin(p/6)); 
  6.         System.out.println("90度的正弦值:"+Math.sin(p/2)); 
  7.         System.out.println("0度的余弦值:"+Math.cos(0)); 
  8.         System.out.println("30度的余弦值:"+Math.cos(p/6)); 
  9.         System.out.println("1的反正切值:"+Math.atan(1)); 
  10.         System.out.println("60度的弧度值:" + Math.toRadians(60.0));   
  11.   } 

运行的结果是:

三、Math类指数函数方法

1.Math类指数函数方法,如下所示:

public static double sqrt(double a ):用来取a的平方根(a2);

public static double cbrt(double a ):用来取a的立方根(a3);

public static double log(double a ):相当于lna;

public static double log10(double a ):以10为底的对数,也就是log10a;

public static double exp(double a ):用来获取e的a次方;

public static double pow(double a,double b):a表示底数,b表示指数,用来求a的b次方;

2.Math类指数函数方法例子:

  1. public class p73 { 
  2. public static void main(String[] args) { 
  3.         // TODO Auto-generated method stub 
  4.         System.out.println("e的二次方"+Math.exp(2)); 
  5.         System.out.println("2的立方值:"+Math.pow(2, 3)); 
  6.         System.out.println("9的平方根:"+Math.sqrt(9)); 
  7.         System.out.println("10为底 10的对数:"+Math.log10(10)); 
  8.   } 

运行的结果是:

四、总结

本文主要介绍了Math类取整函数方法、三角函数方法、指数函数方法。

Math类取整函数方法有ceil、floor、rint、round,这些方法通过例子了解它的用法。Math类三角函数方法有sin、cos、tan、toRadians、toDegrees等,这些方法通过例子了解它的用法。

Math类指数函数方法有sqrt、cbrt、log、log10等,这些方法通过例子了解它的用法。希望大家通过本文的学习,对你有所帮助!

本文转载自微信公众号「Java进阶学习交流」,作者Java进阶者。转载本文请联系Java进阶学习交流公众号。

 

责任编辑:武晓燕 来源: Java进阶学习交流
相关推荐

2021-07-30 06:58:27

python实现三角函数

2021-07-27 05:04:12

python初等函数

2021-02-25 18:05:31

Plots图形绘图应用Linux

2022-04-19 06:27:13

CSS数学函数calc

2020-07-06 14:30:47

前端三角函数动画

2020-10-27 11:24:29

avaScript m

2009-12-01 19:02:20

PHP取整函数

2009-09-01 15:47:20

C#取整函数

2021-06-05 23:39:52

c++函数场景

2021-07-16 07:57:35

JavaScriptEval函数

2009-12-11 17:45:09

PHP Math函数

2010-03-11 17:46:29

Pythond类

2021-08-31 10:01:04

JavaScript函数属性

2010-09-14 17:27:27

SQL函数

2009-07-31 16:06:50

成员函数构造函数C#

2010-09-09 09:31:19

SQL函数left

2016-10-20 13:36:28

WebRTC浏览器服务器

2021-10-09 07:10:30

JavaScriptBigIn函数

2009-02-24 16:17:41

日期时间函数

2010-09-16 09:15:59

SQL函数
点赞
收藏

51CTO技术栈公众号