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
            
        }
    }
  • 相关阅读:
    [HNOI2002]营业额统计
    HDU 1374
    HDU 3345
    HDU 2089
    Graham扫描法
    Codeforces 1144D Deduction Queries 并查集
    Codeforces 916E Jamie and Tree 线段树
    Codeforces 1167F Scalar Queries 树状数组
    Codeforces 1167E Range Deleting
    Codeforces 749E Inversions After Shuffle 树状数组 + 数学期望
  • 原文地址:https://www.cnblogs.com/qiezuimh/p/9712946.html
Copyright © 2011-2022 走看看