View source code Display the source code in std/numeric.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Function std.numeric.normalize

Normalizes values in range by multiplying each element with a number chosen such that values sum up to sum. If elements in range sum to zero, assigns sum / range.length to all. Normalization makes sense only if all elements in range are positive. normalize assumes that is the case without checking it.

Prototype

bool normalize(R)(
  R range,
  ElementType!R sum = 1
)
if (isForwardRange!R);

Returns

true if normalization completed normally, false if all elements in range were zero or if range is empty.

Example

double[] a = [];
assert(!normalize(a));
a = [ 1.0, 3.0 ];
assert(normalize(a));
assert(a == [ 0.25, 0.75 ]);
a = [ 0.0, 0.0 ];
assert(!normalize(a));
assert(a == [ 0.5, 0.5 ]);

Authors

Andrei Alexandrescu, Don Clugston, Robert Jacques

License

Boost License 1.0.

Comments