Results 1 to 9 of 9

# Thread: Java arithemetic

- 08-13-2010, 08:42 AM #1Member
- Join Date
- Jul 2010
- Posts
- 15

- Rep Power
- 0

## Java arithemetic

Hi guys,

Does anyone know a simple Java arithemetic library? I am writing a program where i do really basic stuff like multiplying every value in a list of Integers with some factor, then the same for a list of BigDecimals. Summing over listst etc.

I find myself writing generic methods and methods for summing over objects of type Number. This must have been done a thousand times by a thousand other people.

- 08-13-2010, 09:52 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,004
- Blog Entries
- 7

- Rep Power
- 23

Java is not known for its fast arithmetic; maybe the following link can be of help. Most of the stuff is written in Fortran (or C or C++) but you can find some Java stuff in there as well. For your 'level 1' stuff (BLAS terminology) you can easily write it yourself though; it can be fun writing it ...

kind regards,

Jos

- 08-13-2010, 01:19 PM #3Member
- Join Date
- Jul 2010
- Posts
- 15

- Rep Power
- 0

Thanks for your fast reply Jos,

I'm not so much looking for a fast library - i'm summing production per year so my lists have length 12. Then i also multiply the discrete numbers with decimals so that i have a length 12 list of BigDecimals. Easy of use comes before speed in this case :-)

- 08-13-2010, 01:49 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,004
- Blog Entries
- 7

- Rep Power
- 23

- 08-16-2010, 11:10 AM #5Member
- Join Date
- Jul 2010
- Posts
- 15

- Rep Power
- 0

That all very well (ok, its a hassle). But now i have a method that sums all the values in a list of objects that extend Number. It should return a Number of the input type, so summing a list of Integers yields an Integer, summing a list of Long returns a Long etc.

I can implement this method for non empty lists, because i can get the objects inthe list and get their class. If the list itself is null, i just return null.

So now what is the list is empty? My method should return 0. Specifically, it should return a 0 of the same type as the objects (of which there are none) in the empty input list.

Do you see my problem? I cannot know the exact type of the 0 as i dont have an object whose class i can query.

- 08-16-2010, 05:11 PM #6
Can you make a small program that compiles to demonstrate your problem?

- 08-16-2010, 06:09 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,004
- Blog Entries
- 7

- Rep Power
- 23

I understand your problem; unfortunately there is no way during runtime to find the type Double in, say, List<Double> due to type erasure; if the list is empty (i.e. not containing at least one Double) you're out of luck. Would it be possible to keep an 'exemplar'? (a SmallTalk thingie). The type of the exemplar would be the type of the elements in your collection. Even a String representation of the type will do.

kind regards,

Jos

- 08-17-2010, 10:52 AM #8Member
- Join Date
- Jul 2010
- Posts
- 15

- Rep Power
- 0

I decided to cheat a little and return a BigDecimal whatever kind of (number) list i start with, see below.

@Jos

This Smalltalk, i am puzzled how it never got to be 'big' when i hear so many good things about it from so many people. (programmers anyway)

@Norm

This is what i'm doing now. Ideally the addOverAnySubList method would return a value of type T

Java Code:/** * Adds values over sublist of this. * The sublist is exclusive: * addOverSubList([1, 2, 3], 0, 2) = 3 * returns 0 if the list was null or empty */ public static <T extends Number> BigDecimal addOverAnySubList(List<T> values, int from, int to){ if (values == null) { return new BigDecimal(0); } else if (to > (values.size())) { return addOverAnySubList(values, from, (values.size())); } else if((from > values.size() - 1 || from > to)) { throw new IllegalArgumentException("The start position is larger then the list size or is after the end position."); } else { List<T> sub = values.subList(from, to); BigDecimal tot = new BigDecimal(0); for (T b:sub) { tot.add(getBigDecimal(b)); } return tot; } } private static <T extends Number> BigDecimal getBigDecimal(T num) { if (num == null) { return new BigDecimal(0); } else if (num instanceof Double) { double num12 = (Double) num; return new BigDecimal(num12); } else if (num instanceof Integer) { int num12 = (Integer) num; return new BigDecimal(num12); } else if (num instanceof BigDecimal) { return (BigDecimal) num; } else if (num instanceof Float) { double num12 = ((Float) num).doubleValue(); return new BigDecimal(num12); } else if (num instanceof Long) { long num12 = (Long) num; return new BigDecimal(num12); } else { throw new UnsupportedOperationException("Not yet implemented for AtomicInteger, AtomicLong, BigInteger, Byte and Short"); } }

- 08-17-2010, 12:32 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,004
- Blog Entries
- 7

- Rep Power
- 23

SmallTalk was an anachronism; the main stream computing community wasn't up to it yet (it was all Pacal, C, Fortran etc in those days). They praise Java now and almost turn it into a religion, but SmallTalk already had it all in those days (and even better ;-)

kind regards,

Jos

## Bookmarks