Created
May 10, 2021 13:50
-
-
Save recuraki/d7f301942e2b36c70af4bb971ea22bc9 to your computer and use it in GitHub Desktop.
小数の比較
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
void f(string A, string X, string Y){ | |
cout << "a, x, y = " << A << ", " << X << ", " << Y << "\n"; | |
double dA=stod(A), dX=stod(X), dY=stod(Y); | |
long double ldA=stold(A), ldX=stold(X), ldY=stold(Y); | |
long lA=stol(A), lX=stol(X), lY=stol(Y); | |
/* 入力をdoubleで比較 */ | |
if ((dA/dX) == (dA/dY)) cout << "double : a/x == a/y" << "\n"; | |
if ((dA/dX) > (dA/dY)) cout << "double : a/x > a/y" << "\n"; | |
if ((dA/dX) < (dA/dY)) cout << "double : a/x < a/y" << "\n"; | |
/* 入力をlong doubleで比較 */ | |
if ((ldA/ldX) == (ldA/ldY)) cout << "long double : a/x == a/y" << "\n"; | |
if ((ldA/ldX) > (ldA/ldY)) cout << "long double : a/x > a/y" << "\n"; | |
if ((ldA/ldX) < (ldA/ldY)) cout << "long double : a/x < a/y" << "\n"; | |
/* 入力をlongにいれて通分して比較 */ | |
if ((lA*lY) == (lA*lX)) cout << "Common long : a/x == a/y (means: a*y == a*x)" << "\n"; | |
if ((lA*lY) > (lA*lX)) cout << "Common long : a/x > a/y (means: a*y == a*x)" << "\n"; | |
if ((lA*lY) < (lA*lX)) cout << "Common long : a/x < a/y (means: a*y == a*x)" << "\n"; | |
/* 入力をdoubleにいれて通分して比較 */ | |
if ((dA*dY) == (dA*dX)) cout << "Common double: a/x == a/y (means: a*y == a*x)" << "\n"; | |
if ((dA*dY) > (dA*dX)) cout << "Common double: a/x > a/y (means: a*y == a*x)" << "\n"; | |
if ((dA*dY) < (dA*dX)) cout << "Common double: a/x < a/y (means: a*y == a*x)" << "\n"; | |
/* 入力をlong doubleにいれて通分して比較 */ | |
if ((ldA*ldY) == (ldA*ldX)) cout << "Common long double: a/x == a/y (means: a*y == a*x)" << "\n"; | |
if ((ldA*ldY) > (ldA*ldX)) cout << "Common long double: a/x > a/y (means: a*y == a*x)" << "\n"; | |
if ((ldA*ldY) < (ldA*ldX)) cout << "Common long double: a/x < a/y (means: a*y == a*x)" << "\n"; | |
} | |
int main() { | |
/* X,Yは1e16+(1,2,3,4)です。このため、 以下の入力ではAX, AY <=1e17です。 */ | |
cout << "C++\n"; | |
f("1", "10000000000000001", "10000000000000002"); | |
f("1", "10000000000000003", "10000000000000004"); | |
f("5", "10000000000000001", "10000000000000002"); | |
f("5", "10000000000000003", "10000000000000004"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment