java 指数计算

计算浮点数的整数次幂
部署运行你感兴趣的模型镜像
    /**
     * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
     */
    public static void testGetExponentOfNum() {

        double base = Math.random();
        int exponent = new Random().nextInt(5);
        System.out.println("testGetExponentOfNum--base=" + base+"--exponent="+exponent);
        if (exponent == 0) {
            System.out.println("testGetExponentOfNum--0--result=" + 1);
        } else if (exponent == 1) {
            System.out.println("testGetExponentOfNum--1--result=" + base);
        } else if (exponent > 1) {
            double num = base;
            for (int i = 2; i <= exponent; i++) {
                num = num * base;
            }
            System.out.println("testGetExponentOfNum-->1--result=" + num);
        } else {
            System.out.println("testGetExponentOfNum error");
        }
        System.out.println("testGetExponentOfNum use Api result="+Math.pow(base,exponent));
    }

写完以后突然发现上面的代码没有考虑base 与 exponent可能为负数的情况。

    /**
     * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
     */
    public static void testGetExponentOfNum01() {
        double base = Math.random() * (new Random().nextInt(1) > 0 ? 1 : -1);
        int exponent = new Random().nextInt(5) * (new Random().nextInt(1) > 0 ? 1 : -1);
        double result = 1;
        System.out.println("testGetExponentOfNum--base=" + base + "--exponent=" + exponent);
        if(base == 0){
            System.out.println("testGetExponentOfNum--base == 0");
            return;
        }
        if (exponent == 0) {
            System.out.println("testGetExponentOfNum--[=0]--result=" + 1);
            return;
        } else if (exponent > 0) {
            result = base;
            for (int i = 2; i <= exponent; i++) {
                result = result * base;
            }
            System.out.println("testGetExponentOfNum--[>0]--result=" + result);
        } else {
            result = base;
            for (int i = 2; i <= Math.abs(exponent); i++) {
                result = result * base;
            }
            result = 1 / result;
            System.out.println("testGetExponentOfNum--[<0]--result=" + result);
        }

        System.out.println("testGetExponentOfNum use Api result=" + Math.pow(base, exponent));
    }

RESULT

12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.7797871217121436--exponent=-3
12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[<0]--result=-2.1089769082499257
12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=-2.1089769082499257
12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.7498661439578588--exponent=-3
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[<0]--result=-2.3716399771135768
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=-2.3716399771135768
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.23920783483769525--exponent=-2
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[<0]--result=17.476288271069706
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=17.476288271069706
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.12243023418032972--exponent=0
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[=0]--result=1
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=1.0
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========

====如果有错误,请指正====

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

Java 中进行指数计算,通常可以使用 `Math` 类的 `pow` 方法。`Math.pow(double a, double b)` 方法用于计算 `a` 的 `b` 次幂,返回为 `double` 类型。 以下是一个简单的示例代码: ```java public class ExponentCalculation { public static void main(String[] args) { double base = 2; double exponent = 3; double result = Math.pow(base, exponent); System.out.println(base + " 的 " + exponent + " 次幂是: " + result); } } ``` 在上述代码中,`Math.pow(2, 3)` 计算 2 的 3 次幂,结果存储在 `result` 变量中,并将结果输出。 此外,在之前提供的 BMI 指数计算小工具代码中,虽然没有直接使用 `Math.pow` 方法进行指数计算,但使用了 `h * h` 来计算身高的平方,这也是一种简单的指数计算计算 2 次幂): ```java public static void main(String[] args) { System.out.println("请输入您的身高,单位(米)"); java.util.Scanner sc = new java.util.Scanner(System.in); Double h = sc.nextDouble(); System.out.println("请输入您的体重,单位(公斤)"); Double w = sc.nextDouble(); Double bmi = w / (h * h); System.out.println("您的身高:" + h + "米,\n体重:" + w + "公斤,\n您目前的 BMI:" + String.format("%.2f", bmi) + " 属于" + (bmi > 23.9 ? "偏胖" : bmi < 18.5 ? "偏瘦" : "正常")); System.out.println("标准体重为:" + String.format("%.2f", 18.5 * (h * h)) + "~" + String.format("%.2f", 23.9 * (h * h))); } ``` 这里 `h * h` 等同于 `Math.pow(h, 2)`。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值