[TIL] 따배씨 14일차

전재우·2022년 4월 14일
0
post-thumbnail

관계 연산자 3_5

  • 부동 소수점의 경우 계산할때 오차가 발생할 수 있다.
  • 회사에서 프로젝트를 진행할 때 이런 문제가 발생 한적이 있었다. 앱실론을 쓰는 방법을 생각 하였다면 더 좋을텐데 하는 아쉬움이 남는다. 좋은 강의였다.
#include <iostream>
#include <cmath>

int main()
{
	using namespace std;

	int x, y;
	cin >> x >> y;
	cout << "your input Values ard : " << x << " " << y << endl;

	if (x == y)
		cout << "equal" << endl;
	if (x != y)
		cout << " NOT equal" << endl;
	if (x > y)
		cout << "x is greater than y" << endl;
	if (x < y)
		cout << "x is greater than y or eqqual to y" << endl;
	if (x >= y)
		cout << "x is less than or equal y" << endl;

	double d1(100 - 99.99); // 0.001
	double d2(10 - 9.99); // 0.001 // 부동 소수점의 경우 다른 같은 값이라도 미세한 차이가 생길 수 있다.

	const double epsilon = 1e-10; // 앱실론을 결정하는 것은 그 상황에 따라 다르다. 이런식으로 비교하는것이 좋다.

	cout << std::abs(d1 - d2) << endl;

	if (std::abs(d1 - d2) < epsilon)
	{
		cout << "equal" << endl;
	}
	else 
		cout << "not equal" << endl;




	return 0;
}
profile
코린이

0개의 댓글