[go: up one dir, main page]

Menu

[r10]: / trunk / APIs / ren / util / AD.java  Maximize  Restore  History

Download this file

92 lines (73 with data), 1.9 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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
/*
* Created on 15/02/2006
*
* @author Rene Wooller
*/
package ren.util;
import java.math.BigDecimal;
/**
* AD: Acurate Double!
* thwarting double accuracy errors wherever it goes!
* for counting with doubles
* @author wooller
*
*15/02/2006
*
* Copyright JEDI/Rene Wooller
*
*/
public class AD {
private long v;
private int sc;
public static final double DFACC = 0.0000001;
public AD() {
this(DFACC);
}
public AD(double ac) {
this(ac, 0.0);
}
public AD(double ac, double v) {
this.sc = (int)(Math.log(ac)/Math.log(0.1));
this.set(v);
}
public AD(int sc, double v) {
this.sc = sc;
this.set(v);
}
public void set(double nv) {
//int fsc = Math.max(0, (int)(Math.log(nv)/Math.log(0.1)));
v = (long)(nv*Math.pow(10, sc) + 0.5);
}
public double v() {
return BigDecimal.valueOf(v, sc).doubleValue();
}
public void add(double nv) {
v += (long)(nv*Math.pow(10, sc) + 0.5);
// this.v = v.a
}
public static double mult(double a, double b, int dp) {
BigDecimal da = BigDecimal.valueOf((long)(a*Math.pow(10, dp) + 0.5), dp);
BigDecimal db = BigDecimal.valueOf((long)(b*Math.pow(10, dp) + 0.5), dp);
BigDecimal ret = da.multiply(db);
// PO.p("da = " + da.doubleValue() + " db = " + db + " ret = " + ret.doubleValue());
ret.setScale(dp, BigDecimal.ROUND_HALF_EVEN);
//PO.p("r)
return ret.doubleValue();
}
public static double mult(double a, double b) {
return mult(a, b, 6);
}
/**
* double to scale
* converts a double to a long representation at a certain level of scale
* @param d
* @param scale
* @return
*/
public static long dts(double d, int scale) {
return (long)(d*Math.pow(10, scale) + 0.5);
}
public String toString() {
return String.valueOf(v());
}
}