corejava11(3.9 大数)

3.9 大数

如果基本整数和浮点类型的精度不够,可以在java.math包中找到几个方便的类:BigIntegerBigDecimal。这些类用于处理具有任意长数字序列的数字。BigInteger类实现任意精度整数算术,BigDecimal对浮点数也执行相同的操作。

使用静态的valueOf方法将普通数字转换为大数:

BigInteger a = BigInteger.valueOf(100);

对于较长的数字,请使用带有字符串参数的构造函数:

BigInteger reallyBig = new BigInteger("22223224462942044552973989346190996720666693909")

也有常数BigInteger.ZEROBigInteger.ONEBigInteger.TEN,并且,从Java 9开始,还有BigInteger.TWO

不幸的是,您不能使用熟悉的数学运算符(如+和*)组合大数字。相反,您必须在大数类中使用诸如加法和乘法之类的方法。

BigInteger c = a.add(b); // c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * d

C++注意

与C++不同,Java没有可编程操作符重载。BigInteger类的编程人员无法重新定义+和*运算符,以给出BigInteger类的加法和乘法运算。语言设计者确实重载了+运算符来表示字符串的串联。他们选择不重载其他操作员,也没有给Java程序员机会在自己的类中重载操作符。

清单3.6显示了对清单3.5中彩票赔率程序的修改,并对其进行了更新,以便与大数字一起使用。例如,如果你被邀请参加抽奖,你需要从490个数字中选出60个数字,你可以使用这个程序告诉你你的获胜几率。它是1/7163958434619955574151622254009293341717612789263493493351,祝你好运!

清单3.5中的程序计算了语句

lotteryOdds = lotteryOdds * (n - i + 1) / i;

当使用大数字时,等价语句变为

lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(BigInteger.valueOf(i));

清单3.6 BigIntegerTest/BigIntegerTest.java

import java.math.*;
import java.util.*;

/**
 * This program uses big numbers to compute the odds of winning the grand prize in a lottery.
 * @version 1.20 2004-02-10
 * @author Cay Horstmann
 */
public class BigIntegerTest
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      System.out.print("How many numbers do you need to draw? ");
      int k = in.nextInt();

      System.out.print("What is the highest number you can draw? ");
      int n = in.nextInt();

      /*
       * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
       */

      BigInteger lotteryOdds = BigInteger.valueOf(1);

      for (int i = 1; i <= k; i++)
         lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(
            BigInteger.valueOf(i));

      System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
   }
}

java.math.BigInteger 1.1

  • BigInteger add(BigInteger other)
  • BigInteger subtract(BigInteger other)
  • BigInteger multiply(BigInteger other)
  • BigInteger divide(BigInteger other)
  • BigInteger mod(BigInteger other)
    返回此大整数和其他整数的和、差、积、商和余数。
  • BigInteger sqrt() 9
    生成此大整数的平方根。
  • int compareTo(BigInteger other)
    如果此大整数等于其他整数,则返回0;如果此大整数小于其他整数,则返回负结果;否则返回正结果。
  • static BigInteger valueOf(long x)
    返回值等于x的大整数。

java.math.BigDecimal 1.1

  • BigDecimal add(BigDecimal other)
  • BigDecimal subtract(BigDecimal other)
  • BigDecimal multiply(BigDecimal other)
  • BigDecimal divide(BigDecimal other)
  • BigDecimal divide(BigDecimal other, RoundingMode mode) 5
    返回此大十进制数和其他数的和、差、积或商。如果商没有有限的十进制展开,则第一个除法将引发异常。要获得四舍五入的结果,请使用第二种方法。模式RoundingMode.HALF_UP是您在学校学习的舍入模式:将数字0到4舍弃,将数字5到9进位。它适用于常规计算。有关其他舍入模式,请参阅API文档。
  • int compareTo(BigDecimal other)
    如果此大小数等于Other,则返回0;如果此大小数小于Other,则返回负结果;否则返回正结果。
  • static BigDecimal valueOf(long x)
  • static BigDecimal valueOf(long x, int scale)
    返回一个大小数,其值等于x或x/(10^scale)。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Core Java Volume I–Fundamentals, 1 (11th Edition) By 作者: Cay S. Horstmann ISBN-10 书号: 0135166306 ISBN-13 书号: 9780135166307 Edition 版本: 11 出版日期: 2018-09-06 pages 页数: 928 The #1 Java Guide for Serious Programmers: Fully Updated for Java SE 9, 10 & 11 For serious programmers, Core Java, Volume I—Fundamentals, Eleventh Edition, is the definitive guide to writing robust, maintainable code. Whether you’re using Java SE 9, 10, or 11, it will help you achieve a deep and practical understanding of the language and API, and its hundreds of realistic examples reveal the most powerful and effective ways to get the job done. Cay Horstmann’s updated examples reflect Java’s long-awaited modularization, showing how to write code that’s easier to manage and evolve. You’ll learn how to use JShell’s new Read-Eval-Print Loop (REPL) for more rapid and exploratory development, and apply key improvements to the Process API, contended locking, logging, and compilation. In this first of two volumes, Horstmann offers in-depth coverage of fundamental Java and UI programming, including objects, generics, collections, lambda expressions, Swing design, concurrency, and functional programming. If you’re an experienced programmer moving to Java SE 9, 10, or 11, there’s no better source for expert insight, solutions, and code. Master foundational techniques, idioms, and best practices for writing superior Java code Leverage the power of interfaces, lambda expressions, and inner classes Harden programs through effective exception handling and debugging Write safer, more reusable code with generic programming Improve performance and efficiency with Java’s standard collections Build cross-platform GUIs with the Swing toolkit Fully utilize multicore processors with Java’s improved concurrency
Java核心技术卷1:基础知识——适用于急于实践的读者》是由Cay S. Horstmann和Gary Cornell合著的一本畅销书籍。这本书主要面向那些希望快速入门Java编程语言的读者。 《Java核心技术卷1:基础知识——适用于急于实践的读者》首先介绍了Java的基础知识,包括变量、数据类型、运算符、控制流、数组等,这些核心概念对于理解和使用Java编程非常重要。 接着,书中详细介绍了面向对象编程(OOP)的概念和原则,包括类、对象、继承、封装、多态等。通过理解OOP的基本原则,读者能够充分利用Java的面向对象特性,编写结构清晰、可扩展和易于维护的代码。 此外,本书还对异常处理、输入输出、集合框架、泛型、并发编程等Java的高级特性进行了详细讲解。这些内容使读者能够进一步提升在Java编程领域的能力,并能够编写出更加复杂和高效的程序。 与其他Java教材相比,此书的特点是紧凑而实用,适用于那些渴望快速掌握Java编程语言的读者。每个概念和编程技巧都通过简单而丰富的示例进行了演示,读者能够通过阅读和实践快速理解并掌握这些知识。 总之,《Java核心技术卷1:基础知识——适用于急于实践的读者》是一本权威且实用的Java入门教材,适用于那些希望快速入门和实践Java编程的读者。通过阅读本书,读者能够建立扎实的Java基础,并能够应用这些知识进行实际项目的开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值