math.nbtheory: Implement natural logarithm

This commit is contained in:
2017-10-02 14:55:30 +02:00
parent 74b085b88d
commit e4cd57a615
4 changed files with 143 additions and 33 deletions

View File

@ -35,6 +35,7 @@ import tanya.memory;
import tanya.meta.trait;
import tanya.meta.transform;
import tanya.range.array;
import tanya.range.primitive;
/**
* Thrown on encoding errors.
@ -352,9 +353,9 @@ struct String
* Precondition: $(D_INLINECODE allocator is null).
*/
this(S)(const S str, shared Allocator allocator = defaultAllocator)
if (!std.range.isInfinite!S
&& std.range.isInputRange!S
&& isSomeChar!(std.range.ElementEncodingType!S))
if (!isInfinite!S
&& isInputRange!S
&& isSomeChar!(ElementType!S))
{
this(allocator);
insertBack(str);
@ -667,12 +668,12 @@ struct String
* Throws: $(D_PSYMBOL UTFException).
*/
size_t insertBack(R)(R str) @trusted
if (!std.range.isInfinite!R
&& std.range.isInputRange!R
&& is(Unqual!(std.range.ElementEncodingType!R) == char))
if (!isInfinite!R
&& isInputRange!R
&& is(Unqual!(ElementType!R) == char))
{
size_t size;
static if (std.range.hasLength!R || isNarrowString!R)
static if (hasLength!R || isNarrowString!R)
{
size = str.length + length;
reserve(size);
@ -731,11 +732,11 @@ struct String
/// ditto
size_t insertBack(R)(R str) @trusted
if (!std.range.isInfinite!R
&& std.range.isInputRange!R
&& is(Unqual!(std.range.ElementEncodingType!R) == wchar))
if (!isInfinite!R
&& isInputRange!R
&& is(Unqual!(ElementType!R) == wchar))
{
static if (std.range.hasLength!R || isNarrowString!R)
static if (hasLength!R || isNarrowString!R)
{
reserve(length + str.length * wchar.sizeof);
}
@ -797,11 +798,11 @@ struct String
/// ditto
size_t insertBack(R)(R str) @trusted
if (!std.range.isInfinite!R
&& std.range.isInputRange!R
&& is(Unqual!(std.range.ElementEncodingType!R) == dchar))
if (!isInfinite!R
&& isInputRange!R
&& is(Unqual!(ElementType!R) == dchar))
{
static if (std.range.hasLength!R || isSomeString!R)
static if (hasLength!R || isSomeString!R)
{
reserve(length + str.length * 4);
}
@ -1268,9 +1269,9 @@ struct String
* Throws: $(D_PSYMBOL UTFException).
*/
ref String opAssign(S)(S that) nothrow
if (!std.range.isInfinite!S
&& std.range.isInputRange!S
&& isSomeChar!(std.range.ElementEncodingType!S))
if (!isInfinite!S
&& isInputRange!S
&& isSomeChar!(ElementType!S))
{
this.length_ = 0;
insertBack(that);
@ -1531,9 +1532,9 @@ struct String
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
*/
size_t insertAfter(T, R)(R r, T el) @trusted
if ((isSomeChar!T || (!std.range.isInfinite!T
&& std.range.isInputRange!T
&& isSomeChar!(std.range.ElementEncodingType!T)))
if ((isSomeChar!T || (!isInfinite!T
&& isInputRange!T
&& isSomeChar!(ElementType!T)))
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
in
{
@ -1564,9 +1565,9 @@ struct String
///
size_t insertBefore(T, R)(R r, T el) @trusted
if ((isSomeChar!T || (!std.range.isInfinite!T
&& std.range.isInputRange!T
&& isSomeChar!(std.range.ElementEncodingType!T)))
if ((isSomeChar!T || (!isInfinite!T
&& isInputRange!T
&& isSomeChar!(ElementType!T)))
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
in
{

View File

@ -17,6 +17,15 @@ module tanya.math.nbtheory;
import tanya.math.mp;
import tanya.meta.trait;
version (TanyaNative)
{
}
else
{
import core.math : fabs;
import std.math : log;
}
/**
* Calculates the absolute value of a number.
*
@ -57,21 +66,19 @@ version (D_Ddoc)
I abs(I)(I x)
if (isFloatingPoint!I);
}
else version (TanyaPhobos)
else version (TanyaNative)
{
extern I abs(I)(I number) pure nothrow @safe @nogc
if (isFloatingPoint!I);
}
else
{
import core.math;
I abs(I)(I x)
if (isFloatingPoint!I)
{
return fabs(cast(real) x);
}
}
else
{
extern I abs(I)(I number) pure nothrow @safe @nogc
if (isFloatingPoint!I);
}
///
pure nothrow @safe @nogc unittest
@ -103,3 +110,56 @@ I abs(I : Integer)(I x)
x.sign = Sign.positive;
return x;
}
version (D_Ddoc)
{
/**
* Calculates natural logarithm of $(D_PARAM x).
*
* Params:
* x = Argument.
*
* Returns: Natural logarithm of $(D_PARAM x).
*/
float ln(float x) pure nothrow @safe @nogc;
/// ditto
double ln(double x) pure nothrow @safe @nogc;
/// ditto
real ln(real x) pure nothrow @safe @nogc;
}
else version (TanyaNative)
{
extern float ln(float x) pure nothrow @safe @nogc;
extern double ln(double x) pure nothrow @safe @nogc;
extern real ln(real x) pure nothrow @safe @nogc;
}
else
{
float ln(float x) pure nothrow @safe @nogc
{
return log(x);
}
double ln(double x) pure nothrow @safe @nogc
{
return log(x);
}
alias ln = log;
}
///
pure nothrow @safe @nogc unittest
{
import tanya.math;
assert(isNaN(ln(-7.389f)));
assert(isNaN(ln(-7.389)));
assert(isNaN(ln(-7.389L)));
assert(isInfinity(ln(0.0f)));
assert(isInfinity(ln(0.0)));
assert(isInfinity(ln(0.0L)));
assert(ln(1.0f) == 0.0f);
assert(ln(1.0) == 0.0);
assert(ln(1.0L) == 0.0L);
}