Math的常用函数复习

package com.ethjava;

import java.math.*;

public class mathhanshulianxi {
    public static void main(String[] args) {
        int num = 36;
        Double gen = Math.sqrt(36);
        System.out.println("求平方根:" + gen);//求平方根:6.0
        int gen2 = (int) Math.sqrt(36);
        System.out.println("求平方根:" + gen2);//求平方根:6

        Double lifanggen = Math.cbrt(8);
        System.out.println("求立方根:" + lifanggen);//求立方根:2.0
        int lifanggen2 = (int) Math.cbrt(8);
        System.out.println("求立方根:" + lifanggen2);//求立方根:2

        Double jueduizhi = Math.abs(-2.0);
        System.out.println("求绝对值:" + jueduizhi);
        int jueduizhi2 = Math.abs(-2);
        System.out.println("求绝对值:" + jueduizhi2);
        int jueduizhi3 = Math.abs(-63);
        System.out.println("求绝对值:" + jueduizhi3);
        int jueduizhi4 = Math.abs(63);
        System.out.println("求绝对值:" + jueduizhi4);
        //求绝对值:2.0
        //求绝对值:2
        //求绝对值:63
        //求绝对值:63

        //计算次方
        double cifang = Math.pow(2, 3);
        System.out.println("2的3次方为:" + cifang);
        //2的3次方为:8.0
        int pingfang = (int) Math.pow(3, 2);
        System.out.println("3的2次方为:" + pingfang);
        //3的2次方为:9

        //取两个数的较大值和较小值
        int maxNum = Math.max(3, 4);
        System.out.println("两个数中较大的值:" + maxNum);
        //两个数中较大的值:4

        Double minNumDouble = Math.min(3.6, 6.3);
        System.out.println("两个数中较小的值:" + minNumDouble);
        //两个数中较小的值:3.6

        //随机数
        System.out.println(Math.random()); // [0, 1)的double类型的数
        //0.15667207273587436
        //0.4421616733285778

        //四舍五入
        long numWuru = Math.round(3.5);
        System.out.println(numWuru);//4
        //
        long numSishe=Math.round(3.1);
        System.out.println(numSishe);//3

        //Math.ceil() -- 返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入
        double a=Math.ceil(3.11);
        System.out.println(a);//4.0
        double a1=Math.ceil(3.0);
        System.out.println(a1);//3.0
        double a2=Math.ceil(3.9);
        System.out.println(a2);//4.0
        ///Math.floor() -- 返回小于等于数字参数的最大整数,对数字进行下舍入
        double b=Math.floor(3.11);
        System.out.println(b);//3.0
        double b1=Math.floor(3.99);
        System.out.println(b1);//3.0
        double b2=Math.floor(4.0);
        System.out.println(b2);//4.0

        //这里回忆起了取模的函数
        int qumo=Math.floorMod(3,2);
        System.out.println(qumo);//1
        int qumo2=Math.floorMod(-3,2);
        System.out.println(qumo);//1
        //这里的取商函数是取模的第一步,可以看取余与取模的区别的笔记
        int qushang=Math.floorDiv(-3,2);
        System.out.println(qushang);//-2
        //e的次幂
        double ecimi=Math.exp(1);
        System.out.println(ecimi);//2.718281828459045
        //log
        double log=Math.log10(10);//以10为底
        System.out.println(log);//1.0

        double log2=Math.log(ecimi);//以e为底
        System.out.println(log2);//1.0










    }
}

参考:

https://www.cnblogs.com/XingLifeng/p/11082779.html

https://blog.csdn.net/xuexiangjys/article/details/79849888

发布了45 篇原创文章 · 获赞 8 · 访问量 5859

猜你喜欢

转载自blog.csdn.net/wenyunick/article/details/103524721