Created
September 5, 2019 07:10
-
-
Save vakho10/553d19aafaa7ad635696d6b6a1ef2883 to your computer and use it in GitHub Desktop.
This file contains 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 <iostream> | |
#include <string> | |
#include <sstream> | |
#include <iomanip> | |
#include <regex> | |
#include <exception> | |
bool areSameUsingFixedScientificComparison(double x, double y) | |
{ | |
/* | |
* მაგალითად, -1.4311100000000000e+13 და -1.4311100514304000e+13 | |
*/ | |
// გადაიყვანე ორივე სამეცნიერო ფორმატში, Double-ის წილადი ნაწილის სიზუსტით (რომ არ დაამრგვალოს ბოლოში!) | |
std::string sx{}, sy{}; | |
std::stringstream oss; | |
oss << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << x; | |
sx = oss.str(); | |
oss.str(""); | |
oss << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << y; | |
sy = oss.str(); | |
oss.str(""); | |
oss.clear(); | |
// ჯერ შეადარე ექსპონენტები! | |
// ექსპონენტები ამოიკითხე სტრიქონიდან (რეგულარული გამოსახულების გამოყენებით) | |
std::regex reg("e(\\+|\\-)\\d+$"); | |
std::smatch matches; | |
int expX(0); | |
if (std::regex_search(sx, matches, reg)) { | |
if (matches.ready()) { | |
std::cout << "match: " << matches.str() << std::endl; | |
expX = std::stoi(matches.str().substr(1)); | |
} | |
} | |
std::cout << "expX = " << expX << std::endl; | |
int expY(0); | |
if (std::regex_search(sy, matches, reg)) { | |
if (matches.ready()) { | |
std::cout << "match: " << matches.str() << std::endl; | |
expY = std::stoi(matches.str().substr(1)); | |
} | |
} | |
std::cout << "expY = " << expY << std::endl; | |
// ეს შემოწმება უნდა?! :? | |
//if (expX == 0 || expY == 0) { | |
// throw std::invalid_argument("No exponents were found in the numbers. Bad numbers!"); | |
//} | |
if (expX != expY) { | |
return false; | |
} | |
// აიღე ორივედან წერტილის მერე იმდენი რიცხვი რამდენიც უნდა Float-ს. | |
// მაგალითად, -1.4311100514304000e+13-დან აიღებს -1.4311100-ს. | |
int x_dot_position = sx.find('.'); | |
std::cout << "X dot position = " << x_dot_position << std::endl; | |
// ეს შემოწმება უნდა?! :? | |
/*if (x_dot_position == std::string::npos) { | |
throw std::invalid_argument("No dot found in float string" + sx); | |
}*/ | |
int y_dot_position = sy.find('.'); | |
std::cout << "Y dot position = " << y_dot_position << std::endl; | |
// ეს შემოწმება უნდა?! :? | |
/*if (y_dot_position == std::string::npos) { | |
throw std::invalid_argument("No dot found in float string" + sy); | |
}*/ | |
std::string trimmed_sx = sx.substr(0, x_dot_position + (std::numeric_limits<float>::digits10 + 1) + 1); | |
std::string trimmed_sy = sy.substr(0, y_dot_position + (std::numeric_limits<float>::digits10 + 1) + 1); | |
std::cout << "Trimmed sx = " << trimmed_sx << std::endl; | |
std::cout << "Trimmed sy = " << trimmed_sy << std::endl; | |
// და შეადარე ეს სტრიქონები | |
return trimmed_sx == trimmed_sy; | |
} | |
int main() | |
{ | |
char chaar[] = "-143.111E11"; | |
double x = atof(chaar); | |
float y(x); | |
std::cout << std::boolalpha << areSameUsingFixedScientificComparison(x, y) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment