C中的sqrt函数未返回确切值

问题描述

我的代码如下:

class Base {
public:
    virtual void foo(int a,double b,char c) = 0;
};

class Bar : public Base {
public:
    virtual void foo(int a,char c) override { } // uses all three
};

class Baz : public Base {
public:
    virtual void foo(int a,char c = '') override {} // uses only the int and double
};

class Biz : public Base {
public:
    virtual void foo(int a,double = 0.0,char c = '') override{} // uses only the int
};

我在编译器中得到以下内容

enter image description here

我不确定为什么AB会得到2.00,而不是2.83。如果有人可以解释我哪里出了错,我会很高兴的:)

解决方法

函数distance返回一个整数,因此它会截断sqrt的结果。您应该更改以下声明:

 int distance(int x1,int y1,int x2,int y2)

double distance(int x1,int y2)