如何解决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
};
我在编译器中得到以下内容:
我不确定为什么AB会得到2.00,而不是2.83。如果有人可以解释我哪里出了错,我会很高兴的:)
解决方法
函数distance返回一个整数,因此它会截断sqrt的结果。您应该更改以下声明:
int distance(int x1,int y1,int x2,int y2)
到
double distance(int x1,int y2)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。