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