d*********1 发帖数: 25 | 1 what are the results of the folloing output?
it has been tested: 1st--> c; 2nd --> c++
anybody knows why?
float x=0.7;
if(x<0.7)
printf(" c");
else
printf("c++");
cout<
x=0.8;
if(x<0.8)
printf(" c");
else
printf("c++");
cout< |
k****f 发帖数: 3794 | 2 浮点数表示总有有误差的。。。
【在 d*********1 的大作中提到】 : what are the results of the folloing output? : it has been tested: 1st--> c; 2nd --> c++ : anybody knows why? : : float x=0.7; : if(x<0.7) : printf(" c"); : else : printf("c++"); : cout<
|
w***g 发帖数: 5958 | 3 x是float表示的0.7; 0.7是double表示的0.7。在用<比较的时候x被从float转成了
double,尾巴上被填上了0,所以不等于0.7。但是不知道有什么方法快速地知道一个数四
舍五入时会变大还是变小。
【在 d*********1 的大作中提到】 : what are the results of the folloing output? : it has been tested: 1st--> c; 2nd --> c++ : anybody knows why? : : float x=0.7; : if(x<0.7) : printf(" c"); : else : printf("c++"); : cout<
|
d*********1 发帖数: 25 | 4 I found a solution on this:
we can define a template to eliminate this problem:
template
class compare
{
T value;
public:
compare(T &v):value(v){}
bool operator(T &s) {
// if ( s
return s
}
}; |
a**********s 发帖数: 588 | 5 Why it's so complicated.
I would just change "0.7" to "0.7f"...
【在 d*********1 的大作中提到】 : I found a solution on this: : we can define a template to eliminate this problem: : template : class compare : { : T value; : public: : compare(T &v):value(v){} : bool operator(T &s) { : // if ( s
|
m********g 发帖数: 46 | 6 It doesn't compile...
【在 d*********1 的大作中提到】 : I found a solution on this: : we can define a template to eliminate this problem: : template : class compare : { : T value; : public: : compare(T &v):value(v){} : bool operator(T &s) { : // if ( s
|