如何将运算符用于BigInteger [英] How to use operators for BigInteger

查看:171
本文介绍了如何将运算符用于BigInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.lang.Math;
import java.math.BigInteger;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        int e1 = 20, d = 13;
        BigInteger C = BigDecimal.valueOf(e1).toBigInteger();

        BigInteger po = C.pow(d);
        System.out.println("pow is:" + po);

        int num = 11;
        BigInteger x = po;
        BigInteger n = BigDecimal.valueOf(num).toBigInteger();
        BigInteger p, q, m;

        System.out.println("x: " + x);

        q=(x / n);
        p=(q * n);
        m=(x - p);
        System.out.println("mod is:" + m);
    }
}

我尝试寻找一些与此相关的答案,但无法解决.请有人能告诉我这是怎么回事.我将数据类型更改为整数,但是幂函数不起作用.

I've tried looking for some answers related to it but unable to solve. Please can someone tell me what's wrong in this. I changed the datatype to integer but then the power function doesn't works.

这是我得到的错误:

error: bad operand types for binary operator '/'
    q=(x/n);
        ^
  first type:  BigInteger
  second type: BigInteger
Main.java:33: error: bad operand types for binary operator '*'
    p=(q*n);
        ^
  first type:  BigInteger
  second type: BigInteger
Main.java:34: error: bad operand types for binary operator '-'
    m=(x-p);
        ^
  first type:  BigInteger
  second type: BigInteger
3 errors

    .

推荐答案

说明

您不能在BigInteger上使用运算符.它们不是像int这样的基元,而是类. Java没有运算符重载.

Explanation

You can not use the operators on BigInteger. They are not primitives like int, they are classes. Java has no operator overloading.

看看

Take a look at the class documentation and use the corresponding methods:

BigInteger first = BigInteger.ONE;
BigInteger second = BigInteger.TEN;

BigInteger addResult = first.add(second);
BigInteger subResult = first.subtract(second);
BigInteger multResult = first.multiply(second);
BigInteger divResult = first.divide(second);


操作员详细信息

您可以在 Java语言规范(JLS).


Operator Details

You can look up the detailed definitions for the operators and when you can use them in the Java Language Specification (JLS).

以下是相关部分的一些链接:

Here are some links to the relevant sections:

  • Multiplication * §15.17.1
  • Division / §15.17.2
  • String concatenation + §15.18.1
  • Addition and subtraction + - §15.18.2

其中大多数使用数值类型的概念进行操作

Most of them work with the notion of Numeric Type §4, which consists of Integral Type and FloatingPointType:

整数类型为byteshortintlong,其值分别为8位,16位,32位和64位带符号的二进制补码整数,和char,其值为表示UTF-16代码单元的16位无符号整数(

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).

浮点类型为float(其值包括32位IEEE 754浮点数)和double(其值包括64位IEEE 754浮点数).

The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers.

此外,如果需要,Java可以将诸如Integer之类的包装器类包装到int中,反之亦然.这就添加了取消装箱的转换§ 5.1.8 到支持的操作数集.

Additionally, Java can unbox wrapper classes like Integer into int and vice versa, if needed. That adds the unboxing conversions §5.1.8 to the set of supported operands.

您创建的BigInteger不必要地冗长和复杂:

Your creation of BigInteger is unnecessarily long and complicated:

// Yours
BigInteger C = BigDecimal.valueOf(e1).toBigInteger();

// Prefer this instead
BigInteger c = BigInteger.valueOf(e1);

如果可能的话,您应该更喜欢从StringBigInteger,从BigIntegerString.由于BigInteger的目的是将其用于太大而无法用基元表示的数字:

And if possible, you should prefer to go from String to BigInteger and from BigInteger to String. Since the purpose of BigInteger is to use it for numbers that are too big to be represented with the primitives:

// String -> BigInteger
String numberText = "10000000000000000000000000000000";
BigInteger number = new BigInteger(numberText);

// BigInteger -> String
BigInteger number = ...
String numberText = number.toString();

此外,请遵守Java命名约定.变量名应该是camelCase,所以c而不是C.

Also, please stick to Java naming conventions. Variable names should be camelCase, so c and not C.

此外,更喜欢使用有意义的变量名.像cd这样的名称不能帮助任何人理解该变量应该表示什么.

Additionally, prefer to have meaningful variable names. A name like c or d does not help anyone to understand what the variable is supposed to represent.

这篇关于如何将运算符用于BigInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆