Thread overview
Does D optimize sqrt(2.0)?
Feb 11, 2016
Enjoys Math
Feb 11, 2016
crimaniak
Feb 11, 2016
Johannes Pfau
February 11, 2016
If I just type out sqrt(2.0) in D, is that automatically made into a constant for me?

Thanks.
February 11, 2016
On Thursday, 11 February 2016 at 07:41:55 UTC, Enjoys Math wrote:
> If I just type out sqrt(2.0) in D, is that automatically made into a constant for me?
>
> Thanks.

for DMD -O :

import std.math;

immutable foo = sqrt(2.0);

pure float precalculated()
{
    return foo;
}

pure float not_precalculated()
{
    return sqrt(2.0);
}
February 11, 2016
On Thursday, 11 February 2016 at 07:41:55 UTC, Enjoys Math wrote:
> If I just type out sqrt(2.0) in D, is that automatically made into a constant for me?
>
> Thanks.

For GDC the answer is yes:
http://explore.dgnu.org/#%7B%22version%22%3A3%2C%22filterAsm%22%3A%7B%22labels%22%3Atrue%2C%22directives%22%3Atrue%2C%22commentOnly%22%3Atrue%7D%2C%22compilers%22%3A%5B%7B%22sourcez%22%3A%22IYVwLg9gBAZhEAoCUAoA3iqWoEsC2ADhAE5hQDOYAJgHR7BgAWA3JtsQKZgjEB2FAR1IIATDQAMSVgF8gAA%3D%22%2C%22compiler%22%3A%22gdc-64%22%2C%22options%22%3A%22%22%7D%5D%7D

LDC will probably optimize this as well. Not sure about DMD.