Skip to content

Commit

Permalink
Merge pull request #6014 from wilzbach/complex
Browse files Browse the repository at this point in the history
Start to move away from complex and imaginary types
merged-on-behalf-of: Andrei Alexandrescu <[email protected]>
  • Loading branch information
dlang-bot authored Jan 18, 2018
2 parents 0666fc6 + a972e26 commit 976cb99
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 85 deletions.
2 changes: 1 addition & 1 deletion posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ else
endif

# Set DFLAGS
DFLAGS=-conf= -I$(DRUNTIME_PATH)/import $(DMDEXTRAFLAGS) -w -de -dip25 $(MODEL_FLAG) $(PIC)
DFLAGS=-conf= -I$(DRUNTIME_PATH)/import $(DMDEXTRAFLAGS) -w -de -dip25 $(MODEL_FLAG) $(PIC) -transition=complex
ifeq ($(BUILD),debug)
DFLAGS += -g -debug
else
Expand Down
59 changes: 53 additions & 6 deletions std/complex.d
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,6 @@ Complex!(CommonType!(T, U)) fromPolar(T, U)(T modulus, U argument)
*/
Complex!T sin(T)(Complex!T z) @safe pure nothrow @nogc
{
import std.math : expi, coshisinh;
auto cs = expi(z.re);
auto csh = coshisinh(z.im);
return typeof(return)(cs.im * csh.re, cs.re * csh.im);
Expand All @@ -839,7 +838,6 @@ Complex!T sin(T)(Complex!T z) @safe pure nothrow @nogc
/// ditto
Complex!T cos(T)(Complex!T z) @safe pure nothrow @nogc
{
import std.math : expi, coshisinh;
auto cs = expi(z.re);
auto csh = coshisinh(z.im);
return typeof(return)(cs.re * csh.re, - cs.im * csh.im);
Expand All @@ -849,12 +847,16 @@ Complex!T cos(T)(Complex!T z) @safe pure nothrow @nogc
@safe pure nothrow unittest
{
import std.complex;
import std.math;
assert(cos(complex(0.0)) == 1.0);
assert(cos(complex(1.3L)) == std.math.cos(1.3L));
assert(cos(complex(0, 5.2L)) == cosh(5.2L));
}

deprecated
@safe pure nothrow unittest
{
import std.math;
assert(cos(complex(0, 5.2L)) == cosh(5.2L));
assert(cos(complex(1.3L)) == std.math.cos(1.3L));
}

/**
Params: y = A real number.
Expand All @@ -874,16 +876,61 @@ Complex!real expi(real y) @trusted pure nothrow @nogc

///
@safe pure nothrow unittest
{
import std.math : cos, sin;
assert(expi(0.0L) == 1.0L);
assert(expi(1.3e5L) == complex(cos(1.3e5L), sin(1.3e5L)));
}

deprecated
@safe pure nothrow unittest
{
static import std.math;

assert(expi(1.3e5L) == complex(std.math.cos(1.3e5L), std.math.sin(1.3e5L)));
assert(expi(0.0L) == 1.0L);
auto z1 = expi(1.234);
auto z2 = std.math.expi(1.234);
assert(z1.re == z2.re && z1.im == z2.im);
}

/**
Params: y = A real number.
Returns: The value of cosh(y) + i sinh(y)
Note:
$(D coshisinh) is included here for convenience and for easy migration of code
that uses $(REF _coshisinh, std,math).
*/
Complex!real coshisinh(real y) @safe pure nothrow @nogc
{
static import std.math;
if (std.math.fabs(y) <= 0.5)
return Complex!real(std.math.cosh(y), std.math.sinh(y));
else
{
auto z = std.math.exp(y);
auto zi = 0.5 / z;
z = 0.5 * z;
return Complex!real(z + zi, z - zi);
}
}

///
@safe pure nothrow @nogc unittest
{
import std.math : cosh, sinh;
assert(coshisinh(3.0L) == complex(cosh(3.0L), sinh(3.0L)));
}

deprecated
@safe pure nothrow @nogc unittest
{
static import std.math;
assert(coshisinh(3.0L) == complex(std.math.cosh(3.0L), std.math.sinh(3.0L)));
auto z1 = coshisinh(1.234);
auto z2 = std.math.coshisinh(1.234);
assert(z1.re == z2.re && z1.im == z2.im);
}

/**
Params: z = A complex number.
Expand Down
32 changes: 22 additions & 10 deletions std/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,7 @@ if (is(FloatingPointTypeOf!T) && !is(T == enum) && !hasToString!(T, Char))
/*
Formatting a $(D creal) is deprecated but still kept around for a while.
*/
deprecated("Use of complex types is deprecated. Use std.complex")
private void formatValueImpl(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(Unqual!T : creal) && !is(T == enum) && !hasToString!(T, Char))
{
Expand All @@ -2604,6 +2605,8 @@ if (is(Unqual!T : creal) && !is(T == enum) && !hasToString!(T, Char))
put(w, 'i');
}

version(TestComplex)
deprecated
@safe /*pure*/ unittest // formatting floating point values is now impure
{
import std.conv : to;
Expand All @@ -2621,6 +2624,8 @@ if (is(Unqual!T : creal) && !is(T == enum) && !hasToString!(T, Char))
}
}

version(TestComplex)
deprecated
@system unittest
{
formatTest( 3+2.25i, "3+2.25i" );
Expand All @@ -2641,6 +2646,7 @@ if (is(Unqual!T : creal) && !is(T == enum) && !hasToString!(T, Char))
/*
Formatting an $(D ireal) is deprecated but still kept around for a while.
*/
deprecated("Use of imaginary types is deprecated. Use std.complex")
private void formatValueImpl(Writer, T, Char)(auto ref Writer w, T obj, const ref FormatSpec!Char f)
if (is(Unqual!T : ireal) && !is(T == enum) && !hasToString!(T, Char))
{
Expand All @@ -2650,6 +2656,8 @@ if (is(Unqual!T : ireal) && !is(T == enum) && !hasToString!(T, Char))
put(w, 'i');
}

version(TestComplex)
deprecated
@safe /*pure*/ unittest // formatting floating point values is now impure
{
import std.conv : to;
Expand All @@ -2661,6 +2669,8 @@ if (is(Unqual!T : ireal) && !is(T == enum) && !hasToString!(T, Char))
}
}

version(TestComplex)
deprecated
@system unittest
{
formatTest( 2.25i, "2.25i" );
Expand Down Expand Up @@ -5494,20 +5504,22 @@ private bool needToSwapEndianess(Char)(const ref FormatSpec!Char f)

s = format("%d %s", 0x1234AF, 0xAFAFAFAF);
assert(s == "1193135 2947526575");
}

//version(X86_64)
//{
// pragma(msg, "several format tests disabled on x86_64 due to bug 5625");
//}
//else
//{
s = format("%s", 1.2 + 3.4i);
version(TestComplex)
deprecated
@system unittest
{
string s = format("%s", 1.2 + 3.4i);
assert(s == "1.2+3.4i", s);
}

//s = format("%x %X", 1.32, 6.78f);
//assert(s == "3ff51eb851eb851f 40D8F5C3");
@system unittest
{
import std.conv : octal;

//}
string s;
int i;

s = format("%#06.*f",2,12.345);
assert(s == "012.35");
Expand Down
Loading

0 comments on commit 976cb99

Please sign in to comment.