Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions std/numeric.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Distributed under the Boost Software License, Version 1.0.
module std.numeric;

import std.complex;
import std.exception;
import std.math;
import std.range.primitives;
import std.traits;
Expand Down Expand Up @@ -183,7 +182,7 @@ private:

// If on Linux or Mac, where 80-bit reals are padded, ignore the
// padding.
import std.algorithm : min;
import std.algorithm.comparison : min;
CustomFloat!(CustomFloatParams!(min(F.sizeof*8, 80))) get;

// Convert F to the correct binary type.
Expand Down Expand Up @@ -260,6 +259,7 @@ private:
// Set the current value from signed exponent, normalized form
void fromNormalized(T,U)(ref T sig, ref U exp)
{
import std.exception : enforce;
auto shift = (T.sizeof*8) - precision;
if (exp == exp.max)
{
Expand Down Expand Up @@ -495,6 +495,8 @@ public:
if (__traits(compiles, cast(real)input))
{
import std.conv : text;
import std.exception : enforce;

static if (staticIndexOf!(Unqual!F, float, double, real) >= 0)
auto value = ToBinary!(Unqual!F)(input);
else
Expand Down Expand Up @@ -1626,6 +1628,8 @@ CommonType!(ElementType!(Range1), ElementType!(Range2))
euclideanDistance(Range1, Range2)(Range1 a, Range2 b)
if (isInputRange!(Range1) && isInputRange!(Range2))
{
import std.exception : enforce;

enum bool haveLen = hasLength!(Range1) && hasLength!(Range2);
static if (haveLen) enforce(a.length == b.length);
Unqual!(typeof(return)) result = 0;
Expand All @@ -1643,6 +1647,8 @@ CommonType!(ElementType!(Range1), ElementType!(Range2))
euclideanDistance(Range1, Range2, F)(Range1 a, Range2 b, F limit)
if (isInputRange!(Range1) && isInputRange!(Range2))
{
import std.exception : enforce;

limit *= limit;
enum bool haveLen = hasLength!(Range1) && hasLength!(Range2);
static if (haveLen) enforce(a.length == b.length);
Expand Down Expand Up @@ -1686,6 +1692,8 @@ dotProduct(Range1, Range2)(Range1 a, Range2 b)
if (isInputRange!(Range1) && isInputRange!(Range2) &&
!(isArray!(Range1) && isArray!(Range2)))
{
import std.exception : enforce;

enum bool haveLen = hasLength!(Range1) && hasLength!(Range2);
static if (haveLen) enforce(a.length == b.length);
Unqual!(typeof(return)) result = 0;
Expand Down Expand Up @@ -1754,6 +1762,7 @@ dotProduct(F1, F2)(in F1[] avector, in F2[] bvector)
@system unittest
{
// @system due to dotProduct and assertCTFEable
import std.exception : assertCTFEable;
import std.meta : AliasSeq;
foreach (T; AliasSeq!(double, const double, immutable double))
{
Expand Down Expand Up @@ -1781,6 +1790,8 @@ CommonType!(ElementType!(Range1), ElementType!(Range2))
cosineSimilarity(Range1, Range2)(Range1 a, Range2 b)
if (isInputRange!(Range1) && isInputRange!(Range2))
{
import std.exception : enforce;

enum bool haveLen = hasLength!(Range1) && hasLength!(Range2);
static if (haveLen) enforce(a.length == b.length);
Unqual!(typeof(return)) norma = 0, normb = 0, dotprod = 0;
Expand Down Expand Up @@ -1975,6 +1986,8 @@ CommonType!(ElementType!Range1, ElementType!Range2)
kullbackLeiblerDivergence(Range1, Range2)(Range1 a, Range2 b)
if (isInputRange!(Range1) && isInputRange!(Range2))
{
import std.exception : enforce;

enum bool haveLen = hasLength!(Range1) && hasLength!(Range2);
static if (haveLen) enforce(a.length == b.length);
Unqual!(typeof(return)) result = 0;
Expand Down Expand Up @@ -2021,6 +2034,8 @@ jensenShannonDivergence(Range1, Range2)(Range1 a, Range2 b)
if (isInputRange!Range1 && isInputRange!Range2 &&
is(CommonType!(ElementType!Range1, ElementType!Range2)))
{
import std.exception : enforce;

enum bool haveLen = hasLength!(Range1) && hasLength!(Range2);
static if (haveLen) enforce(a.length == b.length);
Unqual!(typeof(return)) result = 0;
Expand Down Expand Up @@ -2049,6 +2064,8 @@ jensenShannonDivergence(Range1, Range2, F)(Range1 a, Range2 b, F limit)
is(typeof(CommonType!(ElementType!Range1, ElementType!Range2).init
>= F.init) : bool))
{
import std.exception : enforce;

enum bool haveLen = hasLength!(Range1) && hasLength!(Range2);
static if (haveLen) enforce(a.length == b.length);
Unqual!(typeof(return)) result = 0;
Expand Down Expand Up @@ -2162,7 +2179,8 @@ F gapWeightedSimilarity(alias comp = "a == b", R1, R2, F)(R1 s, R2 t, F lambda)
isRandomAccessRange!(R2) && hasLength!(R2))
{
import std.functional : binaryFun;
import std.algorithm : swap;
import std.algorithm.mutation : swap;
import std.exception : enforce;
import core.stdc.stdlib : malloc, free;

if (s.length < t.length) return gapWeightedSimilarity(t, s, lambda);
Expand Down Expand Up @@ -2301,6 +2319,7 @@ time and computes all matches of length 1.
*/
this(Range s, Range t, F lambda)
{
import std.exception : enforce, errnoEnforce;
enforce(lambda > 0);
this.gram = 0;
this.lambda = lambda;
Expand Down Expand Up @@ -2369,7 +2388,7 @@ time and computes all matches of length 1.
*/
void popFront()
{
import std.algorithm : swap;
import std.algorithm.mutation : swap;

// This is a large source of optimization: if similarity at
// the gram-1 level was 0, then we can safely assume
Expand Down Expand Up @@ -2576,6 +2595,7 @@ T gcd(T)(T a, T b)
{
static if (T.min < 0)
{
import std.exception : enforce;
enforce(a >= 0 && b >=0);
}
while (b)
Expand Down Expand Up @@ -2615,7 +2635,7 @@ private alias lookup_t = float;
*/
final class Fft
{
import std.algorithm : map;
import std.algorithm.iteration : map;
import core.bitop : bsf;
import std.array : uninitializedArray;

Expand All @@ -2625,6 +2645,7 @@ private:
void enforceSize(R)(R range) const
{
import std.conv : text;
import std.exception : enforce;
enforce(range.length <= size, text(
"FFT size mismatch. Expected ", size, ", got ", range.length));
}
Expand Down Expand Up @@ -2850,6 +2871,7 @@ private:
// to immutable.
public this(lookup_t[] memSpace) // Public b/c of bug 4636.
{
import std.exception : enforce;
immutable size = memSpace.length / 2;

/* Create a lookup table of all negative sine values at a resolution of
Expand Down Expand Up @@ -2964,6 +2986,7 @@ public:
void fft(Ret, R)(R range, Ret buf) const
if (isRandomAccessRange!Ret && isComplexLike!(ElementType!Ret) && hasSlicing!Ret)
{
import std.exception : enforce;
enforce(buf.length == range.length);
enforceSize(range);

Expand Down