gpt4 book ai didi

java - 如何以BigInteger为指数进行指数计算?

转载 作者:行者123 更新时间:2023-11-30 06:45:19 24 4
gpt4 key购买 nike

我有一个 BigInteger 'X',想看看以下内容是否正确:3^(X/2)=(-1)(模X)

进行指数计算的方法(例如 Math.pow(3, X))不接受 BigInteger 作为 X 的值,那么我该如何进行上述计算?

最佳答案

使用BigDecimal.pow()

来自 tutorialspoint 的示例.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

public static void main(String[] args) {

// create 2 BigDecimal Objects
BigDecimal bg1, bg2;

MathContext mc = new MathContext(4); // 4 precision

bg1 = new BigDecimal("2.17");

// apply pow method on bg1 using mc
bg2 = bg1.pow(3, mc);

String str = "The value of " + bg1 + " to the power of 3, rounded to " + bg2;

// print bg2 value
System.out.println( str );
}
}

关于java - 如何以BigInteger为指数进行指数计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43780505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com