KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > groupJep > groups > BigReals


1 /* @author rich
2  * Created on 05-Mar-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.groupJep.groups;
9 import java.math.*;
10
11 import org.lsmp.djep.groupJep.interfaces.*;
12
13 /**
14  * The field of Reals represented by BigDecimals.
15  * TODO Sorry power is not implemented.
16  * @see BigDecimal
17  *
18  * @author Rich Morris
19  * Created on 05-Mar-2004
20  */

21 public class BigReals extends Group implements FieldI,OrderedSetI {
22
23     private BigDecimal ZERO = BigDecimal.valueOf(0);
24     private BigDecimal ONE = BigDecimal.valueOf(1);
25     private int roundMode;
26     private int scale;
27     
28     /** private constructor as a scale must be specified. */
29     private BigReals() {}
30     /** Create a field of BigIntegers.
31      * The round mode and scale are used during the div
32      * method which calls {@link java.math.BigDecimal#divide(BigDecimal,int,int)}.
33      * if scale is negative then the
34      * {@link java.math.BigDecimal#divide(BigDecimal,int)}. is called instead.
35      */

36     public BigReals(int scale,int roundMode) {
37         this.roundMode = roundMode;
38         this.scale = scale;
39     }
40
41     /** Create a BigReals group with round mode set to
42      * BigDecimal.ROUND_HALF_DOWN.
43      *
44      * @param scale
45      */

46     public BigReals(int scale) {
47         this.roundMode = BigDecimal.ROUND_HALF_DOWN;
48         this.scale = scale;
49     }
50     public Number JavaDoc getZERO() {
51         return ZERO;
52     }
53
54     public Number JavaDoc getONE() {
55         return ONE;
56     }
57
58     public Number JavaDoc getInverse(Number JavaDoc num) {
59         BigDecimal a = (BigDecimal) num;
60         return a.negate();
61     }
62
63     public Number JavaDoc getMulInverse(Number JavaDoc num) {
64         return div(ONE,num);
65     }
66
67     public Number JavaDoc add(Number JavaDoc num1, Number JavaDoc num2) {
68         BigDecimal a = (BigDecimal) num1;
69         BigDecimal b = (BigDecimal) num2;
70         return a.add(b);
71     }
72
73     public Number JavaDoc sub(Number JavaDoc num1, Number JavaDoc num2) {
74         BigDecimal a = (BigDecimal) num1;
75         BigDecimal b = (BigDecimal) num2;
76         return a.subtract(b);
77     }
78
79     public Number JavaDoc mul(Number JavaDoc num1, Number JavaDoc num2) {
80         BigDecimal a = (BigDecimal) num1;
81         BigDecimal b = (BigDecimal) num2;
82         return a.multiply(b);
83     }
84
85     public Number JavaDoc div(Number JavaDoc num1, Number JavaDoc num2) {
86         BigDecimal a = (BigDecimal) num1;
87         BigDecimal b = (BigDecimal) num2;
88         if(scale>0)
89             return a.divide(b,scale,roundMode);
90         else
91             return a.divide(b,roundMode);
92     }
93
94     public boolean equals(Number JavaDoc a,Number JavaDoc b) {
95         return ((BigDecimal) a).compareTo((BigDecimal) b) == 0;
96     }
97     
98     public int compare(Number JavaDoc a,Number JavaDoc b) {
99         return ((BigDecimal) a).compareTo((BigDecimal) b);
100     }
101     
102     public Number JavaDoc valueOf(String JavaDoc str) {
103         return new BigDecimal(str);
104     }
105 }
106
Popular Tags