当前位置:首页 » 《资源分享》 » 正文

Java Math函数简介_GooReey的博客

18 人参与  2021年09月26日 10:43  分类 : 《资源分享》  评论

点击全文阅读


今天小编简单介绍一下Math函数的使用,主要是对数字的各种运算操作,话不多说,直接上代码。

package com.guor.test;

import java.util.Random;
import java.util.stream.IntStream;

public class MathTest {

    private static void test01() {
        System.out.println("计算平方根:"+Math.sqrt(16));//4.0
        System.out.println("计算立方根:"+Math.cbrt(27));//3.0
        System.out.println("计算立方根:"+Math.cbrt(21));//2.7589241763811208
        System.out.println("计算a的b次方:"+Math.pow(2, 3));//8.0
        System.out.println("计算最大值:"+Math.max(26, 19));//26
        System.out.println("计算最小值:"+Math.min(26, 19));//19
        System.out.println("取绝对值:"+Math.abs(23.3));//23.3
        System.out.println("取绝对值:"+Math.abs(-23.3));//23.3
    }
    
    private static void test02() {
        System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));  //1.0
        System.out.println("0度的余弦值:" + Math.cos(0));  //1.0
        
        System.out.println("反余弦值:"+Math.acos(0.1));//1.4706289056333368
        System.out.println("反余弦值:"+Math.acos(0.5));//1.0471975511965979
        System.out.println("反余弦值:"+Math.acos(0.9));//0.45102681179626236
        System.out.println("反余弦值:"+Math.acos(1));//0.0
        System.out.println("反余弦值:"+Math.acos(1.9));//NaN,大于1,输出 NaN
        
        System.out.println("60度的正切值:" + Math.tan(Math.PI/3));  //1.7320508075688767
        System.out.println("1的反正切值: " + Math.atan(1));  //0.7853981633974483
        System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));  //90.0
        System.out.println(Math.PI); //3.141592653589793
        
        //将角度转换为弧度。
        double x = 45.0;
        double y = 30.0;
        System.out.println("将角度转换为弧度:"+Math.toRadians(x) );//0.7853981633974483
        System.out.println("将角度转换为弧度:"+Math.toRadians(y) );//0.5235987755982988
    }
    
    private static void test03() {
        System.out.println("逢余进一:"+Math.ceil(2.66));//3.0
        System.out.println(Math.ceil(10));
        System.out.println("逢余舍一:"+Math.floor(2.66));//2.0
        System.out.println("逢余舍一:"+Math.floor(2.169));//2.0
        
        //返回小于或等于代数商的最大(最接近正无穷大)int值
        System.out.println(Math.floorDiv(10, 15));//0
        System.out.println(Math.floorDiv(15, 10));//1
        System.out.println(Math.floorDiv(25, 10));//2
        
        //四舍五入,返回double值。注意.5的时候会取偶数
        System.out.println(Math.rint(2.6));//3.0
        System.out.println(Math.rint(2.5));//不是3.0,而是2.0
        System.out.println(Math.rint(2.4));//2.0
        System.out.println(Math.rint(3.5));//4.0
        
        //四舍五入,float时返回int值,double时返回long值
        long round2 = Math.round(2.6);
        int round3 = Math.round(3);
        long round4 = Math.round(2.0);
        System.out.println(Math.round(2.6));//3
        long round = Math.round(2.6);
        System.out.println(Math.round(2.6));//3
    }
    
    private static void test04() {
        // 两数相加,并判断是否溢出
        System.out.println(Math.addExact(1, 2));
        System.out.println(Math.addExact(100, 222));
        System.out.println(Math.addExact(Integer.MAX_VALUE, 0));//2147483647
        //System.out.println(Math.addExact(Integer.MAX_VALUE, 1));//java.lang.ArithmeticException: integer overflow
        
        //从指定的变量中减去1并返回它。
        System.out.println(Math.decrementExact(10));//9
        System.out.println(Math.decrementExact(1088));//1087
        
        System.out.println(Math.multiplyExact(10, 2));//20
        
        //exp() 方法用于返回自然数底数e的参数次方
        System.out.println(Math.exp(2.0));
        System.out.println(Math.exp(2.5));
        System.out.println(Math.exp(2.7));
        System.out.println(Math.exp(-22.7));
    }
    
    private static void test05() {
        //复制第二个参数的符号并将其分配给第一个参数
        System.out.println(Math.copySign(10.2, 2.5));//10.2
        System.out.println(Math.copySign(10.2, -2.5));//-10.2
        System.out.println(Math.copySign(-10.2, 0));//10.2,零的符号是正数?也对
        
        //第二个参数的方向上返回与第一个参数相邻的数字。
        System.out.println(Math.nextAfter(10.2, 2.6));//10.199999999999998
        System.out.println(Math.nextAfter(2.6, 10.2));//2.6000000000000005
        System.out.println(Math.nextAfter(10.2, 10.2));//10.2
        
        System.out.println(Math.nextDown(10.5));//10.499999999999998
        System.out.println(Math.nextUp(10.5));//10.500000000000002
        
        //用于返回参数的符号
        System.out.println(Math.signum(10));//1.0
        System.out.println(Math.signum(0));//0.0
        System.out.println(Math.signum(-10));//-1.0
        
        // 返回整数,如果结果溢出,则去掉符号相加或相减
        System.out.println(Math.toIntExact(499)); //499
        System.out.println(Math.toIntExact(Integer.MAX_VALUE)); //2147483647
        System.out.println(Math.toIntExact(Integer.MAX_VALUE+1)); //-2147483648
        System.out.println(Math.toIntExact(Integer.MAX_VALUE+10)); //-2147483639
        
        System.out.println(Math.toIntExact(Integer.MIN_VALUE));//-2147483648
        System.out.println(Math.toIntExact(Integer.MIN_VALUE+10));//-2147483638
        System.out.println(Math.toIntExact(Integer.MIN_VALUE-10));//2147483638
    }
    
    private static void test06() {
        System.out.println("随机数:" + Math.random());// 0.30900647153462657
        Random random = new Random();
        System.out.println(random.nextInt());// -133596410
        System.out.println(random.nextInt(100));// 54,,,100是边界
        
        //返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布的 double值。
        System.out.println(random.nextDouble());//0.14627990739876262
        
        //生成随机字节并将其置于用户提供的 byte 数组中。
        byte[] bytes = new byte[10];
        random.nextBytes(bytes);
    }
    
    public static void main(String[] args) {
        test06();
    }
}

🔥联系作者,或者扫作者主页二维码加群,加入我们吧🔥

往期精彩内容

Java学习路线总结❤️搬砖工逆袭Java架构师❤️(全网最强,建议收藏)

❤️连续面试失败后,我总结了57道面试真题❤️,如果时光可以倒流...(附答案,建议收藏)

10万字208道Java经典面试题总结(附答案,建议收藏)

MySql基础知识总结(2021版)

MySql基础知识总结(SQL优化篇)

【Vue基础知识总结 1】Vue入门

【100天算法入门 - 每日三题 - Day1】二叉树的中序遍历、两数之和、整数反转


点击全文阅读


本文链接:http://zhangshiyu.com/post/28688.html

余弦  返回  参数  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1