From c53e1f6e5659d1e52e2a0bcc1b9bf2a30410f29f Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Sat, 19 Aug 2017 15:57:05 +0200 Subject: [PATCH] Add std.complex.coshisinh and remove uses of creal math. --- std/complex.d | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/std/complex.d b/std/complex.d index 77638229220..aaddacca204 100644 --- a/std/complex.d +++ b/std/complex.d @@ -822,7 +822,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); @@ -840,7 +839,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); @@ -886,6 +884,39 @@ Complex!real expi(real y) @trusted pure nothrow @nogc } +/** + 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 +{ + 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. Returns: The square root of `z`.