Wrote a blog post about CTFE and D

Philippe Sigaud philippe.sigaud at gmail.com
Thu Aug 30 10:39:56 PDT 2012


On Thu, Aug 30, 2012 at 7:10 PM, Danny Arends <Danny.Arends at gmail.com> wrote:

>>> I wrote a blog post about the stuff I've been doing last weekend using
>>> CTFE.
>>> All comments are welcome, you can find the blog post at:
>>>
>>> http://www.dannyarends.nl/index.cgi?viewDetailed=00029

Nice article, Danny!

A few remarks:

degToRad!(float,int) 45

First, it seems like you missed a parenthesis pair?

The compiler will be able to determine V in degToRad, you can call it like this:

degToRad!(float)(45)

Following bearophile's use of isFloatingPoint, you can use a default
value, if that's what you need most of the time:

import std.traits;
pure U degToRad(U = float, V)(in V deg) if (isFloatingPoint!U && isIntegral!V)
{ return (deg * PI) / 180.0; }

Then, to call it:

degToRad(45) => automatically expand to detToRad!(float,int)(45)

And the same type deduction for cordic gives you

cordic( degToRad(45) );

instead of

cordic!(float)(degToRad!(float,int) 45);

In gen_trigonometric, I think the float call should be a T:

result ~= cordic!T(degToRad!(float,int)(i), iter);

=>

result ~= cordic( degToRad!(T)(i), iter);

And, since you know the result's size in advance, you might want to
generate it at once:

T[2][] result = new (T[2][])(iter);
foreach(i; 0 .. 360)
    result[i] = cordic(degToRad!(T)(i), iter);
return result;

(no need for braces for a one-expression foreach)

Or even, using map:

import std.algorithm, std.array;

return map!( i => cordic(degToRag!(T)(i), iter) )(result).array;


More information about the Digitalmars-d-announce mailing list