zoukankan      html  css  js  c++  java
  • 常见对象BigInteger详解

    在用C或者C++处理大数时感觉非常麻烦,但是在Java中有两个类BigInteger和BigDecimal分别表示大整数类和
    大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。 这两个类都在java.math.
    *包中,因此每次必须在开头处引用该包。 Ⅰ基本函数: 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigInteger b=BigInteger.valueOf(a); 则b=3; String s=”12345”; BigInteger c=BigInteger.valueOf(s); 则c=123452.add(); 大整数相加 BigInteger a=new BigInteger(“23”); BigInteger b=new BigInteger(“34”); a. add(b); 3.subtract(); 相减 4.multiply(); 相乘 5.divide(); 相除取整 6.remainder(); 取余 7.pow(); a.pow(b)=a^b 8.gcd(); 最大公约数 9.abs(); 绝对值 10.negate(); 取反数 11.mod(); a.mod(b)=a%b=a.remainder(b); 12.max(); min(); 13.punlic int comareTo(); 14.boolean equals(); 是否相等 15.BigInteger构造函数: 一般用到以下两种: BigInteger(String val); 将指定字符串转换为十进制表示形式; BigInteger(String val,int radix); 将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger if( a.compareTo(b) == 0 ) System.out.println("a == b"); //大整数a==b else if( a.compareTo(b) > 0 ) System.out.println("a > b"); //大整数a>b else if( a.compareTo(b) < 0 ) System.out.println("a < b"); //大整数a<b //大整数绝对值 System.out.println(a.abs()); //大整数a的绝对值 //大整数的幂 int exponent=10; System.out.println(a.pow(exponent)); //大整数a的exponent次幂 //返回大整数十进制的字符串表示 System.out.println(a.toString()); //返回大整数p进制的字符串表示 int p=8; System.out.println(a.toString(p)); ------------------------------------------------------------------------ package com.csf.practicetest; import java.math.BigInteger; import java.util.Arrays; /** * Created by fenglei.ma on 2018/4/3. 17:51 */ public class BigIntegerDemo { public static void main(String[] args) { BigInteger bi1 = new BigInteger("999"); BigInteger bi2 = new BigInteger("50"); //public BigInteger add(BigInteger val):加 System.out.println("add:" + bi1.add(bi2)); //public BigInteger subtract(BigInteger val):减 System.out.println("subtract:" + bi1.subtract(bi2)); //public BigInteger multiply(BigInteger val):乘 System.out.println("multiply:" + bi1.multiply(bi2)); //public BigInteger divide(BigInteger val):除 System.out.println("divide:" + bi1.divide(bi2)); //public BigInteger[] divideAndRemainder(BingInteger val):返回商和余数的数组 BigInteger[] bis = bi1.divideAndRemainder(bi2); System.out.println("divideAndRemainder:" + Arrays.toString(bis)); System.out.println("商:" + bis[0]); System.out.println("余数:" + bis[1]); } }
  • 相关阅读:
    Html禁止粘贴 复制 剪切
    表单标签
    自构BeanHandler(用BeansUtils)
    spring配置中引入properties
    How Subcontracting Cockpit ME2ON creates SD delivery?
    cascadia code一款很好看的微软字体
    How condition value calculated in sap
    Code in SAP query
    SO Pricing not updated for partial billing items
    Javascript learning
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/8710180.html
Copyright © 2011-2022 走看看