I am writing a JVM-hosted application that deals with many floating point numbers; for this reason, the program faces severe precision loss problems. Real values are a no-no as performance is an issue, too.
Given a collection of positive doubles (length can be any above 100 and below 100,000) I need to change each element accordingly to a parameter p, with two fundamental requirements:
- The sum of all values must be equal to p.
- Ratios should remain equal, or change as little as possible.
A very simple example with n=3 and p=10.0: (0.45, 2.0, 0.05)
becomes (1.8, 8.0, 0.2)
.
Is there any library to achieve this result? If not, what algorithm should I use?