Report a bug
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.

# mir.math.numeric

This module contains simple numric algorithms.
Authors:
Ilya Yaroshenko

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.
Examples:
```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(r.prod(e) == 0.8);
assert(e == 10);
assert(r.prod == 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.
Examples:
```import std.math : isNaN;

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