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
            
        }
    }
  • 相关阅读:
    合并n个链表
    合并2个链表
    删除链表中的倒数第n个元素
    判断字符串是否回文字符串
    回文链表
    反转链表
    反转链表2
    冒泡排序法
    编译datax(3.x版本)踩的坑,记录修改记录
    Python基本知识(6)——集合
  • 原文地址:https://www.cnblogs.com/qiezuimh/p/9712946.html
Copyright © 2011-2022 走看看