目錄
前言:
?MATH:
Math類中的常用方法:
總結(jié):
前言:
? ? ? ? ? ? ? ? 本篇往后我們會(huì)詳細(xì)介紹一些常用的API,今天我們介紹的是Math的常用方法。但是其實(shí)我們不需要記住所有的方法,在日常工作中自己學(xué)會(huì)查詢API文檔就可以了。
?MATH:
Math類是Java中提供的一個(gè)標(biāo)準(zhǔn)類,它包含了許多常用的數(shù)學(xué)運(yùn)算和常量,使用這些API可以方便地進(jìn)行數(shù)學(xué)計(jì)算。
Math類中的常用方法:
1. abs函數(shù):返回一個(gè)數(shù)的絕對(duì)值。它有兩個(gè)版本,一個(gè)用于整數(shù),另一個(gè)用于浮點(diǎn)數(shù)。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? int x = -5;
? ? ? ? double y = -2.75;
? ? ? ? int abs_x = Math.abs(x);
? ? ? ? double abs_y = Math.abs(y);
? ? ? ? System.out.println("abs(" + x + ") = " + abs_x);
? ? ? ? System.out.println("abs(" + y + ") = " + abs_y);
? ? }
}
輸出:
abs(-5) = 5
abs(-2.75) = 2.75
2. pow函數(shù):返回一個(gè)數(shù)的指定冪。它有兩個(gè)參數(shù),第一個(gè)是底數(shù),第二個(gè)是指數(shù)。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? double x = 2.0;
? ? ? ? int y = 3;
? ? ? ? double result = Math.pow(x, y);
? ? ? ? System.out.println(x + "^" + y + " = " + result);
? ? }
}
輸出:
2.0^3 = 8.0
3. sqrt函數(shù):返回一個(gè)數(shù)的平方根。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? double x = 16.0;
? ? ? ? double result = Math.sqrt(x);
? ? ? ? System.out.println("sqrt(" + x + ") = " + result);
? ? }
}
輸出:
sqrt(16.0) = 4.0
4. sin,cos,tan函數(shù):分別返回一個(gè)角的正弦、余弦和正切值。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? double x = 45.0 * Math.PI / 180.0;
? ? ? ? double sin_x = Math.sin(x);
? ? ? ? double cos_x = Math.cos(x);
? ? ? ? double tan_x = Math.tan(x);
? ? ? ? System.out.println("sin(" + x + ") = " + sin_x);
? ? ? ? System.out.println("cos(" + x + ") = " + cos_x);
? ? ? ? System.out.println("tan(" + x + ") = " + tan_x);
? ? }
}
輸出:
sin(0.7853981633974483) = 0.7071067811865475
cos(0.7853981633974483) = 0.7071067811865476
tan(0.7853981633974483) = 0.9999999999999999
5. max,min函數(shù):分別返回兩個(gè)數(shù)的最大值和最小值。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? int x = 5;
? ? ? ? int y = 3;
? ? ? ? int max_xy = Math.max(x, y);
? ? ? ? int min_xy = Math.min(x, y);
? ? ? ? System.out.println("max(" + x + "," + y + ") = " + max_xy);
? ? ? ? System.out.println("min(" + x + "," + y + ") = " + min_xy);
? ? }
}
輸出:
max(5,3) = 5
min(5,3) = 3
6. ceil,floor,round函數(shù):分別向上取整、向下取整和四舍五入。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? double x = 2.75;
? ? ? ? double ceil_x = Math.ceil(x);
? ? ? ? double floor_x = Math.floor(x);
? ? ? ? long round_x = Math.round(x);
? ? ? ? System.out.println("ceil(" + x + ") = " + ceil_x);
? ? ? ? System.out.println("floor(" + x + ") = " + floor_x);
? ? ? ? System.out.println("round(" + x + ") = " + round_x);
? ? }
}
輸出:
ceil(2.75) = 3.0
floor(2.75) = 2.0
round(2.75) = 3
7. exp函數(shù):返回常數(shù)e的指數(shù)函數(shù)。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? double x = 2.0;
? ? ? ? double result = Math.exp(x);
? ? ? ? System.out.println("exp(" + x + ") = " + result);
? ? }
}
輸出:
exp(2.0) = 7.3890560989306495
8. log函數(shù):返回一個(gè)數(shù)的自然對(duì)數(shù)。
public class MathDemo {
? ? public static void main(String[] args) {
? ? ? ? double x = 2.0;
? ? ? ? double result = Math.log(x);
? ? ? ? System.out.println("log(" + x + ") = " + result);
? ? }
}
輸出:
log(2.0) = 0.6931471805599453
9. ceil函數(shù):它是將一個(gè)數(shù)字向上取整,返回結(jié)果最小的整數(shù),可以理解為“取天花板”操作。
語(yǔ)法:public static double ceil(double a)
舉例:如果輸入的參數(shù)是2.5,則輸出的結(jié)果為3.0。如果輸入的參數(shù)是-2.5,則輸出的結(jié)果為-2.0。
public class CeilDemo {
? ? public static void main(String[] args) {
? ? ? ? double d1 = 2.5;
? ? ? ? double d2 = -2.5;
? ? ? ? double d3 = 2.0;
? ? ? ? System.out.println(Math.ceil(d1));
? ? ? ? System.out.println(Math.ceil(d2));
? ? ? ? System.out.println(Math.ceil(d3));
? ? }
}
輸出:
3.0
-2.0
2.0
10. floor函數(shù):與ceil函數(shù)相對(duì),它是將一個(gè)數(shù)字向下取整,返回結(jié)果最大的整數(shù),可以理解為“取地板”操作。
語(yǔ)法:public static double floor(double a)
舉例:如果輸入的參數(shù)是2.5,則輸出的結(jié)果為2.0。如果輸入的參數(shù)是-2.5,則輸出的結(jié)果為-3.0。
public class FloorDemo {
? ? public static void main(String[] args) {
? ? ? ? double d1 = 2.5;
? ? ? ? double d2 = -2.5;
? ? ? ? double d3 = 2.0;
? ? ? ? System.out.println(Math.floor(d1));
? ? ? ? System.out.println(Math.floor(d2));
? ? ? ? System.out.println(Math.floor(d3));
? ? }
}
輸出:
2.0
-3.0
2.0
11. round函數(shù):它是對(duì)一個(gè)數(shù)字進(jìn)行四舍五入,返回結(jié)果最接近原始數(shù)字的整數(shù), 如果距離兩側(cè)的整數(shù)一樣,則返回距離較大的那個(gè)整數(shù)(例如,對(duì)1.5進(jìn)行四舍五入,輸出的結(jié)果是2.0)。
語(yǔ)法:public static long round(double a)
舉例:如果輸入的參數(shù)是2.4,則輸出的結(jié)果是2。如果輸入的參數(shù)是2.6,則輸出的結(jié)果是3。
public class RoundDemo {
? ? public static void main(String[] args) {
? ? ? ? double d1 = 2.4;
? ? ? ? double d2 = 2.6;
? ? ? ? System.out.println(Math.round(d1));
? ? ? ? System.out.println(Math.round(d2));
? ? }
}
輸出:
2
3
總結(jié):
? ? ? ? 本篇我們?cè)敿?xì)的介紹了math類中的幾個(gè)常見(jiàn)的方法,各位只需要大概知道就好了,在實(shí)際生活中我們?nèi)绻行枰梢圆殚咥PI文檔。
如果我的內(nèi)容對(duì)你有幫助,請(qǐng)點(diǎn)贊,評(píng)論,收藏。創(chuàng)作不易,大家的支持就是我堅(jiān)持下去的動(dòng)力!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-609116.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-609116.html
到了這里,關(guān)于【從零開(kāi)始學(xué)習(xí)JAVA | 第二十篇】常見(jiàn)API介紹 Math的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!