www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiler gets confused with ambiguity when `int` matches both `real`

reply IM <3di gm.com> writes:
The following expression:

import std.math : sqrt;
sqrt(400);

produces the following compiler error:

std.math.sqrt called with argument types (int) matches both:
/usr/include/dmd/phobos/std/math.d(1592,7):     
std.math.sqrt(float x)
and:
/usr/include/dmd/phobos/std/math.d(1598,6):     
std.math.sqrt(real x)

Shouldn't it just pick one according to some defined rules?
Dec 22 2017
parent Muld <2 2.2> writes:
On Saturday, 23 December 2017 at 07:25:34 UTC, IM wrote:
 The following expression:

 import std.math : sqrt;
 sqrt(400);

 produces the following compiler error:

 std.math.sqrt called with argument types (int) matches both:
 /usr/include/dmd/phobos/std/math.d(1592,7):     
 std.math.sqrt(float x)
 and:
 /usr/include/dmd/phobos/std/math.d(1598,6):     
 std.math.sqrt(real x)

 Shouldn't it just pick one according to some defined rules?
C++ complains the same between float and double. Just put 400.0f or whatever precision you want to use. I think it's better that it doesn't just pick one.
Dec 23 2017