在Java编程中如果打出指数_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==========

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值