ex00: fix: inf(f) is possible now

This commit is contained in:
Camille Chauvet 2023-09-19 09:03:29 +00:00
parent 16964a3add
commit 992895067e

View File

@ -2,6 +2,7 @@
#include <cctype> #include <cctype>
#include <cerrno> #include <cerrno>
#include <cmath>
#include <cstdlib> #include <cstdlib>
#include <cstddef> #include <cstddef>
#include <exception> #include <exception>
@ -11,6 +12,7 @@
#include <float.h> #include <float.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include <math.h>
ScalarConverter::ScalarConverter() ScalarConverter::ScalarConverter()
{} {}
@ -36,7 +38,7 @@ double get_value(const std::string& str)
if (str.length() == 3 && str[0] == '\'' && str[2] == '\'') if (str.length() == 3 && str[0] == '\'' && str[2] == '\'')
return str[1]; return str[1];
double value = std::strtod(str.c_str(), &pos); double value = std::strtod(str.c_str(), &pos);
if (errno == ERANGE || (pos != NULL && strcmp(pos, "f"))) if (errno == ERANGE || (pos[0] != '\0' && strcmp(pos, "f")))
throw std::exception(); throw std::exception();
return value; return value;
} }
@ -66,10 +68,10 @@ void convert2int(const double value)
void convert2float(const double value) void convert2float(const double value)
{ {
std::cout << "float: "; std::cout << "float: ";
if (value > FLT_MAX || FLT_MIN > value) if ((value > FLT_MAX || FLT_MIN > value) && !std::isinf(value))
std::cout << "imposible"; std::cout << "imposible";
else else
std::cout << static_cast<float>(value); std::cout << static_cast<float>(value) << "f";
std::cout << std::endl; std::cout << std::endl;
} }