0

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:

  1. The sum of all values must be equal to p.
  2. 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?

Filippo Costa
  • 488
  • 7
  • 25
  • 1
    Multiply each element in the list L by `p/SUM(L)`? – CollinD Nov 16 '16 at 00:52
  • That won't work for values without an exact `double` representation. See [this](http://stackoverflow.com/questions/394174/how-best-to-sum-up-lots-of-floating-point-numbers) question. – Filippo Costa Nov 16 '16 at 00:55
  • @FilippoC. Nothing will work for values without an exact `double` representation. It remains the correct answer. – user207421 Nov 16 '16 at 02:51

0 Answers0