zoukankan      html  css  js  c++  java
  • java笔记 -- 数学函数与常量

    Math类中, 包含了各种数学函数
      不用在数学方法名和常量名前添加前缀Math., 只要在源文件的顶部加上这行代码:
        import static java.lang.Math.*; (静态导入)
          例: System.out.println("The square root of u03C0 is " + sqrt(PI));
          // The square root of π is 1.7724538509055159

      tap: 在Math类中, 为了达到最快的性能, 所有的方法都使用计算浮点单元中的例程.
      如果需要一个完全可预测的结果, 可以使用StrictMath类, 确保在所有平台上得到相同的结果.

    • 平方根:

    double x = 4;
    double y = Math.sqrt(x);
    System.out.println(y); // 2.0

    println方法操作一个定义在System类中的System.out对象,
    但是Math类中的sqrt方法处理的不是对象, 这样的方法被称为静态方法.

    • 幂运算:

    double y = Math.pow(x, a); // 将y值设置为x的a次幂
    pow方法有两个double类型的参数, 结果返回double类型.

    • 三角函数:

    Math.sin
    Math.cos
    Math.tan
    Math.atan
    Math.atan2

    • 指数函数,对数

    Math.exp
    Math.log
    Math.log10

    • 常量π, e

    Math.PI
    Math.E

    // Constants.java
    
    package com.picc.sample.firstsample;
    import static java.lang.Math.*;
    
    public class Constants {
        public static void main(String[] args) {        
            System.out.println("The square root of u03C0 is " + sqrt(PI));
            // The square root of π is 1.7724538509055159
    
            double x = 9.997;
            System.out.println(Math.round(x));  // 10
            
        }
    }
  • 相关阅读:
    「POJ 2699」The Maximum Number of Strong Kings
    「HNOI 2013」切糕
    「PKUSC 2018」真实排名
    「国家集训队 2009」最大收益
    「TJOI2015」线性代数
    「BZOJ 3280」小R的烦恼
    「SDOI 2017」新生舞会
    「六省联考 2017」寿司餐厅
    「TJOI 2013」循环格
    「TJOI 2013」攻击装置
  • 原文地址:https://www.cnblogs.com/qiezuimh/p/9712946.html
Copyright © 2011-2022 走看看