C++浮点数比较

根据资料,C++浮点数计算时存在精度误差,在一些情况下比较浮点数可能应使用特定的比较函数;

cpp 复制代码
#include "stdafx.h"
#include<iostream>

using namespace std;

#define EPS 1e-9

int main(int argc, char* argv[])
{

	double a = 0.3;
	double b = 0.1 + 0.2;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "a-b = " << a - b << endl;

	if (abs(a - b) < EPS)  // 比较浮点数需考虑精度误差
	{
		cout << "a and b are the same" << endl;
	}
	return 0;
}
相关推荐
2601_949818096 分钟前
Vector从入门到应用(C++ STL动态数组万字全解
开发语言·c++
汉克老师30 分钟前
GESP2026年6月认证C++八级( 第三部分编程题(1、线网建设))精讲
c++·最小生成树·排序·kruskal·并查集·gesp8级
胖大和尚40 分钟前
Linux 内核工程师、HPC工程师、C++工程师
c++·kernel·hpc
想吃火锅10051 小时前
【leetcode】56.合并区间js
算法·leetcode·职场和发展
imuliuliang1 小时前
可合并堆在多任务调度中的优势与实现技巧7
算法
学究天人1 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷6)
网络·算法·数学建模·动态规划·几何学·图论·拓扑学
汤米粥1 小时前
Python爬虫中Xpath用法之——normalize-space()
开发语言·python
wabs6661 小时前
关于动态规划【力扣72.编辑距离的思考】
算法·leetcode·动态规划
科技道人1 小时前
记录 设置-显示大小和字体
开发语言·android设置·字体和显示大小
浮江雾1 小时前
Flutter第三节----Dart中的数据类型
android·开发语言·学习·flutter·入门·函数