1 8 package org.lsmp.djep.groupJep.groups; 9 import java.math.*; 10 11 import org.lsmp.djep.groupJep.interfaces.*; 12 13 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 29 private BigReals() {} 30 36 public BigReals(int scale,int roundMode) { 37 this.roundMode = roundMode; 38 this.scale = scale; 39 } 40 41 46 public BigReals(int scale) { 47 this.roundMode = BigDecimal.ROUND_HALF_DOWN; 48 this.scale = scale; 49 } 50 public Number getZERO() { 51 return ZERO; 52 } 53 54 public Number getONE() { 55 return ONE; 56 } 57 58 public Number getInverse(Number num) { 59 BigDecimal a = (BigDecimal) num; 60 return a.negate(); 61 } 62 63 public Number getMulInverse(Number num) { 64 return div(ONE,num); 65 } 66 67 public Number add(Number num1, Number num2) { 68 BigDecimal a = (BigDecimal) num1; 69 BigDecimal b = (BigDecimal) num2; 70 return a.add(b); 71 } 72 73 public Number sub(Number num1, Number num2) { 74 BigDecimal a = (BigDecimal) num1; 75 BigDecimal b = (BigDecimal) num2; 76 return a.subtract(b); 77 } 78 79 public Number mul(Number num1, Number num2) { 80 BigDecimal a = (BigDecimal) num1; 81 BigDecimal b = (BigDecimal) num2; 82 return a.multiply(b); 83 } 84 85 public Number div(Number num1, Number 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 a,Number b) { 95 return ((BigDecimal) a).compareTo((BigDecimal) b) == 0; 96 } 97 98 public int compare(Number a,Number b) { 99 return ((BigDecimal) a).compareTo((BigDecimal) b); 100 } 101 102 public Number valueOf(String str) { 103 return new BigDecimal(str); 104 } 105 } 106 | Popular Tags |