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.
mir.math.common
Common floating point math functions.
This module has generic LLVM-oriented API compatible with all D compilers.
License:
Authors:
Ilia Ki, Phobos Team
- alias
fmamath
= T; - Functions attribute, an alias for AliasSeq!(llvmFastMathFlag("contract"));.
- 1. Allow floating-point contraction (e.g. fusing a multiply followed by an addition into a fused multiply-and-add).
Note Can be used with all compilers.
- alias
optmath
= T; - Functions attribute, an alias for AliasSeq!(llvmFastMathFlag("fast")).It is similar to fastmath, but does not allow unsafe-fp-math. This flag does NOT force LDC to use the reciprocal of an argument rather than perform division. This flag is default for string lambdas.
Note Can be used with all compilers.
- alias
fastmath
= TList; - Functions attribute, an alias for ldc.attributes.
fastmath
.- 1. Enable optimizations that make unsafe assumptions about IEEE math (e.g. that addition is associative) or may not work for all input ranges. These optimizations allow the code generator to make use of some instructions which would otherwise not be usable (such as fsin on X86).
- 2. Allow optimizations to assume the arguments and result are not NaN. Such optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.
- 3. Allow optimizations to assume the arguments and result are not +`-inf. Such optimizations are required to retain defined behavior over +`-Inf, but the value of the result is undefined.
- 4. Allow optimizations to treat the sign of a zero argument or result as insignificant.
- 5. Allow optimizations to use the reciprocal of an argument rather than perform division.
- 6. Allow floating-point contraction (e.g. fusing a multiply followed by an addition into a fused multiply-and-add).
- 7. Allow algebraically equivalent transformations that may dramatically change results in floating point (e.g. reassociate).
Note Can be used with all compilers.
- T
sqrt
(T)(in Tval
)
if (isFloatingPoint!T); - T
sin
(T)(in Tval
)
if (isFloatingPoint!T); - T
cos
(T)(in Tval
)
if (isFloatingPoint!T); - T
powi
(T)(in Tval
, intpower
)
if (isFloatingPoint!T); - T
pow
(T)(in Tval
, in Tpower
)
if (isFloatingPoint!T); - T
exp
(T)(in Tval
)
if (isFloatingPoint!T); - T
log
(T)(in Tval
)
if (isFloatingPoint!T); - T
fma
(T)(Tvala
, Tvalb
, Tvalc
)
if (isFloatingPoint!T); - T
fabs
(T)(in Tval
)
if (isFloatingPoint!T); - T
floor
(T)(in Tval
)
if (isFloatingPoint!T); - T
exp2
(T)(in Tval
)
if (isFloatingPoint!T); - T
log10
(T)(in Tval
)
if (isFloatingPoint!T); - T
log2
(T)(in Tval
)
if (isFloatingPoint!T); - T
ceil
(T)(in Tval
)
if (isFloatingPoint!T); - T
trunc
(T)(in Tval
)
if (isFloatingPoint!T); - T
rint
(T)(in Tval
)
if (isFloatingPoint!T); - T
nearbyint
(T)(in Tval
)
if (isFloatingPoint!T); - T
copysign
(T)(in Tmag
, in Tsgn
)
if (isFloatingPoint!T); - T
round
(T)(in Tval
)
if (isFloatingPoint!T); - T
fmuladd
(T)(in Tvala
, in Tvalb
, in Tvalc
)
if (isFloatingPoint!T); - T
fmin
(T)(in Tvala
, in Tvalb
)
if (isFloatingPoint!T); - T
fmax
(T)(in Tvala
, in Tvalb
)
if (isFloatingPoint!T); - auto
fabs
(T)(in Tx
)
if (isComplex!T); - Overload for cdouble, cfloat and crealExamples:
import mir.complex; assert(fabs(Complex!double(3, 4)) == 25);
- bool
approxEqual
(T)(const Tlhs
, const Trhs
, const TmaxRelDiff
= T(9.53674e-07F), const TmaxAbsDiff
= T(9.53674e-07F))
if (isFloatingPoint!T);
boolapproxEqual
(T)(const Tlhs
, const Trhs
, floatmaxRelDiff
= 9.53674e-07F, floatmaxAbsDiff
= 9.53674e-07F)
if (isComplexOf!(T, float));
boolapproxEqual
(T)(const Tlhs
, const Trhs
, doublemaxRelDiff
= 9.53674e-07F, doublemaxAbsDiff
= 9.53674e-07F)
if (isComplexOf!(T, double));
boolapproxEqual
(T)(const Tlhs
, const Trhs
, realmaxRelDiff
= 9.53674e-07F, realmaxAbsDiff
= 9.53674e-07F)
if (isComplexOf!(T, real)); - Computes whether two values are approximately equal, admitting a maximum relative difference, and a maximum absolute difference.Parameters:
T lhs
First item to compare. T rhs
Second item to compare. T maxRelDiff
Maximum allowable difference relative to rhs
. Defaults to 0.5 ^^ 20.T maxAbsDiff
Maximum absolute difference. Defaults to 0.5 ^^ 20. Returns:true if the two items are equal or approximately equal under either criterium.Examples:assert(approxEqual(1.0, 1.0000001)); assert(approxEqual(1.0f, 1.0000001f)); assert(approxEqual(1.0L, 1.0000001L)); assert(approxEqual(10000000.0, 10000001)); assert(approxEqual(10000000f, 10000001f)); assert(!approxEqual(100000.0L, 100001L));
Examples:Complex types works asapproxEqual
(l.re, r.re) &&approxEqual
(l.im, r.im)import mir.internal.utility: isComplexOf; static struct UserComplex(T) { T re, im; } alias _cdouble = UserComplex!double; static assert(isComplexOf!(_cdouble, double)); assert(approxEqual(_cdouble(1.0, 1), _cdouble(1.0000001, 1), 1.0000001)); assert(!approxEqual(_cdouble(100000.0L, 0), _cdouble(100001L, 0)));
Copyright © 2016-2023 by Ilya Yaroshenko | Page generated by
Ddoc on Mon Nov 6 15:24:34 2023