-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IntMath.sum(int[]), and likewise #309
Comments
Original comment posted by [email protected] on 2010-01-06 at 01:00 AM Agreed. Varargs? The best way to implement Longs.average() is a somewhat interesting puzzle. Status: |
Original comment posted by gpampara on 2010-01-06 at 05:21 AM Could you please elaborate at what the complexities are? I think I'm missing something. |
Original comment posted by hein.meling on 2010-01-06 at 08:12 AM About varargs; currently I have this: int sum(Collection<Integer> c) in my project. Not sure if such a collection can be transformed into an array without copying; if so varargs are Another issue that come to mind when I started to think about test cases for this was: what to return if the sum We have to avoid any silent overflows for this. Maybe the return type should be Number to allow |
Original comment posted by ogregoire on 2010-01-06 at 03:03 PM for the Longs.average() wouldn't it be interesting to use BigInteger as internal Or use an integer to keep track of the number of times the overflow (or underflow) is |
Original comment posted by [email protected] on 2010-01-06 at 04:54 PM Yup, BigInteger is the simple solution, but I'm quite curious to benchmark simpler As for what to do in the case of overflow, I think ArithmeticException seems |
Original comment posted by [email protected] on 2011-01-26 at 10:16 PM (No comment entered for this change.) Labels: |
Original comment posted by [email protected] on 2011-01-27 at 09:07 AM I'd like to have each method to accept iterable, too. While looking at my custom math utilities: What about rate and percentage calculation? Like this: public static double calculateRate(double count, double total) { This might be useful to implement for multiple primitives as one might be dealing with doubles or floats etc. depending on the application. |
Original comment posted by wasserman.louis on 2011-03-15 at 05:24 PM I'm up for writing this, including some attempts I've thought of for "simpler alternatives." |
Original comment posted by raymondofrish on 2011-03-16 at 02:01 AM How about something like the following? Not thorough benchmarked or optimized, but surely better than BigInteger, no? I hear there's a nice microbenchmarking tool available for Java...
|
Original comment posted by [email protected] on 2011-07-13 at 06:18 PM (No comment entered for this change.) Status: |
Original comment posted by [email protected] on 2011-07-13 at 08:40 PM (No comment entered for this change.) |
Original comment posted by blank101 on 2011-07-19 at 07:06 PM Recollecting back to my numerical methods class, there are also some interesting issues for the floating point primitives when calculating sum/average values beyond (over|under)flow. |
Original comment posted by wasserman.louis on 2011-10-18 at 11:26 PM Revisiting... I'm thinking we might just provide sum(), which has a less ambiguous return type. In particular: long IntMath.sum(int... ints) since we can safely assume that the sum of an array of ints will fit into a long. (The result will fit in 96 bits, so we can do intermediate accumulation with smaller types, too...) |
Original comment posted by [email protected] on 2011-10-19 at 07:53 AM So iterables are not supported as arguments? I've got a lot of them floating around when creating reports. |
Original comment posted by dancerjohn on 2011-10-19 at 10:35 AM You can use Longs.toArray(Lists.newArrayList(myIterable)) to convert to an array and pass to a varargs. |
Original comment posted by [email protected] on 2011-10-19 at 12:31 PM That does two copies. Seems a little unnecessary, considering that the "for each" loop introduced in Java 5 can iterate both arrays and iterables. As implementing the loop twice (for each number type) would be somewhat unattractive, one might convert the other way round, i.e. from array to iterable. For the record, a very common use case in my projects is retrieving a total field's value from some iterable of objects using |
Original comment posted by ogregoire on 2011-10-19 at 12:43 PM Can't you have Collections instead of Iterables ? You speak about the transform case, but to be honest, I never had the case where I wanted to perform only one action on my transformed list, so it was always "persisted" in a collection in order to be used more than once without having to re-transform it. Don't forget that Collection2 contains a transform(Collection,Function) method as well... |
Original comment posted by [email protected] on 2011-10-19 at 07:23 PM Well, as long as I can get away with passing just iterables around, I try to do it. Maybe I'm overestimating the memory efficiency here (compared to, say, generators in Python). OTOH, I had the impression that one tried to avoid arrays in favor of collections, especially in Guava. Also, what is a common use case that supports the idea of accepting varargs? I'd expect to either have very few numbers which I then could just easily sum up using the classic |
Original comment posted by [email protected] on 2011-10-20 at 06:17 AM "avoid arrays in favor of collections" -- definitely, for Object arrays, but primitive arrays do make some sense. |
Original comment posted by [email protected] on 2011-12-10 at 03:42 PM (No comment entered for this change.) Labels: |
Original comment posted by [email protected] on 2011-12-15 at 09:32 AM I revisited the use of my custom sum methods and it turns out that they are called with an |
Original comment posted by [email protected] on 2012-02-16 at 07:17 PM (No comment entered for this change.) Status: |
Original comment posted by [email protected] on 2012-05-25 at 06:47 PM I still think the simplest most obvious win here is just to add IntMath.sum(int[]) and friends. I'd like to save the topic of the mean/average for when we look at statistics calculation in general. |
Original comment posted by wasserman.louis on 2012-05-25 at 08:30 PM We need more detail, though:
|
Original comment posted by [email protected] on 2012-05-30 at 07:43 PM (No comment entered for this change.) Labels: - |
Original comment posted by [email protected] on 2012-06-22 at 06:16 PM (No comment entered for this change.) Status: |
Original comment posted by [email protected] on 2012-06-26 at 01:13 PM private final <T> long sum(long bias, Iterable<T> ts, Function<T, Integer> transformer) { |
Original comment posted by wasserman.louis on 2012-06-26 at 01:26 PM Hrrrrm. I'd like this method to do very limited things, because if you need it to do smarter things, you can just write the implementation yourself, given that it's only a few lines at the most. At the moment, I'm leaning towards a very dumb, straightforward implementation. IntMath.sum(int...) returns an int. If you want something more sophisticated, whip it up yourself; this is just a convenience method for the most common case. |
Original comment posted by orionllmain on 2013-05-14 at 01:36 PM int IntMath.sum(int[] array) |
Any progress on this issue? |
Should this never be done in guava as Java 8 has exactly the duplicated API? https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html |
Many people use guava as a backfill of Java 8 functionality for Java 7-and-earlier projects (like Android). |
@lowasser shall we bring this to API review? |
Even with Java 8, there seem to be some methods missing: Using Varargs or int/long streams arguments may be better. |
@Maaartinus With regards to int[] ints = ...;
long sum = Arrays.stream(ints).mapToLong(i -> i).reduce(0, LongMath::checkedAdd); |
Likewise, for long[] longs = ...;
BigInteger sum = Arrays.stream(longs).mapToObj(BigInteger::valueOf).reduce(BigInteger.ZERO, (a, b) -> a.add(b)); |
@jbduncan Agreed. I guess, your |
Am I right to think that it's because a Regardless, I think that
I'm a bit lost but very curious by what you mean here. Can you give me a code example? |
Yes, the minimum value is
There may be tons of errors. |
Is this covered by |
Only technically, but I would not expect Stats to be a good choice in cases where the Stats instance is immediately thrown away. It does too much extra work. |
|
Original issue created by hein.meling on 2010-01-05 at 11:15 PM
Suggest to provide:
sum()
average()
for Ints, Longs, Floats etc.
The text was updated successfully, but these errors were encountered: