View source code
Display the source code in std/numeric.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
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.
Function std.numeric.sumOfLog2s
Compute the sum of binary logarithms of the input range r
.
The error of this method is much smaller than with a naive sum of log2.
ElementType!Range sumOfLog2s(Range)
(
Range r
)
if (isInputRange!Range && isFloatingPoint!(ElementType!Range));
Example
import std .math .traits : isNaN;
writeln(sumOfLog2s(new double[0])); // 0
writeln(sumOfLog2s([0.0L])); // -real.infinity
writeln(sumOfLog2s([-0.0L])); // -real.infinity
writeln(sumOfLog2s([2.0L])); // 1
assert(sumOfLog2s([-2.0L]) .isNaN());
assert(sumOfLog2s([real .nan]) .isNaN());
assert(sumOfLog2s([-real .nan]) .isNaN());
writeln(sumOfLog2s([real .infinity])); // real.infinity
assert(sumOfLog2s([-real .infinity]) .isNaN());
writeln(sumOfLog2s([0.25, 0.25, 0.25, 0.125])); // -9
Authors
Andrei Alexandrescu, Don Clugston, Robert Jacques, Ilya Yaroshenko
License
Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.