iota to array
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun Feb 25 06:35:07 UTC 2018
On Sunday, February 25, 2018 06:22:03 psychoticRabbit via Digitalmars-d-
learn wrote:
> On Sunday, 25 February 2018 at 05:40:19 UTC, Jonathan M Davis
>
> wrote:
> > int[] intArr = iota(1, 11).array();
> >
> > - Jonathan M Davis
>
> thanks!
>
> oh man. It's so easy to do stuff in D ;-)
>
> But this leads me to a new problem now.
>
> When I run my code below, I get ints printed instead of doubles??
>
> ---------------------
> module test;
>
> import std.stdio : writeln;
> import std.traits : isArray;
> import std.array : array;
> import std.range : iota;
>
>
> void main()
> {
> int[] intArr = iota(1, 11).array(); // 1..10
> double[] doubleArr = iota(1.0, 11.0).array(); // 1.0..10.0
> char[] charArr = iota('a', '{').array(); // a..z
>
> printArray(intArr);
> printArray(doubleArr); // why is it printing ints instead of
> doubles??
> printArray(charArr);
> }
>
> void printArray(T)(const ref T[] a) if (isArray!(T[]))
> {
> foreach(t; a)
> writeln(t);
> }
>
> ---------------------------------
It's not printing ints. It's printing doubles. It's just that all of the
doubles have nothing to the right of the decimal point, so they don't get
printed with a decimal point. If you did something like start with 1.1, then
you'd see decimal points, because there would be data to the right of the
decimal point. The same thing happens if you do
writeln(1.0);
as opposed to something like
writeln(1.3);
BTW, you can just call writeln on the array directly, and then you'll get
something like
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list