digitalmars.D.learn - Why 1f.iota(100f).array returns double[] not float[]?
- drug (6/6) Sep 08 2015 import std.array : array;
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (16/22) Sep 08 2015 It is probably because the type of floating point literals like 1.0 is
- Dominikus Dittes Scherkl (11/17) Sep 08 2015 Such kind of stuff would better be written as
- Meta (7/26) Sep 08 2015 Even better, use D's universal construction feature.
- Steven Schveighoffer (4/26) Sep 08 2015 I think this warrants a bug report, iota with only floats as parameters
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/5) Sep 08 2015 https://issues.dlang.org/show_bug.cgi?id=15033
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (2/4) Sep 09 2015 https://github.com/D-Programming-Language/phobos/pull/3641
import std.array : array; import std.range : iota; pragma(msg, typeof(iota(1f, 100f).array)); // why double[] not float[]? void main() { }
Sep 08 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
Sep 08 2015
On Tuesday, 8 September 2015 at 07:17:01 UTC, Ali Çehreli wrote: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); }Such kind of stuff would better be written as auto iota(B, E)(B begin, E end) { return iota(begin, end, cast(CommonType!(B, E))1.0); } this doesn't need a constraint anymore, or maybe use if(isNumeric!(CommonType!(B, E))) I tend to use the above kind of cast often, because I like to work with ubyte or ushort, and every f***ing number literal changes the type to uint :-/
Sep 08 2015
On Tuesday, 8 September 2015 at 12:28:07 UTC, Dominikus Dittes Scherkl wrote:On Tuesday, 8 September 2015 at 07:17:01 UTC, Ali Çehreli wrote:Even better, use D's universal construction feature. auto iota(B, E)(B begin, E end) { return iota(begin, end, CommonType!(B, E)(1.0)); }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); }Such kind of stuff would better be written as auto iota(B, E)(B begin, E end) { return iota(begin, end, cast(CommonType!(B, E))1.0); } this doesn't need a constraint anymore, or maybe use if(isNumeric!(CommonType!(B, E))) I tend to use the above kind of cast often, because I like to work with ubyte or ushort, and every f***ing number literal changes the type to uint :-/
Sep 08 2015
On 9/8/15 3:17 AM, Ali Çehreli wrote:On 09/08/2015 12:00 AM, drug wrote:I think this warrants a bug report, iota with only floats as parameters should result in floats. -Steveimport 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.
Sep 08 2015
On 09/08/2015 06:37 AM, Steven Schveighoffer wrote:I think this warrants a bug report, iota with only floats as parameters should result in floats.https://issues.dlang.org/show_bug.cgi?id=15033 Ali
Sep 08 2015
On Wednesday, 9 September 2015 at 06:37:17 UTC, Ali Çehreli wrote:https://issues.dlang.org/show_bug.cgi?id=15033 Alihttps://github.com/D-Programming-Language/phobos/pull/3641
Sep 09 2015