www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6531] New: assertion failure in std.range.iota

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531

           Summary: assertion failure in std.range.iota
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com



07:49:29 PDT ---
import std.range;
import std.stdio;

void main()
{
   auto r1 = iota(0.0, 4.0, 0.03);  // ok
   auto r2 = iota(0.0, 3.0, 0.03);  //
core.exception.AssertError std.range(4001): Assertion failure
}

I want a range in steps of 0.03, beginning at 0.0 and ending at the
closest point to 3.0.

Line 4001 is:
assert(start + count * step >= end);

There's no documentation, and no custom assert message, so I don't know what
was the intention of the author.

It doesn't help that this fails in debug mode but works fine in release mode.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 19 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531


Yao Gomez <yao.gomez gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yao.gomez gmail.com



With DMD 2.058HEAD the assertion is now at line 4136.

Doing a little bit of debugging, it seems that the line at 4135:
---
if (pastEnd < end) ++count;
---
is the culprit. With the r1 range, the count variable is correctly updated, as
both pastEnd and end are different.

But with the failling range (r2), pastEnd and end have the same value!, and
thus the count is not correctly updated.

Changing the previous code to:
---
if (pastEnd <= end) ++count;
---
seems to do the trick, and passes all the std.range unit tests. But I don't
know if that's the correct fix.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531




I have a patch with this solution, but I haven't pulled it yet, because I need
more input in whether this is the right fix or not.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531


dawg dawgfoto.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dawg dawgfoto.de
         Depends on|                            |7455



The correct fix is to narrow the lhs of the comparison.
assert(cast(Value)(start + count * step) >= end)

It is a common source of bugs that the intermediate 80-bit result is used for
temporaries.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531




10:34:42 PDT ---

 The correct fix is to narrow the lhs of the comparison.
 assert(cast(Value)(start + count * step) >= end)
That doesn't fix the issue. Yao Gomez'es solution does, but I don't know if it's a proper solution. Yao, you could try making a pull request and see if you get more comments on github. Issue 9877 might be a duplicate too. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 05 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531




 if (pastEnd <= end) ++count;
This is definitely wrong. There are only 100 steps in [0, 0.03 ..., 3.0). The problem was that assert(0.0 + 100 * 0.03 >= 3.0) fails because of floating-point excess precision. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531


drug007 <drug2004 bk.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |drug2004 bk.ru



I propose the following:

    auto pastEnd = start + count * step;
    if (step > 0)
    {
        if (pastEnd < end) {
            ++count;
            pastEnd = start + count * step;
        }
        assert(pastEnd >= end);
    }
    else
    {
        if (pastEnd > end) {
            ++count;
            pastEnd = start + count * step;
        }
        assert(pastEnd <= end);
    }

Martin Nowak's fix is the better (simpler and more clear) but I just don't like
cast.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531




08:13:26 PDT ---

 Martin Nowak's fix is the better (simpler and more clear) but I just don't like
 cast.
I've tried his fix and it didn't work for me. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531






 Martin Nowak's fix is the better (simpler and more clear) but I just don't like
 cast.
I've tried his fix and it didn't work for me.
Try mine. It works for me (with your example). -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531


safety0ff.bugz gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |safety0ff.bugz gmail.com



Indeed, casting does not seem sufficient to force correct rounding of
intermediate results.
Seems like the solution is to either assign and force rounding, or use
approxEqual with appropriate constants for the error terms.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |acehreli yahoo.com



*** Issue 9877 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531




 Indeed, casting does not seem sufficient to force correct rounding of
 intermediate results.
 Seems like the solution is to either assign and force rounding, or use
 approxEqual with appropriate constants for the error terms.
Yes, casting is optimized away by dmd. Walter suggested to use an opaque function or inline asm to enforce rounding to lower precision. It seems like C99 addresses this by specifying that casts and assignments need to be rounded to lower precision. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 30 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6531


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull



https://github.com/D-Programming-Language/phobos/pull/1673

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