math: Add floating point support to abs

This commit is contained in:
2017-09-13 06:43:49 +02:00
parent 3705cf387e
commit 3e9ca359da
5 changed files with 206 additions and 32 deletions

View File

@ -15,6 +15,7 @@
module tanya.math;
public import tanya.math.mp;
public import tanya.math.nbtheory;
public import tanya.math.random;
import tanya.meta.trait;
@ -197,33 +198,3 @@ private pure nothrow @safe @nogc unittest
assert(899809363.isPseudoprime);
assert(982451653.isPseudoprime);
}
/**
* Calculates the absolute value of a number.
*
* Params:
* I = Value type.
* x = Value.
*
* Returns: Absolute value of $(D_PARAM x).
*/
I abs(I : Integer)(const auto ref I x)
{
auto result = Integer(x, x.allocator);
result.sign = Sign.positive;
return result;
}
/// Ditto.
I abs(I : Integer)(I x)
{
x.sign = Sign.positive;
return x;
}
/// Ditto.
I abs(I)(const I x)
if (isIntegral!I)
{
return x >= 0 ? x : -x;
}