• Java.lang.Math类详解


    Math类位于Java.lang包中,包含用于执行基本数学运算的方法!Math类的所有执行方法都是静态方法,可以直接使用类名.方法名调用,如:Math.round()

     常用的方法:Math.round()    返回最接近参数的 int,它表示"四舍五入"
    
                        Math.rint()         返回最接近参数并等于某一整数的 double 值,如果有2个数同样接近,则返回偶数的那个
    
                        Math.floor()       返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数
    
                        Math.ceil()         返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数
    
                        Math.cbrt()        返回 double 值的立方根
    
                        Math.sqrt()        返回正确舍入的 double 值的正平方根
    
                        Math.pow()       返回第一个参数的第二个参数次幂的值
    
                        Math.max()       返回两个 double 值中较大的一个
    
                        Math.min()        返回两个 double 值中较小的一个
    
    package 算数Math类;
    
    /**
     * @author yyx 2017年6月26日
     */
    public class MathExample {
        public static void main(String[] args) {
            sqrt(10);
            System.out.println("******************");
            pow(2, 3);
            System.out.println("******************");
            minAndMax(5, 8);
            System.out.println("******************");
            rint(4.5);
            System.out.println("******************");
            round(5.64);
            System.out.println("******************");
            floorAndCeil(2.35);
            System.out.println("******************");
            cbrt(8);
        }
        /**
         * 返回最接近参数的 int,它表示"四舍五入"
         * @param n
         */
        public static void round(double n){
            System.out.println(Math.round(n));
        }
        /**
         * Math.rint返回最接近参数并等于某一整数的 double 值,如果有2个数同样接近,则返回偶数的那个
         * @param n
         */
        public static void rint(double n){
            System.out.println(Math.rint(n));
        }
        /**
         * Math.floor返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数
         * Math.ceil返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数
         * @param n
         */
        public static void floorAndCeil(double n) {
            System.out.println(Math.floor(n));
            System.out.println(Math.ceil(n));
        }
    
        /**
         * 返回 double 值的立方根
         * 
         * @param n
         */
        public static void cbrt(double n) {
            System.out.println(Math.cbrt(n));
        }
    
        /**
         * 返回正确舍入的 double 值的正平方根
         * 
         * @param n
         */
        public static void sqrt(double n) {
            System.out.println(Math.sqrt(n));
        }
    
        /**
         * 返回第一个参数的第二个参数次幂的值
         * 
         * @param m
         * @param n
         */
        public static void pow(double m, double n) {
            System.out.println(Math.pow(m, n));
        }
    
        /**
         * max返回两个 double 值中较大的一个 min返回两个 double 值中较小的一个
         * 
         * @param m
         * @param n
         */
        public static void minAndMax(double m, double n) {
            System.out.println(Math.min(m, n));
            System.out.println(Math.max(m, n));
        }
    }
    运行结果:
    3.1622776601683795
    ******************
    8.0
    ******************
    5.0
    8.0
    ******************
    4.0
    ******************
    ******************
    2.0
    3.0
    ******************
    2.0
    

    原文:https://www.cnblogs.com/budaixiangzi/p/7079318.html

  • 相关阅读:
    AtCoder Beginner Contest 162 C~F
    题解 | 【CF896B】 Ithea Plays With Chtholly
    C# | C#快速入门
    Codeforces Round #618 (Div. 2) A~E
    Educational Codeforces Round 92 (Rated for Div. 2) A~C
    使用 Python 参与算法竞赛
    【网络爬虫学习】实战,爬取网页以及贴吧数据
    国内pip源提示“not a trusted or secure host”解决方案
    【网络爬虫学习】第一个Python爬虫程序 & 编码与解码详解 & Pythonの实现
    【网络爬虫学习】网页的基本构成
  • 原文地址:https://www.cnblogs.com/tfxz/p/12621799.html
Copyright © 2020-2023  润新知