mirror of
https://github.com/processing/processing4.git
synced 2026-02-04 06:09:17 +01:00
improve sum() functions in processing.data
This commit is contained in:
@@ -508,6 +508,27 @@ public class IntDict {
|
||||
}
|
||||
|
||||
|
||||
public int sum() {
|
||||
long amount = sumLong();
|
||||
if (amount > Integer.MAX_VALUE) {
|
||||
throw new RuntimeException("sum() exceeds " + Integer.MAX_VALUE + ", use sumLong()");
|
||||
}
|
||||
if (amount < Integer.MIN_VALUE) {
|
||||
throw new RuntimeException("sum() less than " + Integer.MIN_VALUE + ", use sumLong()");
|
||||
}
|
||||
return (int) amount;
|
||||
}
|
||||
|
||||
|
||||
public long sumLong() {
|
||||
long sum = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
sum += values[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
public int index(String what) {
|
||||
Integer found = indices.get(what);
|
||||
return (found == null) ? -1 : found.intValue();
|
||||
@@ -676,10 +697,7 @@ public class IntDict {
|
||||
* @return an IntDict with the original keys, mapped to their pct of the total
|
||||
*/
|
||||
public FloatDict getPercent() {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
sum += values[i];
|
||||
}
|
||||
double sum = sum(); // a little more accuracy
|
||||
FloatDict outgoing = new FloatDict();
|
||||
for (int i = 0; i < size(); i++) {
|
||||
double percent = value(i) / sum;
|
||||
|
||||
Reference in New Issue
Block a user