zoukankan      html  css  js  c++  java
  • JAVA BigInter

    使用long等基本数据类型,标识的数据范围是有限的,JAVA中使用BigInter模拟大整数运算

    BigInter在数学就是表示一个比较大的整数,在JAVA内部用一个int[]数组来模拟,没什么特别;

    使用上,和基本类型有一些要注意:

    BigInteger i1 = new BigInteger("1234567890");
    BigInteger i2 = new BigInteger("12345678901234567890");
    BigInteger sum = i1.add(i2); // 12345678902469135780
    

    long型整数运算比,BigInteger不会有范围限制,超出float表示的范围时,输出 Infinity

    四则运算需要使用实例化方法

    BigInteger i = new BigInteger("123456789000");
    System.out.println(i.longValue()); // 123456789000
    System.out.println(i.multiply(i).longValueExact()); // java.lang.ArithmeticException: BigInteger out of long range
    

    longValueExact 方法可以在运算超出范围时抛出异常;此外还有intValueExact

    转换为基本类型:

    • 转换为bytebyteValue()
    • 转换为shortshortValue()
    • 转换为intintValue()
    • 转换为longlongValue()
    • 转换为floatfloatValue()
    • 转换为doubledoubleValue()
  • 相关阅读:
    vim常用命令
    转:CRF++总结1
    转:CRF++总结2
    并查集算法程序
    CRF++使用小结(转)
    并查集算法程序
    C#winform 画图
    转:字符识别
    转:A Survey On Relation Extraction
    转:生产计划问题
  • 原文地址:https://www.cnblogs.com/debug-the-heart/p/13718824.html
Copyright © 2011-2022 走看看