Bug 6586 – feqrel for const values too

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-08-31T22:09:00Z
Last change time
2017-07-19T17:42:14Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-08-31T22:09:28Z
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;
Comment #1 by clugdbug — 2011-09-01T12:06:31Z
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.
Comment #2 by bearophile_hugs — 2011-09-02T03:18:39Z
(In reply to comment #1) > 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 :-)
Comment #3 by hsteoh — 2014-04-08T13:23:49Z
Tested on git HEAD: the original code now compiles, although I did find a different related failing case: ======Code:====== import std.math, std.stdio; void main() { const(double) a=1.0, b=2.0; writeln(feqrel(a, b)); } ======Compile:====== /usr/src/d/phobos/std/math.d(5394): Error: cannot modify const expression diff test.d(4): Error: template instance std.math.feqrel!(const(double)) error instantiating
Comment #4 by github-bugzilla — 2015-02-27T16:30:46Z
Comment #5 by github-bugzilla — 2015-02-27T20:55:47Z
Commit pushed to 2.067 at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/cf57c7529a63ac970167c65cb659d5249a3a1ab9 Merge pull request #3017 from 9il/frexp [2.067.0-b2][regression] fix Issues 14212 and 6586
Comment #6 by github-bugzilla — 2015-04-11T12:24:50Z
Comment #7 by github-bugzilla — 2015-06-17T21:03:08Z
Comment #8 by github-bugzilla — 2017-07-19T17:42:14Z