Phobos colour module?

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Jan 6 10:07:01 PST 2015


On Tue, Jan 06, 2015 at 09:52:57AM -0800, Andrei Alexandrescu via Digitalmars-d wrote:
> On 1/6/15 6:28 AM, Manu via Digitalmars-d wrote:
> >I can't create look-up-tables at the moment, since '^^' doesn't work
> >in CTFE! >_<
> 
> Assuming the exponent is integral, could you write a naive pow()
> function that works in ctfe? -- Andrei

I thought std.math already has an overload for pow() that works with
integral exponents?

In any case, a naïve CTFE implementation would be as simple as:

	T pow(T, U)(T t, U exp)
		if (isIntegral!U)
	{
		T result = 1;
		foreach (_; 0 .. abs(exp))
			result *= t;
		return (exp < 0) ? 1 / result : result;
	}

It's the non-integral exponents that require the currently-non-CTFE-able
code, right?

In any case, pow() itself isn't the cause of non-CTFE-ability, since
Iain has so kindly provided "native" D implementations of pow() (and
most (all?) other standard math functions) in std.math; it's just that
floating-point primitives like isInfinite currently can't be CTFE'd
because they require access to the bit representation of the floats
which CTFE currently doesn't allow.

Implementing union painting in CTFE would singlehandedly solve (almost?)
all of std.math CTFE issues, AFAICT.


T

-- 
Almost all proofs have bugs, but almost all theorems are true. -- Paul Pedersen


More information about the Digitalmars-d mailing list