LoginSignup
2
0

More than 1 year has passed since last update.

D言語 UFCS表記のcomplexの初期化

Last updated at Posted at 2021-06-12

D言語 虚数、複素数型が非推奨 dmd 2.097.0 より

std.complex ライブラリを利用する

ライブラリへの変更で使用できなくなるのが、虚数、複素数リテラル

type_i.d
ifloat im = 0.123i;
cfloat c = 1.23 + 4.56i;

これを何とかする。
何とかしました。https://github.com/deepprog/im4ufcs
関数i で complexを初期化します。

test_i.d
import std.complex;
import im4ufcs;

//虚数は複素数の実数をゼロとして初期化
auto im = 1.2345.i;
assert(im == complex(0.0, 1.2345));

//+は通常の演算子   
auto c = 1.23 + 4.56.i;
assert(c == complex(1.23, 4.56));

auto x = 0.123;
auto y = 0.456;
//もちろん変数でも可
auto z = x - y.i;
assert(z == complex(0.123, -0.456));

im4ufcs.d
module im4ufcs;
import std.complex;

auto i(T)(T im) @safe pure nothrow @nogc
{
    return complex(0.0, im);
}

虚数リテラルをufcs関数で模倣してます。
複素数はcomplexライブラリでうまいことなっています。

同じことを誰かに発見される前に記事にしておきます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0