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
        
    }
}

 

转载于:https://www.cnblogs.com/qiezuimh/p/9712946.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值