回答編集履歴
2
追記
test
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
つ [浮動小数点数(float, double)演算の丸め誤差と対策](http://tech-blog.tsukaby.com/archives/740)
|
2
2
|
|
3
3
|
[追記]
|
4
|
+
|
5
|
+
なぜという質問の答えではないですが^^;
|
4
6
|
|
5
7
|
```java
|
6
8
|
|
@@ -21,8 +23,6 @@
|
|
21
23
|
/**
|
22
24
|
|
23
25
|
*
|
24
|
-
|
25
|
-
* @author kondo
|
26
26
|
|
27
27
|
*/
|
28
28
|
|
1
追記
test
CHANGED
@@ -1 +1,67 @@
|
|
1
1
|
つ [浮動小数点数(float, double)演算の丸め誤差と対策](http://tech-blog.tsukaby.com/archives/740)
|
2
|
+
|
3
|
+
[追記]
|
4
|
+
|
5
|
+
```java
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
package javaapplication1;
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
import java.math.BigDecimal;
|
14
|
+
|
15
|
+
import java.math.RoundingMode;
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
/**
|
22
|
+
|
23
|
+
*
|
24
|
+
|
25
|
+
* @author kondo
|
26
|
+
|
27
|
+
*/
|
28
|
+
|
29
|
+
public class JavaApplication1 {
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
/**
|
34
|
+
|
35
|
+
* @param args the command line arguments
|
36
|
+
|
37
|
+
*/
|
38
|
+
|
39
|
+
public static void main(String[] args) {
|
40
|
+
|
41
|
+
// TODO code application logic here
|
42
|
+
|
43
|
+
double d = 10/3.0;
|
44
|
+
|
45
|
+
System.out.println( d + " = " + Long.toBinaryString(Double.doubleToRawLongBits(d)));
|
46
|
+
|
47
|
+
//
|
48
|
+
|
49
|
+
BigDecimal b1 = new BigDecimal("10.0");
|
50
|
+
|
51
|
+
BigDecimal b2 = new BigDecimal("3.0");
|
52
|
+
|
53
|
+
BigDecimal a = b1.divide(b2,20,RoundingMode.HALF_UP);
|
54
|
+
|
55
|
+
System.out.println( a );
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
結果:
|
64
|
+
|
65
|
+
3.3333333333333335 = 100000000001010101010101010101010101010101010101010101010101011
|
66
|
+
|
67
|
+
3.33333333333333333333
|