|
@@ -2176,22 +2176,20 @@ scale_array_elements_to_u64(uint64_t *entries_out, const double *entries_in,
|
|
|
double total = 0.0;
|
|
|
double scale_factor = 0.0;
|
|
|
int i;
|
|
|
- /* big, but far away from overflowing an int64_t */
|
|
|
-#define SCALE_TO_U64_MAX ((double) (INT64_MAX / 4))
|
|
|
|
|
|
for (i = 0; i < n_entries; ++i)
|
|
|
total += entries_in[i];
|
|
|
|
|
|
- if (total > 0.0)
|
|
|
- scale_factor = SCALE_TO_U64_MAX / total;
|
|
|
+ if (total > 0.0) {
|
|
|
+ scale_factor = ((double)INT64_MAX) / total;
|
|
|
+ scale_factor /= 4.0; /* make sure we're very far away from overflowing */
|
|
|
+ }
|
|
|
|
|
|
for (i = 0; i < n_entries; ++i)
|
|
|
entries_out[i] = tor_llround(entries_in[i] * scale_factor);
|
|
|
|
|
|
if (total_out)
|
|
|
*total_out = (uint64_t) total;
|
|
|
-
|
|
|
-#undef SCALE_TO_U64_MAX
|
|
|
}
|
|
|
|
|
|
/** Pick a random element of <b>n_entries</b>-element array <b>entries</b>,
|