/* REQUIRED_ARGS: -d TEST_OUTPUT: --- --- */ import core.stdc.math : isnan, signbit; import core.stdc.stdio; template AliasSeq(T...) { alias T AliasSeq; } /************************************/ static assert(-(6i) == -6i); static assert(-(1 + 6i) == -1 - 6i); static assert(!3.7i == 0); static assert(!0.0i == 1); static assert(!(2+3.7i) == 0); static assert(!(0+3.7i) == 0); static assert(!(2+0.0i) == 0); static assert(!(0+0.0i) == 1); static assert(-6i + 2i == -4i); static assert(6i - 1i == 5i); static assert((3.6 + 7.2i) / (1 + 0i) == 3.6 + 7.2i); static assert((3.6 + 7.2i) / (0.0 + 1i) == 7.2 - 3.6i); static assert((7.2i < 6.2i) == 0); static assert((7.2i == 6.2i) == 0); static assert((7.2i != 6.2i) == 1); static assert((7.2i == 7.2i) == 1); static assert((7.2i != 7.2i) == 0); static assert((5.1 is 5.1i) == 0); static assert((5.1 !is 5.1i) == 1); /***************************************/ ireal f2() { return 1i; } void test2() { creal v = 0+0i; v += f2(); assert(v == 0 + 1i); v = v + f2(); assert(v == 0 + 2i); } /***************************************/ cdouble[1] a3; cdouble[1] b3; cdouble[] concat3() { return a3~b3; } void test3() { a3[]=0.5+1.0i; b3[]=0.5+3.0i; cdouble[] arr=concat3(); assert(arr.length==2); assert(arr[0]==0.5+1.0i); assert(arr[1]==0.5+3.0i); } /***************************************/ creal[1] a4; creal[1] b4; creal[] concat4() { return a4~b4; } void test4() { a4[]=0.5+1.0i; b4[]=0.5+3.0i; creal[] arr=concat4(); assert(arr.length==2); assert(arr[0]==0.5+1.0i); assert(arr[1]==0.5+3.0i); } /***************************************/ void test5() { ifloat i=1.0fi; // i += 2.2; // assert(i == 1i); } /***************************************/ void test6() { float i=1.0f; // i /= 2.2fi; // assert(i == 0); } /***************************************/ void test7() { creal x=1.0i+2.0; creal[] arr; arr = arr ~ x; assert(arr.length==1); assert(arr[0]==1.0i+2.0); x=0.0i+5.0; assert(arr[0]==1.0i+2.0); } /****************************************/ creal[1] a8; creal[1] b8; creal[] concat8() { return a8 ~ b8; } void test8() { a8[]=0.5L+1.0Li; b8[]=0.5L+3.0Li; creal[] arr=concat8(); assert(arr.length==2); assert(arr[0]==0.5L+1.0Li); assert(arr[1]==0.5L+3.0Li); } /***************************************/ creal[1] a9; creal[1] b9; creal[] concat9() { return a9~b9; } void test9() { a9[]=0.5L+1.0Li; b9[]=0.5L+3.0Li; creal[] arr=concat9(); assert(arr.length==2); assert(arr[0]==0.5L+1.0Li); assert(arr[1]==0.5L+3.0Li); } /***************************************/ void test10() { ifloat a = 1.0i; assert(a.im == 1.0); const ifloat b = 2.0i; static assert(b.im == 2.0); // FAIL } /***************************************/ void test11() { real r = real.nan; assert( r!=0 ); if (r==0) assert(0); ireal ir = ireal.nan; assert( ir!=0 ); assert( ir!=0i ); if (ir==0) assert(0); if (ir==0i) assert(0); creal cr = creal.nan; assert( cr!=0 ); assert( cr!=0i ); if (cr==0) assert(0); if (cr==0i) assert(0); double d = double.nan; assert( d!=0 ); if (d==0) assert(0); idouble id = idouble.nan; assert( id!=0 ); assert( id!=0i ); if (id==0) assert(0); if (id==0i) assert(0); cdouble cd = cdouble.nan; assert( cd!=0 ); assert( cd!=0i ); if (cd==0) assert(0); if (cd==0i) assert(0); float f = float.nan; assert( f!=0 ); if (f==0) assert(0); ifloat ifx = ifloat.nan; assert( ifx!=0 ); assert( ifx!=0i ); if (ifx==0) assert(0); if (ifx==0i) assert(0); cfloat cf = cfloat.nan; assert( cf!=0 ); assert( cf!=0i ); if (cf==0) assert(0); if (cf==0i) assert(0); } /***************************************/ void test12() { real x = 3; creal a = (2 + 4i) % 3; printf("%Lg %Lgi\n", a.re, a.im); assert(a == 2 + 1i); creal b = (2 + 4i) % x; printf("%Lg %Lgi\n", b.re, b.im); assert(b == a); } /***************************************/ void test13() { ireal a = 5i; ireal b = a % 2; printf("%Lg %Lgi\n", b.re, b.im); assert(b == 1i); } /***************************************/ cdouble inv( cdouble expr ) { return (1.0 + 0.0i) / expr; } /***************************************/ void test14() { cfloat c; cfloat d; assert(c != d); cdouble e; cdouble f; assert(e != f); creal g; creal h; assert(g != h); } /***************************************/ void test7581() { cfloat a() { return cfloat.nan; } assert(a() != 0); } /***************************************/ float f() { return 1.0f; } ifloat i() { return 1.0fi; } void test7594() { assert(f() + i() == 1.0f + 1.0fi); } /***************************************/ cdouble conv(cfloat a) { return a; } void test7593() { assert(conv(1.0f+1.0fi) == 1.0+1.0i); } /***************************************/ cfloat get() { return cfloat.nan; } void test7591() { assert(!(get() == 0)); } /***************************************/ void foo8966(cfloat x) { assert(x.re == 3.0f); } __gshared cfloat[] a8966; void test8966() { a8966 = new cfloat[2]; a8966[0] = 3.0f + 1.0fi; foo8966(a8966[0]); } /***************************************/ void formatTest2(cfloat s, double re, double im) { assert(s.re == re); assert(s.im == im); } cfloat getcf() { return 2 + 1i; } void test10677() { formatTest2( getcf(), 2, 1 ); } /***************************************/ void test7806() { for (idouble i = -2i; i <= 2i; i += .125i) for (double r = -2; r <= 2; r += .0625) { cdouble c = r + i; printf("%g %gi\n", c.re, c.im); } } /***************************************/ void test7976() { creal[] a = new creal[2]; auto b = a[0] = a[1]; } /***************************************/ cfloat foo15f(ifloat re, float im) { return re + im; } cfloat bar15f(float re, ifloat im) { return re + im; } cdouble foo15(idouble re, double im) { return re + im; } cdouble bar15(double re, idouble im) { return re + im; } creal foo15r(ireal re, real im) { return re + im; } creal bar15r(real re, ireal im) { return re + im; } void test15() { assert(foo15f(1.0fi, 2.0f) == 2.0f + 1.0fi); assert(bar15f(1.0f, 2.0fi) == 1.0f + 2.0fi); assert(foo15(1.0i, 2.0) == 2.0 + 1.0i); assert(bar15(1.0, 2.0i) == 1.0 + 2.0i); assert(foo15r(1.0Li, 2.0L) == 2.0L + 1.0Li); assert(bar15r(1.0L, 2.0Li) == 1.0L + 2.0Li); } /************************************/ void test16() { real n = -0.0; const real m = -0.0; creal c = -0.0 + 3i; creal d = n + 3i; creal e = m + 3i; assert(signbit(c.re) != 0); assert(signbit(d.re) != 0); assert(signbit(e.re) != 0); } /************************************/ void test17() { void test(cdouble v) { auto x2 = cdouble(v); assert(x2 == v); } test(1.2+3.4i); } /************************************/ template factorial18(float n, cdouble c, string sss, string ttt) { static if (n == 1) const float factorial18 = 1; else const float factorial18 = n * 2; } template bar18(wstring abc, dstring def) { const int x = 3; } void test18() { float f = factorial18!(4.25, 6.8+3i, "hello", null); printf("%g\n", f); assert(f == 8.5); int i = bar18!("abc"w, "def"d).x; printf("%d\n", i); assert(i == 3); } /*****************************************/ void test19() { float f; double d; real r; if (f > ifloat.max) goto Loverflow; if (d > ifloat.max) goto Loverflow; if (r > ifloat.max) goto Loverflow; if (ifloat.max < f) goto Loverflow; if (ifloat.max < d) goto Loverflow; if (ifloat.max < r) goto Loverflow; return; Loverflow: return; } /*****************************************/ void test20() { double d = 1; cdouble cd = 1+0i; assert(cd == 1.0 + 0i); } /*****************************************/ void test21() { cdouble[] a; cdouble[] b; foreach(ref cdouble d; b) { d = -a[0]; for(;;){} } } /*************************************/ void test22() { static creal[] params = [1+0i, 3+0i, 5+0i]; printf("params[0] = %Lf + %Lfi\n", params[0].re, params[0].im); printf("params[1] = %Lf + %Lfi\n", params[1].re, params[1].im); printf("params[2] = %Lf + %Lfi\n", params[2].re, params[2].im); creal[] sums = new creal[3]; sums[] = 0+0i; foreach(creal d; params) { creal prod = d; printf("prod = %Lf + %Lfi\n", prod.re, prod.im); for(int i; i<2; i++) { sums[i] += prod; prod *= d; } sums[2] += prod; } printf("sums[0] = %Lf + %Lfi", sums[0].re, sums[0].im); assert(sums[0].re==9); assert(sums[0].im==0); assert(sums[1].re==35); assert(sums[1].im==0); assert(sums[2].re==153); assert(sums[2].im==0); } /*******************************************/ cdouble y23; cdouble f23(cdouble x) { return (y23 = x); } void test23() { f23(1.0+2.0i); assert(y23 == 1.0+2.0i); } /*************************************/ ifloat func_24_1(ifloat f, double d) { // f /= cast(cdouble)d; return f; } ifloat func_24_2(ifloat f, double d) { f = cast(ifloat)(f / cast(cdouble)d); return f; } float func_24_3(float f, double d) { // f /= cast(cdouble)d; return f; } float func_24_4(float f, double d) { f = cast(float)(f / cast(cdouble)d); return f; } void test24() { ifloat f = func_24_1(10i, 8); printf("%fi\n", f); // assert(f == 1.25i); f = func_24_2(10i, 8); printf("%fi\n", f); assert(f == 1.25i); float g = func_24_3(10, 8); printf("%f\n", g); // assert(g == 1.25); g = func_24_4(10, 8); printf("%f\n", g); assert(g == 1.25); } /*******************************************/ void test25() { ireal x = 4.0Li; ireal y = 4.0Li; ireal z = 4Li; creal c = 4L + 0Li; } /*************************************/ string toString26(cdouble z) { char[ulong.sizeof*8] buf; auto len = snprintf(buf.ptr, buf.sizeof, "%f+%fi", z.re, z.im); return buf[0 .. len].idup; } void test26() { static cdouble[] A = [1+0i, 0+1i, 1+1i]; string s; foreach( cdouble z; A ) { s = toString26(z); printf("%.*s ", cast(int)s.length, s.ptr); } printf("\n"); for(int ii=0; ii