C++double比较的问题在新版本是被解决了吗?
  • 板块灌水区
  • 楼主XaoWa118
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/7/30 21:05
  • 上次更新2023/11/3 06:50:55
查看原帖
C++double比较的问题在新版本是被解决了吗?
741415
XaoWa118楼主2023/7/30 21:05

最近把g++升级到了13.1.0,测试了一下三向比较运算符,结果无意中发现double类型的比较居然对了

附代码与运行截图

#include <bits/stdc++.h>
using namespace std;

int main(){
    double a,b;
    while(cin >> a >> b){
        auto tmp= a<=>b;
        if(tmp==0){
            cout <<"three-way comparison:\t"<< a << "==" << b << endl;
        }
        if(tmp>0){
            cout <<"three-way comparison:\t"<< a << ">" << b << endl;
        }
        if(tmp<0){
            cout <<"three-way comparison:\t"<< a << "<" << b << endl;
        }
        if(a==b){
            cout <<"                    :\t"<< a << "==" << b << endl;
        }
        if(a>b){
            cout <<"                    :\t" << a << ">" << b << endl;
        }
        if(a<b){
            cout <<"                    :\t" << a << "<" << b << endl;
        }
    }
}

2023/7/30 21:05
加载中...