Report a bug
If you spot a problem with this page, click here to create a Github 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 a local clone.


This module contains simple numric algorithms.
Ilya Yaroshenko

SponsorsThis work has been sponsored by and Kaleidic Associates.

struct Prod(T) if (isFloatingPoint!T);
long exp;
T x;
Unqual!(ForeachType!Range) prod(Range)(Range r, ref long exp)
if (isFloatingPoint!(ForeachType!Range));

Unqual!(ForeachType!Range) prod(Range)(Range r)
if (isFloatingPoint!(ForeachType!Range));
Compute the product of the input range r using separate exponent accomulation.
enum l = 2.0 ^^ (double.max_exp - 1);
enum s = 2.0 ^^ -(double.max_exp - 1);
auto r = [l, l, l, s, s, s, 0.8 * 2.0 ^^ 10];
long e;
assert( == 0.8);
assert(e == 10);
assert( == 0.8 * 2.0 ^^ 10);
Unqual!(ForeachType!Range) sumOfLog2s(Range)(Range r)
if (isFloatingPoint!(ForeachType!Range));
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.
import std.math : isNaN;

assert(sumOfLog2s(new double[0]) == 0);
assert(sumOfLog2s([0.0L]) == -real.infinity);
assert(sumOfLog2s([-0.0L]) == -real.infinity);
assert(sumOfLog2s([2.0L]) == 1);
assert(sumOfLog2s([real.infinity]) == real.infinity);
assert(sumOfLog2s([ 0.25, 0.25, 0.25, 0.125 ]) == -9);