Something to consider in one's rare spare moments:
Currently, Chapel has various symbolic constants which largely map to symbolic constants defined in the C standard. One can argue they unnecessarily pollute the name space of the
Math module.
Rather than using Chapel's existing compile time constant
sqrt2
the expression
sqrt(2.0)
would be a far clearer way to reference that square root. But as we do not want any run-time overhead in this expression, we need to define a param variant of the standard sqrt() routine as in:
proc sqrt(param x : real(?w)) param where x == 2
{
param A002193 = 1.4142135623730950488016887242096980785696718753;
return A002193:real(w);
}
Such a zero run-time overhead routine could be called as
sqrt(2.0:real(32))
sqrt(2.0:real(64))
sqrt(2.0);
to provide the desired result, the first to 32-bits of precision, the second to 64-bits of precision, and the last, because 2.0 has the default type real(64), also to 64-bits of precision.
That A002193 is that sequence within the On-Line Encyclopedia of Integer Sequences that consists of the digits in the decimal expansion of the square root of 2.
One could exploit the long standardized and well accepted name of the inverse square root function as defined by the C (and C++) standards, i.e. rsqrt(), to provide the reciprocal of 2 as
proc rsqrt(param x : real(?w)) param where x == 2
{
return 1 / sqrt(x);
}
which can then be evaluated with no run-time overhead as
rsqrt(2.0:real(32))
rsqrt(2.0:real(64))
rsqrt(2.0);
where the precision of each is left as an exercise for the reader.
This is just another example of the elegance and power of Chapel
One could also declare
proc exp(param x : real(?w)) param where x == 1
{
param A001113 = 2.71828182845904523536028747135266249775724709369995;
return A001113:T;
}
to allow a programmer zero-overhead run-time access to Napier's constant (or Euler's number or e, the 32-bit value of which would be
exp(1:real(32))
and provide future justification to drop e from the Math.chpl namespace.
But that is a whole new ball game that I leave for others to philosophically ponder