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()
  • 相关阅读:
    SPOJ 149 FSHEEP Fencing in the Sheep ( 计算几何 + 二分 )
    UVa 11806
    UVa 1445
    HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )
    HDU 4661 Message Passing ( 树DP + 推公式 )
    从远程库克隆库
    添加远程库
    远程仓库
    删除文件
    撤销修改
  • 原文地址:https://www.cnblogs.com/debug-the-heart/p/13718824.html
Copyright © 2011-2022 走看看