mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 13:40:20 +03:00
Narrow imports in std.math.
This commit is contained in:
parent
943cf022ea
commit
0c4dcb32a0
8 changed files with 246 additions and 143 deletions
|
@ -64,7 +64,9 @@ version (D_HardFloat)
|
|||
Unqual!F pow(F, G)(F x, G n) @nogc @trusted pure nothrow
|
||||
if (isFloatingPoint!(F) && isIntegral!(G))
|
||||
{
|
||||
import std.math : abs, floor, isNaN, log2;
|
||||
import std.math.algebraic : abs;
|
||||
import std.math.rounding : floor;
|
||||
import std.math.traits : isNaN;
|
||||
import std.traits : Unsigned;
|
||||
|
||||
// NaN ^^ 0 is an exception defined by IEEE (yields 1 instead of NaN)
|
||||
|
@ -174,7 +176,7 @@ if (isFloatingPoint!(F) && isIntegral!(G))
|
|||
///
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : feqrel;
|
||||
import std.math.operations : feqrel;
|
||||
|
||||
assert(pow(2.0, 5) == 32.0);
|
||||
assert(pow(1.5, 9).feqrel(38.4433) > 16);
|
||||
|
@ -184,7 +186,7 @@ if (isFloatingPoint!(F) && isIntegral!(G))
|
|||
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : isClose, feqrel;
|
||||
import std.math.operations : isClose, feqrel;
|
||||
|
||||
// Make sure it instantiates and works properly on immutable values and
|
||||
// with various integer and float types.
|
||||
|
@ -218,7 +220,7 @@ if (isFloatingPoint!(F) && isIntegral!(G))
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
assert(isClose(pow(2.0L, 10L), 1024, 1e-18));
|
||||
}
|
||||
|
@ -241,7 +243,8 @@ if (isFloatingPoint!(F) && isIntegral!(G))
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : isClose, isInfinity;
|
||||
import std.math.operations : isClose;
|
||||
import std.math.traits : isInfinity;
|
||||
|
||||
static float f1 = 19100.0f;
|
||||
static float f2 = 0.000012f;
|
||||
|
@ -273,7 +276,7 @@ if (isFloatingPoint!(F) && isIntegral!(G))
|
|||
|
||||
@safe @nogc nothrow pure unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
enum f1 = 19100.0f;
|
||||
enum f2 = 0.000012f;
|
||||
|
@ -487,7 +490,8 @@ if (isIntegral!I && isFloatingPoint!F)
|
|||
Unqual!(Largest!(F, G)) pow(F, G)(F x, G y) @nogc @trusted pure nothrow
|
||||
if (isFloatingPoint!(F) && isFloatingPoint!(G))
|
||||
{
|
||||
import std.math : exp2, fabs, isInfinity, isNaN, log2, signbit, sqrt;
|
||||
import std.math.algebraic : fabs, sqrt;
|
||||
import std.math.traits : isInfinity, isNaN, signbit;
|
||||
|
||||
alias Float = typeof(return);
|
||||
|
||||
|
@ -701,7 +705,7 @@ if (isFloatingPoint!(F) && isFloatingPoint!(G))
|
|||
///
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
assert(isClose(pow(2.0, 3.0), 8.0));
|
||||
assert(isClose(pow(1.5, 10.0), 57.6650390625));
|
||||
|
@ -734,11 +738,11 @@ if (isFloatingPoint!(F) && isFloatingPoint!(G))
|
|||
///
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
// the result is a complex number
|
||||
// which cannot be represented as floating point number
|
||||
import std.math : isNaN;
|
||||
import std.math.traits : isNaN;
|
||||
assert(isNaN(pow(-2.5, -1.5)));
|
||||
|
||||
// use the ^^-operator of std.complex instead
|
||||
|
@ -753,7 +757,7 @@ if (isFloatingPoint!(F) && isFloatingPoint!(G))
|
|||
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : isNaN;
|
||||
import std.math.traits : isNaN;
|
||||
|
||||
assert(pow(1.5, real.infinity) == real.infinity);
|
||||
assert(pow(0.5, real.infinity) == 0.0);
|
||||
|
@ -775,14 +779,16 @@ if (isFloatingPoint!(F) && isFloatingPoint!(G))
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
assert(isClose(pow(2.0L, 10.0L), 1024, 1e-18));
|
||||
}
|
||||
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : isClose, isIdentical, isNaN, PI;
|
||||
import std.math.operations : isClose;
|
||||
import std.math.traits : isIdentical, isNaN;
|
||||
import std.math.constants : PI;
|
||||
|
||||
// Test all the special values. These unittests can be run on Windows
|
||||
// by temporarily changing the version (linux) to version (all).
|
||||
|
@ -852,7 +858,7 @@ if (isFloatingPoint!(F) && isFloatingPoint!(G))
|
|||
// https://issues.dlang.org/show_bug.cgi?id=20508
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : isNaN;
|
||||
import std.math.traits : isNaN;
|
||||
|
||||
assert(isNaN(pow(-double.infinity, 0.5)));
|
||||
|
||||
|
@ -876,7 +882,7 @@ if (isFloatingPoint!(F) && isFloatingPoint!(G))
|
|||
pragma(inline, true)
|
||||
real exp(real x) @trusted pure nothrow @nogc // TODO: @safe
|
||||
{
|
||||
import std.math : LOG2E;
|
||||
import std.math.constants : LOG2E;
|
||||
|
||||
version (InlineAsm_X87)
|
||||
{
|
||||
|
@ -900,7 +906,8 @@ float exp(float x) @safe pure nothrow @nogc { return __ctfe ? cast(float) exp(ca
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.math : feqrel, E;
|
||||
import std.math.operations : feqrel;
|
||||
import std.math.constants : E;
|
||||
|
||||
assert(exp(0.0) == 1.0);
|
||||
assert(exp(3.0).feqrel(E * E * E) > 16);
|
||||
|
@ -908,7 +915,11 @@ float exp(float x) @safe pure nothrow @nogc { return __ctfe ? cast(float) exp(ca
|
|||
|
||||
private T expImpl(T)(T x) @safe pure nothrow @nogc
|
||||
{
|
||||
import std.math : floatTraits, RealFormat, isNaN, floor, poly, LOG2E;
|
||||
import std.math : floatTraits, RealFormat;
|
||||
import std.math.traits : isNaN;
|
||||
import std.math.rounding : floor;
|
||||
import std.math.algebraic : poly;
|
||||
import std.math.constants : LOG2E;
|
||||
|
||||
alias F = floatTraits!T;
|
||||
static if (F.realFormat == RealFormat.ieeeSingle)
|
||||
|
@ -1045,12 +1056,16 @@ private T expImpl(T)(T x) @safe pure nothrow @nogc
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : floatTraits, RealFormat, NaN, E, feqrel, isIdentical, abs, isClose;
|
||||
import std.math : floatTraits, RealFormat;
|
||||
import std.math.operations : NaN, feqrel, isClose;
|
||||
import std.math.constants : E;
|
||||
import std.math.traits : isIdentical;
|
||||
import std.math.algebraic : abs;
|
||||
|
||||
version (IeeeFlagsSupport) import std.math : IeeeFlags, resetIeeeFlags, ieeeFlags;
|
||||
version (IeeeFlagsSupport) import std.math.hardware : IeeeFlags, resetIeeeFlags, ieeeFlags;
|
||||
version (FloatingPointControlSupport)
|
||||
{
|
||||
import std.math : FloatingPointControl;
|
||||
import std.math.hardware : FloatingPointControl;
|
||||
|
||||
FloatingPointControl ctrl;
|
||||
if (FloatingPointControl.hasExceptionTraps)
|
||||
|
@ -1237,7 +1252,8 @@ float expm1(float x) @safe pure nothrow @nogc
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.math : isIdentical, feqrel;
|
||||
import std.math.traits : isIdentical;
|
||||
import std.math.operations : feqrel;
|
||||
|
||||
assert(isIdentical(expm1(0.0), 0.0));
|
||||
assert(expm1(1.0).feqrel(1.71828) > 16);
|
||||
|
@ -1414,7 +1430,10 @@ L_largenegative:
|
|||
|
||||
private T expm1Impl(T)(T x) @safe pure nothrow @nogc
|
||||
{
|
||||
import std.math : floatTraits, RealFormat, floor, poly, LN2;
|
||||
import std.math : floatTraits, RealFormat;
|
||||
import std.math.rounding : floor;
|
||||
import std.math.algebraic : poly;
|
||||
import std.math.constants : LN2;
|
||||
|
||||
// Coefficients for exp(x) - 1 and overflow/underflow limits.
|
||||
enum realFormat = floatTraits!T.realFormat;
|
||||
|
@ -1533,7 +1552,8 @@ private T expm1Impl(T)(T x) @safe pure nothrow @nogc
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : isNaN, isClose, CommonDefaultFor;
|
||||
import std.math.traits : isNaN;
|
||||
import std.math.operations : isClose, CommonDefaultFor;
|
||||
|
||||
static void testExpm1(T)()
|
||||
{
|
||||
|
@ -1588,7 +1608,8 @@ float exp2(float x) @nogc @safe pure nothrow { return __ctfe ? cast(float) exp2(
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.math : isIdentical, feqrel;
|
||||
import std.math.traits : isIdentical;
|
||||
import std.math.operations : feqrel;
|
||||
|
||||
assert(isIdentical(exp2(0.0), 1.0));
|
||||
assert(exp2(2.0).feqrel(4.0) > 16);
|
||||
|
@ -1801,7 +1822,10 @@ L_was_nan:
|
|||
|
||||
private T exp2Impl(T)(T x) @nogc @safe pure nothrow
|
||||
{
|
||||
import std.math : floatTraits, RealFormat, isNaN, floor, poly;
|
||||
import std.math : floatTraits, RealFormat;
|
||||
import std.math.traits : isNaN;
|
||||
import std.math.rounding : floor;
|
||||
import std.math.algebraic : poly;
|
||||
|
||||
// Coefficients for exp2(x)
|
||||
enum realFormat = floatTraits!T.realFormat;
|
||||
|
@ -1920,7 +1944,9 @@ private T exp2Impl(T)(T x) @nogc @safe pure nothrow
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : feqrel, NaN, isIdentical, SQRT2, isClose;
|
||||
import std.math.operations : feqrel, NaN, isClose;
|
||||
import std.math.traits : isIdentical;
|
||||
import std.math.constants : SQRT2;
|
||||
|
||||
assert(feqrel(exp2(0.5L), SQRT2) >= real.mant_dig -1);
|
||||
assert(exp2(8.0L) == 256.0);
|
||||
|
@ -1985,7 +2011,8 @@ private T exp2Impl(T)(T x) @nogc @safe pure nothrow
|
|||
T frexp(T)(const T value, out int exp) @trusted pure nothrow @nogc
|
||||
if (isFloatingPoint!T)
|
||||
{
|
||||
import std.math : isSubnormal, floatTraits, RealFormat;
|
||||
import std.math : floatTraits, RealFormat;
|
||||
import std.math.traits : isSubnormal;
|
||||
|
||||
if (__ctfe)
|
||||
{
|
||||
|
@ -2211,7 +2238,7 @@ if (isFloatingPoint!T)
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
int exp;
|
||||
real mantissa = frexp(123.456L, exp);
|
||||
|
@ -2228,7 +2255,7 @@ if (isFloatingPoint!T)
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
int exp;
|
||||
real mantissa = frexp(123.456L, exp);
|
||||
|
@ -2239,7 +2266,8 @@ if (isFloatingPoint!T)
|
|||
|
||||
@safe unittest
|
||||
{
|
||||
import std.math : isIdentical, floatTraits, RealFormat;
|
||||
import std.math : floatTraits, RealFormat;
|
||||
import std.math.traits : isIdentical;
|
||||
import std.meta : AliasSeq;
|
||||
import std.typecons : tuple, Tuple;
|
||||
|
||||
|
@ -2570,7 +2598,8 @@ alias FP_ILOGBNAN = core.stdc.math.FP_ILOGBNAN;
|
|||
|
||||
@safe nothrow @nogc unittest
|
||||
{
|
||||
import std.math : nextUp, floatTraits, RealFormat;
|
||||
import std.math : floatTraits, RealFormat;
|
||||
import std.math.operations : nextUp;
|
||||
import std.meta : AliasSeq;
|
||||
import std.typecons : Tuple;
|
||||
static foreach (F; AliasSeq!(float, double, real))
|
||||
|
@ -2720,7 +2749,7 @@ float ldexp(float n, int exp) @safe pure nothrow @nogc { return core.math.ldex
|
|||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
static real[3][] vals = // value,exp,ldexp
|
||||
[
|
||||
|
@ -2861,7 +2890,9 @@ private
|
|||
*/
|
||||
real log(real x) @safe pure nothrow @nogc
|
||||
{
|
||||
import std.math : LN2, LOG2, isInfinity, isNaN, signbit, SQRT1_2, poly;
|
||||
import std.math.constants : LN2, LOG2, SQRT1_2;
|
||||
import std.math.traits : isInfinity, isNaN, signbit;
|
||||
import std.math.algebraic : poly;
|
||||
|
||||
version (INLINE_YL2X)
|
||||
return core.math.yl2x(x, LN2);
|
||||
|
@ -2941,7 +2972,8 @@ real log(real x) @safe pure nothrow @nogc
|
|||
///
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : feqrel, E;
|
||||
import std.math.operations : feqrel;
|
||||
import std.math.constants : E;
|
||||
|
||||
assert(feqrel(log(E), 1) >= real.mant_dig - 1);
|
||||
}
|
||||
|
@ -2958,7 +2990,9 @@ real log(real x) @safe pure nothrow @nogc
|
|||
*/
|
||||
real log10(real x) @safe pure nothrow @nogc
|
||||
{
|
||||
import std.math : LOG2, LN2, fabs, isNaN, isInfinity, signbit, SQRT1_2;
|
||||
import std.math.constants : LOG2, LN2, SQRT1_2;
|
||||
import std.math.algebraic : fabs;
|
||||
import std.math.traits : isNaN, isInfinity, signbit;
|
||||
|
||||
version (INLINE_YL2X)
|
||||
return core.math.yl2x(x, LOG2);
|
||||
|
@ -3042,7 +3076,7 @@ real log10(real x) @safe pure nothrow @nogc
|
|||
///
|
||||
@safe pure nothrow @nogc unittest
|
||||
{
|
||||
import std.math : fabs;
|
||||
import std.math.algebraic : fabs;
|
||||
|
||||
assert(fabs(log10(1000) - 3) < .000001);
|
||||
}
|
||||
|
@ -3063,7 +3097,9 @@ real log10(real x) @safe pure nothrow @nogc
|
|||
*/
|
||||
real log1p(real x) @safe pure nothrow @nogc
|
||||
{
|
||||
import std.math : fabs, isNaN, isInfinity, signbit, LN2;
|
||||
import std.math.algebraic : fabs;
|
||||
import std.math.traits : isNaN, isInfinity, signbit;
|
||||
import std.math.constants : LN2;
|
||||
|
||||
version (INLINE_YL2X)
|
||||
{
|
||||
|
@ -3090,7 +3126,8 @@ real log1p(real x) @safe pure nothrow @nogc
|
|||
///
|
||||
@safe pure unittest
|
||||
{
|
||||
import std.math : isIdentical, isNaN, feqrel;
|
||||
import std.math.traits : isIdentical, isNaN;
|
||||
import std.math.operations : feqrel;
|
||||
|
||||
assert(isIdentical(log1p(0.0), 0.0));
|
||||
assert(log1p(1.0).feqrel(0.69314) > 16);
|
||||
|
@ -3115,7 +3152,9 @@ real log1p(real x) @safe pure nothrow @nogc
|
|||
*/
|
||||
real log2(real x) @safe pure nothrow @nogc
|
||||
{
|
||||
import std.math : isNaN, isInfinity, signbit, SQRT1_2, poly;
|
||||
import std.math.traits : isNaN, isInfinity, signbit;
|
||||
import std.math.constants : SQRT1_2;
|
||||
import std.math.algebraic : poly;
|
||||
|
||||
version (INLINE_YL2X)
|
||||
return core.math.yl2x(x, 1.0L);
|
||||
|
@ -3190,14 +3229,14 @@ real log2(real x) @safe pure nothrow @nogc
|
|||
///
|
||||
@safe unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
assert(isClose(log2(1024.0L), 10));
|
||||
}
|
||||
|
||||
@safe @nogc nothrow unittest
|
||||
{
|
||||
import std.math : isClose;
|
||||
import std.math.operations : isClose;
|
||||
|
||||
// check if values are equal to 19 decimal digits of precision
|
||||
assert(isClose(log2(1024.0L), 10, 1e-18));
|
||||
|
@ -3292,7 +3331,7 @@ float scalbn(float x, int n) @safe pure nothrow @nogc { return _scalbn(x,n); }
|
|||
pragma(inline, true)
|
||||
private F _scalbn(F)(F x, int n)
|
||||
{
|
||||
import std.math : isInfinity;
|
||||
import std.math.traits : isInfinity;
|
||||
|
||||
if (__ctfe)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue