java中Math类的常用方法总结

/**
Math类中常用方法
*
/
public class M {
public static void main(String[] args) {
/

  • Math的静态常量:
    Math.PI:表示圆周率
    Math.E:自然对数
    /
    System.out.println(Math.PI);
    //输出3.141592653589793
    System.out.println(Math.E);
    //输出2.718281828459045(自然对数)
    /
    *

  • Math.abs求绝对值 数值为double、int、float、long类型都可以
    /
    System.out.println(Math.abs(-110));//输出110
    System.out.println(Math.abs(110.110));//输出110.11
    /
    *

  • Math.ceil返回x轴靠右的整数值。 例:11.1返回12.0 数值为double类型
    /
    System.out.println(Math.ceil(11.1));//输出12.0
    System.out.println(Math.ceil(-11.1));//输出-11.0
    /
    *

  • Math.floor返回x轴靠左的整数值。 例:11.1返回11.0 数值为double类型
    */

     System.out.println(Math.floor(11.1));//输出11.0
     System.out.println(Math.floor(-11.1));//输出-12.0
    

/**

  • Math.max Math.min分别表示在两者之间选最大值和最小值

  • 例:max(110,119)输出119 min(120,110)输出110

  • 数值为double、int、float、long类型都可
    /
    System.out.println(Math.max(110,119 ));//输出119
    System.out.println(Math.min(110,119 ));//输出110
    /
    *

  • Math.pow 求幂运算 例:Math.pow(2,3) 输出8(2为底数,3为指数)

  • 其中Math.sqrt为开平方,Math.cbrt为开立方

  • 只能以double类型
    /
    System.out.println(Math.pow(2,3));//输出8.0
    System.out.println(Math.sqrt(8));//输出2.8284271247461903
    System.out.println(Math.cbrt(8));//输出2.0
    /
    *

  • Math.round四舍五入运算 例: 4.5输出5

  • 类型为double、float类型
    /
    System.out.println(Math.round(4.5));//输出5
    /
    *

  • Math.random显示0到1的随机数
    */
    Random r=new Random();
    int a=r.nextInt(100)+1;//产生1-100的随机数
    System.out.println(a);

    }

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java的复数可以用来进行复数运算和处理。以下是Java复数总结: 1. Java自带的复数Java自带了Complex,可以通过导入java.util.Complex包来使用。 2. 复数的定义:复数通常由实部和虚部组成,可以定义一个复数来表示这两个部分。例如: ```java public class Complex { private double real; private double imag; // 构造方法 public Complex(double real, double imag) { this.real = real; this.imag = imag; } // getter和setter方法 public double getReal() { return real; } public void setReal(double real) { this.real = real; } public double getImag() { return imag; } public void setImag(double imag) { this.imag = imag; } } ``` 3. 复数的运算:复数可以进行加、减、乘、除等运算,例如: ```java public Complex add(Complex other) { double real = this.real + other.real; double imag = this.imag + other.imag; return new Complex(real, imag); } public Complex subtract(Complex other) { double real = this.real - other.real; double imag = this.imag - other.imag; return new Complex(real, imag); } public Complex multiply(Complex other) { double real = this.real * other.real - this.imag * other.imag; double imag = this.real * other.imag + this.imag * other.real; return new Complex(real, imag); } public Complex divide(Complex other) { double real = (this.real * other.real + this.imag * other.imag) / (Math.pow(other.real, 2) + Math.pow(other.imag, 2)); double imag = (this.imag * other.real - this.real * other.imag) / (Math.pow(other.real, 2) + Math.pow(other.imag, 2)); return new Complex(real, imag); } ``` 4. 复数常用方法:复数还可以实现一些常用方法,例如获取模长和相角,例如: ```java public double getModulus() { return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2)); } public double getArgument() { return Math.atan2(this.imag, this.real); } ``` 5. 复数的使用:定义好复数后,就可以通过实例化对象来进行复数运算,例如: ```java Complex c1 = new Complex(2, 3); Complex c2 = new Complex(4, -2); Complex sum = c1.add(c2); Complex difference = c1.subtract(c2); Complex product = c1.multiply(c2); Complex quotient = c1.divide(c2); double modulus = c1.getModulus(); double argument = c1.getArgument(); ``` 以上就是Java复数总结

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

崽崽不会编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值