Why 1f.iota(100f).array returns double[] not float[]?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 8 00:17:01 PDT 2015


On 09/08/2015 12:00 AM, drug wrote:
> import std.array : array;
> import std.range : iota;
>
> pragma(msg, typeof(iota(1f, 100f).array)); // why double[] not float[]?
>
> void main()
> {
> }

It is probably because the type of floating point literals like 1.0 is 
double. Probably there is a 1.0 in iota's implementation, converting the 
element type to double according to rule number 2 here:

   http://dlang.org/type.html#usual-arithmetic-conversions

Yep, here it is:

 
https://github.com/D-Programming-Language/phobos/blob/master/std/range/package.d#L4630

auto iota(B, E)(B begin, E end)
if (isFloatingPoint!(CommonType!(B, E)))
{
     return iota(begin, end, 1.0);
}

Although any such expression can become double easily, I think the 
literal should be 1.0f in this case.

Ali



More information about the Digitalmars-d-learn mailing list