Thread overview
Why approxEqual not working for integers in dmd 2068-b2
Jul 28, 2015
lobo
Jul 28, 2015
Daniel Kozák
Jul 28, 2015
Daniel Kozák
Jul 28, 2015
Daniel Kozák
Jul 28, 2015
lobo
July 28, 2015
Hi all,

I have a bunch of unittests for template code taking any numeric type. Because I'm lazy I just use the approxEqual for both floating point and integer comparisons in these tests.

In DMD 2067.1 everthing compiled OK but in 2068-b2 I get the errors shown at the end of this post for integer types.


I'd like to know if it is OK to use approxEqual like I am in unittests and why approxEqual was changed, if it was intentional, for learning more about good use of D language


Thanks,
lobo



Test code:
---
void main() {

    int a = 10;
    assert(approxEqual(10, a));

}
---
Errors:

src/phobos/std/math.d(6718): Error: std.math.fabs called with argument types (int) matches both:
src/phobos/std/math.d(3415):     std.math.fabs(real x)
and:
/src/phobos/std/math.d(3421):     std.math.fabs(float x)
/src/phobos/std/math.d(6725): Error: std.math.fabs called with argument types (int) matches both:
/src/phobos/std/math.d(3415):     std.math.fabs(real x)
and:
/src/phobos/std/math.d(3421):     std.math.fabs(float x)
/src/phobos/std/math.d(6726): Error: std.math.fabs called with argument types (int) matches both:
/src/phobos/std/math.d(3415):     std.math.fabs(real x)
and:
/src/phobos/std/math.d(3421):     std.math.fabs(float x)
/src/phobos/std/math.d(6736): Error: template instance std.math.approxEqual!(int, int, double) error instantiating
hack.d(13):        instantiated from here: approxEqual!(int, int)
Failed: ["dmd", "-v", "-o-", "hack.d", "-I."]
---
July 28, 2015
On Tue, 28 Jul 2015 02:16:56 +0000
"lobo" <swamplobo@gmail.com> wrote:

> Hi all,
> 
> I have a bunch of unittests for template code taking any numeric type. Because I'm lazy I just use the approxEqual for both floating point and integer comparisons in these tests.
> 
> In DMD 2067.1 everthing compiled OK but in 2068-b2 I get the errors shown at the end of this post for integer types.
> 
> 
> I'd like to know if it is OK to use approxEqual like I am in unittests and why approxEqual was changed, if it was intentional, for learning more about good use of D language
> 
> 
> Thanks,
> lobo
> 
> 
> 
> Test code:
> ---
> void main() {
> 
>      int a = 10;
>      assert(approxEqual(10, a));
> 
> }
> ---
> Errors:
> 
> src/phobos/std/math.d(6718): Error: std.math.fabs called with
> argument types (int) matches both:
> src/phobos/std/math.d(3415):     std.math.fabs(real x)
> and:
> /src/phobos/std/math.d(3421):     std.math.fabs(float x)
> /src/phobos/std/math.d(6725): Error: std.math.fabs called with
> argument types (int) matches both:
> /src/phobos/std/math.d(3415):     std.math.fabs(real x)
> and:
> /src/phobos/std/math.d(3421):     std.math.fabs(float x)
> /src/phobos/std/math.d(6726): Error: std.math.fabs called with
> argument types (int) matches both:
> /src/phobos/std/math.d(3415):     std.math.fabs(real x)
> and:
> /src/phobos/std/math.d(3421):     std.math.fabs(float x)
> /src/phobos/std/math.d(6736): Error: template instance
> std.math.approxEqual!(int, int, double) error instantiating
> hack.d(13):        instantiated from here: approxEqual!(int, int)
> Failed: ["dmd", "-v", "-o-", "hack.d", "-I."]
> ---

I would say it is a compiler bug.

consider this:

bool some(real x, real y) {
	return true;
}

bool some(float x, float y) {
	return true;
}

void main() {
	some(4.0L, 4.0L); // ok
	some(4L,4L); // this should implicit convert to real,real
}

but:

//m.d(11): Error: m.some called with argument types (long, long) matches
both: //m.d(1):     m.some(real x, real y) //and: //m.d(5):
m.some(float x, float y

It is in confrontance with TDPL p.44 figure 2.3
July 28, 2015
On Tue, 28 Jul 2015 08:50:53 +0200
Daniel Kozák <kozzi@dlang.cz> wrote:

> 
> On Tue, 28 Jul 2015 02:16:56 +0000
> "lobo" <swamplobo@gmail.com> wrote:
> 
> I would say it is a compiler bug.
> 
> consider this:
> 
> bool some(real x, real y) {
> 	return true;
> }
> 
> bool some(float x, float y) {
> 	return true;
> }
> 
> void main() {
> 	some(4.0L, 4.0L); // ok
> 	some(4L,4L); // this should implicit convert to real,real
> }
> 
> but:
> 
> //m.d(11): Error: m.some called with argument types (long, long)
> matches both: //m.d(1):     m.some(real x, real y) //and: //m.d(5):
> m.some(float x, float y
> 
> It is in confrontance with TDPL p.44 figure 2.3

OTOH D has same behaviour as C++

July 28, 2015
On Tue, 28 Jul 2015 02:16:56 +0000
"lobo" <swamplobo@gmail.com> wrote:

> Hi all,
> 
> I have a bunch of unittests for template code taking any numeric type. Because I'm lazy I just use the approxEqual for both floating point and integer comparisons in these tests.
> 
> In DMD 2067.1 everthing compiled OK but in 2068-b2 I get the errors shown at the end of this post for integer types.
> 
> 
> I'd like to know if it is OK to use approxEqual like I am in unittests and why approxEqual was changed, if it was intentional, for learning more about good use of D language
> 
> 
> Thanks,
> lobo
> 
> 
> 
> Test code:
> ---
> void main() {
> 
>      int a = 10;
>      assert(approxEqual(10, a));
> 
> }
> ---
> Errors:
> 
> src/phobos/std/math.d(6718): Error: std.math.fabs called with
> argument types (int) matches both:
> src/phobos/std/math.d(3415):     std.math.fabs(real x)
> and:
> /src/phobos/std/math.d(3421):     std.math.fabs(float x)
> /src/phobos/std/math.d(6725): Error: std.math.fabs called with
> argument types (int) matches both:
> /src/phobos/std/math.d(3415):     std.math.fabs(real x)
> and:
> /src/phobos/std/math.d(3421):     std.math.fabs(float x)
> /src/phobos/std/math.d(6726): Error: std.math.fabs called with
> argument types (int) matches both:
> /src/phobos/std/math.d(3415):     std.math.fabs(real x)
> and:
> /src/phobos/std/math.d(3421):     std.math.fabs(float x)
> /src/phobos/std/math.d(6736): Error: template instance
> std.math.approxEqual!(int, int, double) error instantiating
> hack.d(13):        instantiated from here: approxEqual!(int, int)
> Failed: ["dmd", "-v", "-o-", "hack.d", "-I."]
> ---

Even if this will be considered as non compiler bug, it is a regression on phobos side and should be addressed. So please fill a bug report on http://issues.dlang.org

July 28, 2015
On Tuesday, 28 July 2015 at 07:42:32 UTC, Daniel Kozák wrote:
>
> Even if this will be considered as non compiler bug, it is a regression on phobos side and should be addressed. So please fill a bug report on http://issues.dlang.org

done,
thanks

https://issues.dlang.org/show_bug.cgi?id=14842