[Bis-parep] [Jhs-mail] Math in Java

Kirkpatrick, Ivan Ivan.Kirkpatrick@dep.state.fl.us
Wed, 8 Jun 2005 17:14:49 -0400


If anyone is interested in doing any mathematical operations in java it =
might
be helpful to take a look at a math helper class Mike and I have been =
working
on.  The source is available at
http://epic52.dep.state.fl.us/arm-pars/xref/index.html

I would appreciate any comments or suggestions in terms of a code review =
from
anyone that might be so inclined.  Note that the checkstyle report still
contains 6 errors which I have not been able to eliminate as yet.

A couple of things came to mind while we were working on this class.

Mike and I decided that we would pass Objects or Collections of Objects
between classes.  For example the math helper class accepts a List of =
Doubles
in the setPercentDiffArray() method.  Internally, for accuracy
considerations, all math operations are performed on double primitives.  =
It
turned out to be rather cumbersome to use BigDecimal Objects internally. =
 The
math helper class returns all math results as BigDecimals so the return =
is
easily rounded to the desired number of decimal points.  I found there =
are
eight different rounding schemes in the BigDecimal class.  These are =
part of
the MathContext in java 1.5 I think. =20

The JUnit test class at
http://epic52.dep.state.fl.us/arm-pars/xref-test/index.html contains =
about 30
or so unit tests that exercise the calculations and the conversions from
Objects to primitives.  It turns out that in java 1.5 (we are all using =
1.4.2
now) the conversion from Objects to primitives and back again is handled
automatically.  This would eliminate all the casting from the List =
object and
in creating Doubles and would make the code a lot better.  I am =
definitely
looking forward to the migration to 1.5 but I am not sure when that will
happen.

Another purpose of the Junit tests is to show examples of how the math =
class
is expected to be used.  Although the tests are pretty simple and not =
very
sophisticated they do help in development.  Because I have so many tests =
and
they are easy to run I can change the code quite easily for experiments =
and
so on and still be assured that I know exactly when it works and when it =
does
not.

I also wanted to show that the math helper class threw Exceptions when =
it
should so I included some JUnit tests specifically designed to cause =
that
condition to occur.  This results in Errors being reported so I am not =
doing
something right in the tests.  Typically we expect failures and are =
obligated
to test for them but errors are a problem.  I can probably study some =
more
JUnit techniques and eliminate this condition in the near future.

As much as I like math, I like it even more when I can get the computer =
to do
it for me.  I learned a lot working on this class and the unit tests.  I =
hope
everyone can benefit from it as well.

Ivan S Kirkpatrick

_______________________________________________
Jhs-mail mailing list
Jhs-mail@lists.dep.state.fl.us
http://lists.dep.state.fl.us/cgi-bin/mailman/listinfo/jhs-mail