www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting a number to complex

reply "Frederik Vagner" <vnataneg.jykhan gmail.com> writes:
I am trying to make a templated class to accept any numeric type:

class example(Type) if (isNumeric(Type))
{
     Type k = to!Type(1);
     ....
}

however I always get a compiler erro stating I cannot make that 
conversion. How do I fix it?
Nov 22 2012
next sibling parent reply "Joshua Niehus" <jm.niehus gmail.com> writes:
On Thursday, 22 November 2012 at 15:47:11 UTC, Frederik Vagner 
wrote:
 I am trying to make a templated class to accept any numeric 
 type:

 class example(Type) if (isNumeric(Type))
 {
     Type k = to!Type(1);
     ....
 }

 however I always get a compiler erro stating I cannot make that 
 conversion. How do I fix it?

// phobos import std.stdio, std.conv, std.traits; class Example(T) if (isNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!double(); writeln(x.k); }
Nov 22 2012
parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 11/23/12 18:06, Joshua Niehus wrote:
 meh, couldn't resist:
 
 import std.stdio, std.conv, std.traits, std.complex;
 template isComplex(T)
 {
     static if (is(T == Complex!double))
     {
         enum bool isComplex = true;
     }
     else static if (is(T == Complex!float))
     {
         enum bool isComplex = true;
     }
     else static if (is(T == Complex!real))
     {
         enum bool isComplex = true;
     }
     else {
         enum bool isComplex = false;
     }
 }

template isComplex(T) { static if (is(T _ == Complex!CT, CT)) enum isComplex = true; else enum isComplex = false; } artur
Nov 23 2012
next sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 11/24/12 08:27, Philippe Sigaud wrote:
 What's even nicer with an is() expr is that the introspection info is
accessible when used inside a static if. So with Artur's code, CT can be seen
inside the true branch of the static if.

Actually, static-ifs do not create a scope, so 'CT' leaks and is accessible after it's defined, not just inside the static-if branch: void main() { Complex!double c; static if (is(typeof(c) _ == Complex!CT, CT)) {} writeln(CT.stringof); } It has to work like that, as otherwise it would be impossible (well, harder) to conditionally compile /declarations/. Unfortunately the is-expressions don't handle aliases properly, at least with my old compiler here. So this does not work: template isTempInst(alias TEMPL, T) { static if (is(T _ == TEMPL!CT, CT)) enum isTempInst = true; else enum isTempInst = false; } artur
Nov 24 2012
prev sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 11/24/12 12:30, Philippe Sigaud wrote:
 
     Unfortunately the is-expressions don't handle aliases properly, at least
     with my old compiler here. So this does not work:
 
        template isTempInst(alias TEMPL, T) {
           static if (is(T _ == TEMPL!CT, CT))
              enum isTempInst = true;
           else
              enum isTempInst = false;
        }
 
 
 Works for me:
 
 
 template isTempInst(alias TEMPL, T)
 {
     static if (is(T _ == TEMPL!CT, CT...))
         enum isTempInst = true;
     else
         enum isTempInst = false;
 }
    
 void main()
 {
 
     Tuple!(int,string) t = tuple(1, "abc");
     writeln(typeof(t).stringof);
     writeln(isTempInst!(Tuple, typeof(t)));
 }
 
 (DMD 2.060)

Then it's probably just my old compiler, gdc r748:ab99d67f04c2, using dmd 2.057, which thinks that that is-expression is always false. artur
Nov 24 2012
prev sibling next sibling parent "Frederik Vagner" <vnataneg.jykhan gmail.com> writes:
On Thursday, 22 November 2012 at 16:09:46 UTC, Joshua Niehus 
wrote:
 On Thursday, 22 November 2012 at 15:47:11 UTC, Frederik Vagner 
 wrote:
 I am trying to make a templated class to accept any numeric 
 type:

 class example(Type) if (isNumeric(Type))
 {
    Type k = to!Type(1);
    ....
 }

 however I always get a compiler erro stating I cannot make 
 that conversion. How do I fix it?

// phobos import std.stdio, std.conv, std.traits; class Example(T) if (isNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!double(); writeln(x.k); }

Now do it for complex number please ^^
Nov 23 2012
prev sibling next sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
On Friday, 23 November 2012 at 12:39:59 UTC, Frederik Vagner 
wrote:
 Now do it for complex number please ^^

touche! import std.stdio, std.conv, std.traits, std.complex; template isComplexNumeric(T) { static if(is(NumericTypeOf!T)) { enum bool isComplexNumeric = is(NumericTypeOf!T); } else static if (is(T == Complex!double)) { enum bool isComplexNumeric = is(Complex!double); } // fillin real and float here... (e.g. is(Complex!real); etc... } class Example(T) if (isComplexNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!(Complex!double)(); writeln(x.k); auto y = new Example!double(); writeln(y.k); auto z = new Example!string(); // fail writeln(z.k); } A bit messy, but im sure there is some room for cleanup somewhere...
Nov 23 2012
prev sibling next sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
On Friday, 23 November 2012 at 16:11:25 UTC, Joshua Niehus wrote:
 A bit messy, but im sure there is some room for cleanup 
 somewhere...

Errata... (what i get for copy/pasting) import std.stdio, std.conv, std.traits, std.complex; template isComplexNumeric(T) { static if(isNumeric!T) { enum bool isComplexNumeric = true; } else static if (is(T == Complex!double)) { enum bool isComplexNumeric = true; } else { enum bool isComplexNumeric = false; } } class Example(T) if (isComplexNumeric!T) { T k = to!T(1); } void main() { auto x = new Example!(Complex!double)(); writeln(x.k); auto y = new Example!double(); writeln(y.k); auto z = new Example!string(); writeln(z.k); } i did have to reference Philippe Sigaud's excellent book on Templates several times: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf
Nov 23 2012
prev sibling next sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
meh, couldn't resist:

import std.stdio, std.conv, std.traits, std.complex;
template isComplex(T)
{
     static if (is(T == Complex!double))
     {
         enum bool isComplex = true;
     }
     else static if (is(T == Complex!float))
     {
         enum bool isComplex = true;
     }
     else static if (is(T == Complex!real))
     {
         enum bool isComplex = true;
     }
     else {
         enum bool isComplex = false;
     }
}

template isComplexOrNumeric(T)
{
     enum bool isComplexOrNumeric = (isComplex!T || isNumeric!T);
}

class Example(T) if (isComplexOrNumeric!T)
{
     T k = to!T(1);
}
Nov 23 2012
prev sibling next sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
On Friday, 23 November 2012 at 18:45:53 UTC, Artur Skawina wrote:
    template isComplex(T) {
       static if (is(T _ == Complex!CT, CT))
          enum isComplex = true;
       else
          enum isComplex = false;
    }

 artur

oh wow didnt know u could do that. much nicer.
Nov 23 2012
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--20cf3074b298c4e40304cf38a041
Content-Type: text/plain; charset=UTF-8

Joshua:

 oh wow didnt know u could do that. much nicer.

It's an is() expression (you cited my tutorial, there is an appendix on it). It recently became even more powerful, so the tutorial is not accurate any more. A few months ago, you couldn't do is(Type _ == Template!Args, Args...) with Args being whatever number of args are needed. Only fixed numbers were possible: is(Type _ == Template!(T), T) is(Type _ == Template!(T,U), T,U) and so on... Now, testing for a Tuple is easy: is(Type _ == Tuple!Args, Args...) What's even nicer with an is() expr is that the introspection info is accessible when used inside a static if. So with Artur's code, CT can be seen inside the true branch of the static if. template ComplexType(T) if (isComplex!T) // Using the previously defined isComplex { // we know it's a Complex!CT, for some CT static if (is(T _ == Complex!CT, CT) alias CT ComplexType; else static assert(false, "This should not happen!"); } void main() { Complex!double c; writeln(ComplexType!(typeof(c)).stringof); } --20cf3074b298c4e40304cf38a041 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Joshua:<br><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" sty= le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> oh wow didnt know u could do that. much nicer.<br></blockquote><div><br></d= iv><div>It&#39;s an is() expression (you cited my tutorial, there is an app= endix on it). It recently became even more powerful, so the tutorial is not= accurate any more.</div> <div><br></div><div>A few months ago, you couldn&#39;t do=C2=A0</div><div><= br></div><div>is(Type _ =3D=3D Template!Args, Args...)</div><div><br></div>= <div>with Args being whatever number of args are needed. Only fixed numbers= were possible:</div> <div><br></div><div><div>is(Type _ =3D=3D Template!(T), T)</div></div><div>= is(Type _ =3D=3D Template!(T,U), T,U)</div><div>and so on...</div><div><br>= </div><div>Now, testing for a Tuple is easy:</div><div><br></div><div>is(Ty= pe _ =3D=3D Tuple!Args, Args...)</div> <div><br></div><div><br></div><div><br></div><div>What&#39;s even nicer wit= h an is() expr is that the introspection info is accessible when used insid= e a static if. So with Artur&#39;s code, CT can be seen inside the true bra= nch of the static if.</div> <div><br></div><div>template ComplexType(T)=C2=A0</div><div>if (isComplex!T= ) // Using the previously defined isComplex</div><div>{</div><div>=C2=A0 = =C2=A0 // we know it&#39;s a Complex!CT, for some CT</div><div>=C2=A0 =C2= =A0 static if (is(T _ =3D=3D Complex!CT, CT)</div> <div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 alias CT ComplexType;</div><div>=C2=A0 =C2= =A0 else</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 static assert(false, &quot;T= his should not happen!&quot;);</div><div>}</div><div><br></div><div>void ma= in()</div><div>{</div><div>=C2=A0 =C2=A0 Complex!double c;</div> <div>=C2=A0 =C2=A0 writeln(ComplexType!(typeof(c)).stringof);</div><div>}</= div><div><br></div></div> --20cf3074b298c4e40304cf38a041--
Nov 23 2012
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--20cf3074b298413fda04cf3c0674
Content-Type: text/plain; charset=UTF-8

On Sat, Nov 24, 2012 at 11:30 AM, Artur Skawina <art.08.09 gmail.com> wrote:

 On 11/24/12 08:27, Philippe Sigaud wrote:
 What's even nicer with an is() expr is that the introspection info is

seen inside the true branch of the static if. Actually, static-ifs do not create a scope, so 'CT' leaks and is accessible after it's defined, not just inside the static-if branch:

You're right, I didn't think it through. Time to update my tutorial, then :)
 Unfortunately the is-expressions don't handle aliases properly, at least
 with my old compiler here. So this does not work:

    template isTempInst(alias TEMPL, T) {
       static if (is(T _ == TEMPL!CT, CT))
          enum isTempInst = true;
       else
          enum isTempInst = false;
    }

Works for me: template isTempInst(alias TEMPL, T) { static if (is(T _ == TEMPL!CT, CT...)) enum isTempInst = true; else enum isTempInst = false; } void main() { Tuple!(int,string) t = tuple(1, "abc"); writeln(typeof(t).stringof); writeln(isTempInst!(Tuple, typeof(t))); } (DMD 2.060) --20cf3074b298413fda04cf3c0674 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sat, Nov 24, 2012 at 11:30 AM, Artur = Skawina <span dir=3D"ltr">&lt;<a href=3D"mailto:art.08.09 gmail.com" target= =3D"_blank">art.08.09 gmail.com</a>&gt;</span> wrote:<br><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"> <div class=3D"im">On 11/24/12 08:27, Philippe Sigaud wrote:<br> &gt; What&#39;s even nicer with an is() expr is that the introspection info= is accessible when used inside a static if. So with Artur&#39;s code, CT c= an be seen inside the true branch of the static if.<br> <br> </div>Actually, static-ifs do not create a scope, so &#39;CT&#39; leaks and= is accessible<br> after it&#39;s defined, not just inside the static-if branch:<br></blockquo= te><div><br></div><div>You&#39;re right, I didn&#39;t think it through. Tim= e to update my tutorial, then :)</div><div><br></div><div>=C2=A0</div><bloc= kquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #cc= c solid;padding-left:1ex"> Unfortunately the is-expressions don&#39;t handle aliases properly, at leas= t<br> with my old compiler here. So this does not work:<br> <br> =C2=A0 =C2=A0template isTempInst(alias TEMPL, T) {<br> =C2=A0 =C2=A0 =C2=A0 static if (is(T _ =3D=3D TEMPL!CT, CT))<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0enum isTempInst =3D true;<br> =C2=A0 =C2=A0 =C2=A0 else<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0enum isTempInst =3D false;<br> =C2=A0 =C2=A0}</blockquote><div><br></div><div>Works for me:</div><div><br>= </div><div><br></div><div>template isTempInst(alias TEMPL, T)</div><div>{</= div><div>=C2=A0 =C2=A0 static if (is(T _ =3D=3D TEMPL!CT, CT...))</div><div=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 enum isTempInst =3D true;</div>

st =3D false;</div><div>}</div><div>=C2=A0 =C2=A0</div><div>void main()</di= v><div>{</div><div><br></div><div>=C2=A0 =C2=A0 Tuple!(int,string) t =3D tu= ple(1, &quot;abc&quot;);</div><div>=C2=A0 =C2=A0 writeln(typeof(t).stringof= );</div> <div>=C2=A0 =C2=A0 writeln(isTempInst!(Tuple, typeof(t)));</div><div>}</div=
<div><br></div><div>(DMD 2.060)</div><div>=C2=A0</div></div>

--20cf3074b298413fda04cf3c0674--
Nov 24 2012
prev sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
On Saturday, 24 November 2012 at 07:27:18 UTC, Philippe Sigaud 
wrote:
 It's an is() expression (you cited my tutorial, there is an 
 appendix on
 it). It recently became even more powerful, so the tutorial is 
 not accurate
 any more.

its time for another read through:) Template constraints might make for a good talk at the conf...
Nov 25 2012