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
by multiplying each element with a
number chosen such that values range
sum
up to
. If elements in sum
range
sum
to zero, assigns
to
all. Normalization makes sense only if all elements in sum
/ range.length
are
positive. range
assumes that is the case without checking it.
normalize
Prototype
bool normalize(R)( R range, ElementType!R sum = 1 ) if (isForwardRange!R);
Returns
true
if normalization completed normally, false
if
all elements in
were zero or if range
is empty.
range
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