www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6586] New: feqrel for const values too

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

           Summary: feqrel for const values too
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



import std.math: feqrel;
void main() {
    double x = 1.5;
    const double y = 1.50000001;
    auto d = feqrel(x, y);
}


DMD 2.055head gives me:

test.d(5): Error: template std.math.feqrel(X) if (isFloatingPoint!(X)) does not
match any function template declaration
test.d(5): Error: template std.math.feqrel(X) if (isFloatingPoint!(X)) cannot
deduce template function from argument types !()(double,const(double))


I'd like feqrel to work with mixed const arguments too. Const doubles come from
"in" function arguments, and they pop out in other situations too, like:


import std.math: feqrel;
double foo(double x)
out(r) {
    assert(feqrel(r, x) > 10);
}
body {
    return x + 0.0000001;
}
void main() {
    foo(1.5);
}


Workaround:


import std.math: feqrel;
double foo(double x)
out(r) {
    assert(feqrel(cast()r, x) > 10);
}
body {
    return x + 0.0000001;
}
void main() {
    foo(1.5);
}




To solve this problem I think that it's enough to replace this:


int feqrel(X)(X x, X y)  trusted pure nothrow
    if (isFloatingPoint!(X))
{
    /* Public Domain. Author: Don Clugston, 18 Aug 2005.
     */


With something like:

int feqrel(X1, X2)(X1 x, X2 y)  trusted pure nothrow
    if (isFloatingPoint!(Unqual!X1) && is(Unqual!X1 == Unqual!X2))
{
    /* Public Domain. Author: Don Clugston, 18 Aug 2005.
     */
    alias Unqual!X1 X;

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au



I hit this myself a couple of days ago.
I prefer:

int feqrel(X)(const(X) x, const(X) y)  trusted pure nothrow
    if (isFloatingPoint!(X))

which causes less template bloat, and also avoids some unpleasant issues where
x and y are different sizes.

I'm not really sure why double and const(double) need to be different types. It
causes a huge amount of template bloat, and I don't think we're getting much
(if anything) in return.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 01 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6586






 I'm not really sure why double and const(double) need to be different types. It
 causes a huge amount of template bloat, and I don't think we're getting much
 (if anything) in return.
Tell this again in the main D newsgroup :-) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 02 2011