Monday, May 5, 2008

Rhymes For February Birthdays

BigDecimal in

Whenever a test framework we choose the easiest route, which also implies that the use of such data types as String and Integer . However, the time comes when we must face at least some types of data including the type of floating. Experience has taught me that when it comes to Java is the best type of floating-point arithmetic operations to perform is BigDecimal. Perhaps what some of begs the question - why not double . My answer would be an example taken from the http://epramono.blogspot.com :
 public static void main (String [] args) {System.out.println 
(
"(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1) =" +
(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1));

double d = 0.0;
while (d <= 1.0) d += 0.1;
System.out.println ("d =" + d);

System.out . println (0.0175 * 100000 = '+ 0.0175 * 100000);}
Result Output:
 (0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1) = 0.9999999999999999 
d 1.0999999999999999
= 0.0175 * 100000 = 1750.0000000000002
Upss ... what an unpleasant surprise: (

but not what I'm going to write my main theme is the GWT (version 1.4.1960), and in principle support for BigDecimal type in GWT, which generally speaking is simply no. This is obviously a serious problem. .. but there is a solution:
  • go to page: http://code.google.com/p/gwt-math/ .
  • download the two libraries gwt-math-2.0.1.jar , gwt-math-server-2.0.1.jar and put them in the classpath our project.
  • In the file add MyModule.gwt.xml section:
     \u0026lt;inherits name = "com.googlecode.gwt.math.Math" /> 
  • At this end of our work, we can use classes already java.math.BigDecimal, java.math.BigInteger .

In summary, it is often the case when the situation seems to be beznadziena with the help of Google comes through which we discover that the Open Source community predicted that we come across a problem. Hence the first look better than I do in his own way:)