在Java中實(shí)現(xiàn)浮點(diǎn)數(shù)的精確計(jì)算
Posted on 2010-12-17 20:12 愛(ài)冷の心無(wú)悔 閱讀(257) 評(píng)論(0) 編輯 收藏 所屬分類: Java基礎(chǔ)
【問(wèn)題提出】
假如我們編譯運(yùn)行下面這個(gè)程序會(huì)看到什么?

2

3

4

5

6

7

8

你沒(méi)有看錯(cuò)!結(jié)果確實(shí)是
0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999
Java中的簡(jiǎn)單浮點(diǎn)數(shù)類型float和double不能夠進(jìn)行運(yùn)算。不光是Java,在其它很多編程語(yǔ)言中也有這樣的問(wèn)題。在大多數(shù)情況下,計(jì)算的結(jié)果是準(zhǔn)確的,但是多試幾次(可以做一個(gè)循環(huán))就可以試出類似上面的錯(cuò)誤?,F(xiàn)在終于理解為什么要有BCD碼了。
這個(gè)問(wèn)題相當(dāng)嚴(yán)重,假如你有9.999999999999元,你的計(jì)算機(jī)是不會(huì)認(rèn)為你可以購(gòu)買10元的商品的。
在有的編程語(yǔ)言中提供了專門的貨幣類型來(lái)處理這種情況,但是Java沒(méi)有?,F(xiàn)在讓我們看看如何解決這個(gè)問(wèn)題。
四舍五入
我們的第一個(gè)反應(yīng)是做四舍五入。Math類中的round方法不能設(shè)置保留幾位小數(shù),我們只能象這樣(保留兩位):

2

3

非常不幸,上面的代碼并不能正常工作,給這個(gè)方法傳入4.015它將返回4.01而不是4.02,如我們?cè)谏厦婵吹降?/p>
4.015*100=401.49999999999994
因此假如我們要做到精確的四舍五入,不能利用簡(jiǎn)單類型做任何運(yùn)算
java.text.DecimalFormat也不能解決這個(gè)問(wèn)題:
System.out.println(new java.text.DecimalFormat("0.00").format(4.025));
輸出是4.02
BigDecimal
在《Effective Java》這本書中也提到這個(gè)原則,float和double只能用來(lái)做科學(xué)計(jì)算或者是工程計(jì)算,在商業(yè)計(jì)算中我們要用java.math.BigDecimal。BigDecimal一共有4個(gè)夠造方法,我們不關(guān)心用BigInteger來(lái)夠造的那兩個(gè),那么還有兩個(gè),它們是:
BigDecimal(double val)
Translates a double into a BigDecimal.
BigDecimal(String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
上面的API簡(jiǎn)要描述相當(dāng)?shù)拿鞔_,而且通常情況下,上面的那一個(gè)使用起來(lái)要方便一些。我們可能想都不想就用上了,會(huì)有什么問(wèn)題呢?等到出了問(wèn)題的時(shí)候,才發(fā)現(xiàn)上面哪個(gè)夠造方法的具體說(shuō)明中有這么一段:
Note: the results of this constrUCtor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would eXPect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
原來(lái)我們假如需要精確計(jì)算,非要用String來(lái)夠造BigDecimal不可!在《Effective Java》一書中的例子是用String來(lái)夠造BigDecimal的,但是書上卻沒(méi)有強(qiáng)調(diào)這一點(diǎn),這也許是一個(gè)小小的失誤吧。
【解決方案】
現(xiàn)在我們已經(jīng)可以解決這個(gè)問(wèn)題了,原則是使用BigDecimal并且一定要用String來(lái)夠造。
但是想像一下吧,假如我們要做一個(gè)加法運(yùn)算,需要先將兩個(gè)浮點(diǎn)數(shù)轉(zhuǎn)為String,然后夠造成BigDecimal,在其中一個(gè)上調(diào)用add方法,傳入另一個(gè)作為參數(shù),然后把運(yùn)算的結(jié)果(BigDecimal)再轉(zhuǎn)換為浮點(diǎn)數(shù)。你能夠忍受這么煩瑣的過(guò)程嗎?下面我們提供一個(gè)工具類Arith來(lái)簡(jiǎn)化操作。它提供以下靜態(tài)方法,包括加減乘除和四舍五入:











附錄
源文件Arithmetic.java:

2

3

4

5

6

7

8

9

10

11

12

13

14

15


17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113
