d06 ex00 ajout qq tests avec limits des floats

This commit is contained in:
hugogogo
2022-03-10 16:14:01 +01:00
parent 04dea6722f
commit 31c9d8af06
10 changed files with 161 additions and 58 deletions

View File

@@ -2,29 +2,11 @@
void fromDouble(double value) {
// char
std::cout << std::setw(7) << std::left << "char";
if (value < 0 || value > std::numeric_limits<char>::max())
std::cout << B_RED << "impossible" << RESET "\n";
else if (!isprint(value))
std::cout << B_RED << "non displayable" << RESET "\n";
else
std::cout << B_CYAN << static_cast<char>(value) << RESET "\n";
toChar(value);
// int
std::cout << std::setw(7) << std::left << "int";
if (value < std::numeric_limits<int>::min()
|| value > std::numeric_limits<int>::max() )
std::cout << B_RED << "impossible" << RESET "\n";
else
std::cout << B_CYAN << static_cast<int>(value) << RESET "\n";
toInt(value);
// float
std::cout << std::setw(7) << std::left << "float";
if (value < std::numeric_limits<float>::min()
|| value > std::numeric_limits<float>::max() )
std::cout << B_RED << "impossible" << RESET "\n";
else
std::cout << B_CYAN << static_cast<double>(value) << RESET "\n";
toFloat(value);
}
bool isDouble(std::string str) {
@@ -57,8 +39,11 @@ bool checkDouble(std::string str) {
if (!isDouble(str))
return false;
std::istringstream(str) >> d;
std::cout << B_CYAN << d << B_YELLOW " double" RESET "\n";
// std::istringstream(str) >> d;
d = strtod(str.c_str(), NULL);
std::cout << std::fixed << B_CYAN << d;
// printDot(d);
std::cout << B_YELLOW " double" RESET "\n";
fromDouble(d);
return true;