Thread overview
[Issue 4111] New: Foreach ranges accept floating-point extrema
Apr 22, 2010
Aelxx
Apr 22, 2010
bearophile
April 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4111

           Summary: Foreach ranges accept floating-point extrema
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-04-21 14:01:40 PDT ---
This D2 code works with dmd 2.043, it shows that foreach accepts floating point extrema too.

But I think it's safer/tidier to accept only ranges with integral extrema (in Python too the range/xrange expects integers). FP approximations can cause problems here.


import std.stdio;
import std.math: nextUp;
void main() {
    foreach(i; 2.1 .. 4.10001) {
        writeln(typeid(typeof(i))); // Output: double
        break;
    }
    foreach(i; 2.1 .. nextUp(4.1))
        write(i, " "); // Output: 2.1 3.1 4.1
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2010
>    foreach(i; 2.1 .. 4.10001) {
>        writeln(typeid(typeof(i))); // Output: double
>        break;
>    }

I'd like MATLAB style more:
foreach (f; linspace (0.0, 1.0, 100))
{...}
here f gets values 0, 0.0101.., 0.0202..., ..., 0.98989..., 1.0

foreach (f; logspace (1.0, 1e6, 7))
{...}
here f gets values 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6.


April 22, 2010
Aelxx:
> >    foreach(i; 2.1 .. 4.10001) {
> >        writeln(typeid(typeof(i))); // Output: double
> >        break;
> >    }
> 
> I'd like MATLAB style more:
> foreach (f; linspace (0.0, 1.0, 100))
> {...}
> here f gets values 0, 0.0101.., 0.0202..., ..., 0.98989..., 1.0
> 
> foreach (f; logspace (1.0, 1e6, 7))
> {...}
> here f gets values 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6.

If you have comments on a bug report, it's quite better if you write them in Bugzilla, because here they will probably be lost/ignored.
Phobos has iota() that's similar to what you ask, a FP version is easy to create.

Bye,
bearophile
April 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4111



--- Comment #1 from bearophile_hugs@eml.cc 2010-04-22 02:09:06 PDT ---
Aelxx (aelxx at yandex dot ru) suggests:

I'd like MATLAB style more:
foreach (f; linspace (0.0, 1.0, 100))
{...}
here f gets values 0, 0.0101.., 0.0202..., ..., 0.98989..., 1.0

foreach (f; logspace (1.0, 1e6, 7))
{...}
here f gets values 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6.

------------------------

My answer:

Phobos has iota() that's similar to what you ask, a FP version is easy to create. With iota even the current interval syntax becomes less useful. See also bug 4112.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------